Skip to content

Elide nonce allocation in encrypt path#36

Merged
arashpayan merged 4 commits intomainfrom
elide_nonce_allocation
Mar 10, 2026
Merged

Elide nonce allocation in encrypt path#36
arashpayan merged 4 commits intomainfrom
elide_nonce_allocation

Conversation

@rakitzis
Copy link
Copy Markdown
Collaborator

Generate the nonce directly into the transform header buffer instead of
allocating a separate slice and copying it. This removes one allocation
from the hot path per encrypted send/receive.

Before:

BenchmarkRoundTrip/Encrypted/64KB-10 38607 31706 ns/op 2067.02 MB/s 164634 B/op 9 allocs/op

After:

BenchmarkRoundTrip/Encrypted/64KB-10 39374 31195 ns/op 2100.83 MB/s 164614 B/op 8 allocs/op

Exercises the send/recv path through conn using net.Pipe and a fake
server, measuring throughput at different payload sizes for plain
and encrypted sessions.
Generate the nonce directly into the transform header buffer instead of
allocating a separate slice and copying it. This removes one allocation
from the hot path per encrypted send/receive.

Before:

BenchmarkRoundTrip/Encrypted/64KB-10    	   38607	     31706 ns/op	2067.02 MB/s	  164634 B/op	       9 allocs/op

After:

BenchmarkRoundTrip/Encrypted/64KB-10    	   39374	     31195 ns/op	2100.83 MB/s	  164614 B/op	       8 allocs/op
@arashpayan arashpayan merged commit 9ec4b41 into main Mar 10, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants