Skip to content

CCM Encrypt Speed + Memory Mgmt Improvements#784

Merged
adrianosela merged 1 commit intopion:masterfrom
adrianosela:ccm-perf
Jan 22, 2026
Merged

CCM Encrypt Speed + Memory Mgmt Improvements#784
adrianosela merged 1 commit intopion:masterfrom
adrianosela:ccm-perf

Conversation

@adrianosela
Copy link
Copy Markdown
Contributor

@adrianosela adrianosela commented Jan 22, 2026

CCM Encrypt Speed + Memory Mgmt Improvements

Note: only encrypt is modified, decrypt is unchanged.

Pretty significant results by doing some minor tweaks:

  • Pre-calculating the exact output size
  • Having Seal() write encrypted data directly into the final buffer eliminates an intermediate allocation and copy operation.
  • Buffer pool use for nonces

Improvements Summary

  • Encrypt latency: -7% to -11% improvement
  • Encrypt throughput: +8% to +12% improvement
  • Encrypt allocations: -4 allocs/op (~30% reduction: 11 to 7)
  • Encrypt memory: -12% to -47% reduction (scales with payload size)

Raw benchmark output before and after

Before

20:19 $ go test -tags=bench -bench=CCM -benchmem
goos: darwin
goarch: arm64
pkg: github.com/pion/dtls/v3/pkg/crypto/ciphersuite
cpu: Apple M1 Pro
BenchmarkCCMEncrypt/016B-8         	 2874972	       406.6 ns/op	  39.35 MB/s	     776 B/op	      11 allocs/op
BenchmarkCCMEncrypt/064B-8         	 2421391	       501.9 ns/op	 127.51 MB/s	    1016 B/op	      11 allocs/op
BenchmarkCCMEncrypt/256B-8         	 1417029	       852.7 ns/op	 300.24 MB/s	    2040 B/op	      11 allocs/op
BenchmarkCCMEncrypt/512B-8         	  890650	      1299 ns/op	 394.20 MB/s	    3224 B/op	      11 allocs/op
BenchmarkCCMEncrypt/1KB-8          	  528723	      2164 ns/op	 473.14 MB/s	    5592 B/op	      11 allocs/op
BenchmarkCCMEncrypt/1.4KB-8        	  380935	      2944 ns/op	 509.48 MB/s	    7512 B/op	      11 allocs/op
BenchmarkCCMDecrypt/016B-8         	 4245405	       282.5 ns/op	  56.63 MB/s	     680 B/op	       9 allocs/op
BenchmarkCCMDecrypt/064B-8         	 3032066	       355.1 ns/op	 180.22 MB/s	     776 B/op	       9 allocs/op
BenchmarkCCMDecrypt/256B-8         	 1849956	       646.6 ns/op	 395.91 MB/s	    1176 B/op	       9 allocs/op
BenchmarkCCMDecrypt/512B-8         	 1000000	      1061 ns/op	 482.58 MB/s	    1688 B/op	       9 allocs/op
BenchmarkCCMDecrypt/1KB-8          	  612861	      1802 ns/op	 568.34 MB/s	    2776 B/op	       9 allocs/op
BenchmarkCCMDecrypt/1.4KB-8        	  421500	      2586 ns/op	 580.13 MB/s	    3928 B/op	       9 allocs/op
PASS
ok  	github.com/pion/dtls/v3/pkg/crypto/ciphersuite	17.370s

After

20:16 $ go test -tags=bench -bench=CCM -benchmem
goos: darwin
goarch: arm64
pkg: github.com/pion/dtls/v3/pkg/crypto/ciphersuite
cpu: Apple M1 Pro
BenchmarkCCMEncrypt/016B-8         	 3241846	       358.9 ns/op	  44.58 MB/s	     672 B/op	       7 allocs/op
BenchmarkCCMEncrypt/064B-8         	 2797452	       428.1 ns/op	 149.51 MB/s	     768 B/op	       7 allocs/op
BenchmarkCCMEncrypt/256B-8         	 1642609	       751.3 ns/op	 340.76 MB/s	    1184 B/op	       7 allocs/op
BenchmarkCCMEncrypt/512B-8         	 1000000	      1105 ns/op	 463.20 MB/s	    1728 B/op	       7 allocs/op
BenchmarkCCMEncrypt/1KB-8          	  615655	      1847 ns/op	 554.28 MB/s	    2880 B/op	       7 allocs/op
BenchmarkCCMEncrypt/1.4KB-8        	  435160	      2587 ns/op	 579.91 MB/s	    3905 B/op	       7 allocs/op
BenchmarkCCMDecrypt/016B-8         	 4267266	       279.1 ns/op	  57.32 MB/s	     680 B/op	       9 allocs/op
BenchmarkCCMDecrypt/064B-8         	 3391534	       353.0 ns/op	 181.30 MB/s	     776 B/op	       9 allocs/op
BenchmarkCCMDecrypt/256B-8         	 1853229	       642.2 ns/op	 398.65 MB/s	    1176 B/op	       9 allocs/op
BenchmarkCCMDecrypt/512B-8         	 1000000	      1028 ns/op	 497.90 MB/s	    1688 B/op	       9 allocs/op
BenchmarkCCMDecrypt/1KB-8          	  629172	      1806 ns/op	 567.10 MB/s	    2776 B/op	       9 allocs/op
BenchmarkCCMDecrypt/1.4KB-8        	  449751	      2564 ns/op	 584.91 MB/s	    3928 B/op	       9 allocs/op
PASS
ok  	github.com/pion/dtls/v3/pkg/crypto/ciphersuite	17.022s

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.73%. Comparing base (100a3aa) to head (ca879ed).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #784      +/-   ##
==========================================
+ Coverage   81.68%   81.73%   +0.04%     
==========================================
  Files         105      105              
  Lines        5838     5847       +9     
==========================================
+ Hits         4769     4779      +10     
  Misses        686      686              
+ Partials      383      382       -1     
Flag Coverage Δ
go 81.73% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Sean-Der
Copy link
Copy Markdown
Member

These changes are amazing. I think it is worth a dedicated announce!

'FYI if you are running DataChannel servers and pushing lots of data you should see this CPU usage drop' cool stuff

@Sean-Der
Copy link
Copy Markdown
Member

would you mind auditing pion/srtp also? I haven't looked deeply, but could be some wins

@JoTurk
Copy link
Copy Markdown
Member

JoTurk commented Jan 22, 2026

would you mind auditing pion/srtp also? I haven't looked deeply, but could be some wins

We have audit tool now SCP, we're going to run it in the CI and make it generic so it's easier to benchmark changes like this :)

Encrypt latency: -7% to -11% improvement
Encrypt throughput: +8% to +12% improvement
Encrypt allocations: -4 allocs/op (30.8% reduction: 13 to 9)
Encrypt memory: -12% to -47% reduction (scales with payload size)
@adrianosela
Copy link
Copy Markdown
Contributor Author

Rebased onto master, merging.

@adrianosela adrianosela merged commit 5b824b0 into pion:master Jan 22, 2026
27 of 28 checks passed
@adrianosela adrianosela deleted the ccm-perf branch January 22, 2026 05:27
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.

3 participants