Skip to content

perf: optimize writes of small payloads using internal []byte buffer pools#59

Closed
mccutchen wants to merge 4 commits intomainfrom
marshalto
Closed

perf: optimize writes of small payloads using internal []byte buffer pools#59
mccutchen wants to merge 4 commits intomainfrom
marshalto

Conversation

@mccutchen
Copy link
Owner

@mccutchen mccutchen commented Sep 20, 2025

Optimize writes of small (<= 8KiB) payloads by using a set of internal []byte buffer pools in WriteFrame to minimize allocations.

@github-actions
Copy link

github-actions bot commented Sep 20, 2025

🔥 Run benchmarks comparing cc642fc against main:

gh workflow run bench.yaml -f pr_number=59

Note: this comment will update with each new commit.

@github-actions
Copy link

github-actions bot commented Sep 20, 2025

benchstats: 7aee74c...cc642fc

View full benchmark output on the workflow summary.

goos: linux
goarch: amd64
pkg: github.com/mccutchen/websocket
cpu: AMD EPYC 7763 64-Core Processor                
                  │ ./baseline/bench-results.txt │      ./head/bench-results.txt       │
                  │            sec/op            │   sec/op     vs base                │
ReadFrame/1KiB-4                     577.6n ± 1%   592.7n ± 1%   +2.61% (p=0.000 n=10)
ReadFrame/1MiB-4                     281.6µ ± 3%   290.9µ ± 3%   +3.30% (p=0.015 n=10)
WriteFrame/1KiB-4                    671.6n ± 0%   535.0n ± 1%  -20.34% (p=0.000 n=10)
WriteFrame/1MiB-4                    358.0µ ± 6%   330.6µ ± 4%   -7.65% (p=0.003 n=10)
geomean                              14.06µ        13.21µ        -6.03%

                  │ ./baseline/bench-results.txt │       ./head/bench-results.txt       │
                  │             B/s              │     B/s       vs base                │
ReadFrame/1KiB-4                    1.665Gi ± 1%   1.623Gi ± 1%   -2.53% (p=0.000 n=10)
ReadFrame/1MiB-4                    3.468Gi ± 3%   3.357Gi ± 3%   -3.19% (p=0.015 n=10)
WriteFrame/1KiB-4                   1.433Gi ± 0%   1.798Gi ± 1%  +25.53% (p=0.000 n=10)
WriteFrame/1MiB-4                   2.728Gi ± 6%   2.954Gi ± 4%   +8.28% (p=0.003 n=10)
geomean                             2.180Gi        2.320Gi        +6.42%

                  │ ./baseline/bench-results.txt │        ./head/bench-results.txt        │
                  │             B/op             │     B/op      vs base                  │
ReadFrame/1KiB-4                    1.164Ki ± 0%   1.164Ki ± 0%        ~ (p=1.000 n=10) ¹
ReadFrame/1MiB-4                    1.008Mi ± 0%   1.008Mi ± 0%    0.00% (p=0.036 n=10)
WriteFrame/1KiB-4                   1152.00 ± 0%     24.00 ± 0%  -97.92% (p=0.000 n=10)
WriteFrame/1MiB-4                   1.008Mi ± 0%   1.008Mi ± 0%   +0.00% (p=0.000 n=10)
geomean                             34.37Ki        13.06Ki       -62.01%
¹ all samples are equal

                  │ ./baseline/bench-results.txt │       ./head/bench-results.txt        │
                  │          allocs/op           │ allocs/op   vs base                   │
ReadFrame/1KiB-4                      5.000 ± 0%   5.000 ± 0%         ~ (p=1.000 n=10) ¹
ReadFrame/1MiB-4                      5.000 ± 0%   5.000 ± 0%         ~ (p=1.000 n=10) ¹
WriteFrame/1KiB-4                     1.000 ± 0%   1.000 ± 0%         ~ (p=1.000 n=10) ¹
WriteFrame/1MiB-4                     1.000 ± 0%   2.000 ± 0%  +100.00% (p=0.000 n=10)
geomean                               2.236        2.659        +18.92%
¹ all samples are equal

@codecov
Copy link

codecov bot commented Sep 20, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.21%. Comparing base (7aee74c) to head (cc642fc).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #59      +/-   ##
==========================================
+ Coverage   92.94%   93.21%   +0.27%     
==========================================
  Files           2        2              
  Lines         496      516      +20     
==========================================
+ Hits          461      481      +20     
  Misses         28       28              
  Partials        7        7              

☔ 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.

@mccutchen
Copy link
Owner Author

While this does yield the expected performance improvements, I'm going to close it for now as it feels like the additional complexity is not worth it. Would rather keep the code a bit cleaner/simpler than chase maximum performance at the moment.

Might revisit later!

@mccutchen mccutchen closed this Sep 21, 2025
@mccutchen mccutchen changed the title perf: use internal buffer pool when marshaling frames perf: optimize writes of small payloads using internal []byte buffer pools Sep 21, 2025
mccutchen added a commit that referenced this pull request Sep 21, 2025
- drop `MarshalFrame` from the public API
- pull in a few small improvements to its implementation from #59
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.

1 participant