Skip to content

perf: optimize MarshalFrame#55

Merged
mccutchen merged 6 commits intomainfrom
perf/marshalframe
May 9, 2025
Merged

perf: optimize MarshalFrame#55
mccutchen merged 6 commits intomainfrom
perf/marshalframe

Conversation

@mccutchen
Copy link
Owner

@mccutchen mccutchen commented May 8, 2025

Context

Some small optimizations for MarshalFrame (and therefore also WriteFrame) that hopefully do not harm readability/clarity of code. See also #50 for ReadFrame optimizations, from which the we learned some of the tricks below.

Summary

Note

👉 With all of these combined, it appears that we've improved WriteFrame's throughput by ~27%!

Each commit represents a separate small optimization, summarized here:

  1. small optimization for writing first two bytes to pre-allocated buffer in 73e023c
    • ~4.5% improvement in throughput (this was a bit surprising to me!)
    • benchmark results
    • (This also dropped the marshaledSize() helper in favor of pre-allocating worst-case buffer size, but that was restored in 00024a3 without meaningful impact on performance)
  2. replace modulo division with bit shifting in 0bbc064
    • Why? As discovered in perf: optimize ReadFrame #50, i % N == i & (N - 1) when N is a power of 2 and bitwise AND is usually a single CPU cycle vs multiple cycles for modulo division
    • Additional ~4% improvement over (1)
    • benchmark results
  3. use manually-optimized applyMask() from perf: optimize ReadFrame #50 in 502dcc3
  4. *use copy() instead of append() for writing mask and payload in b082698

Final results comparing 6526127 (on main) vs b082698

Copied from 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                     911.2n ± 1%   888.6n ± 3%   -2.49% (p=0.009 n=10)
ReadFrame/1MiB-4                     567.4µ ± 1%   542.2µ ± 1%   -4.44% (p=0.000 n=10)
WriteFrame/1KiB-4                   1329.5n ± 0%   944.6n ± 0%  -28.95% (p=0.000 n=10)
WriteFrame/1MiB-4                   1020.1µ ± 0%   584.4µ ± 1%  -42.71% (p=0.000 n=10)
geomean                              28.94µ        22.71µ       -21.52%

                  │ ./baseline/bench-results.txt │       ./head/bench-results.txt        │
                  │             B/s              │      B/s       vs base                │
ReadFrame/1KiB-4                    1.056Gi ± 1%    1.083Gi ± 3%   +2.55% (p=0.009 n=10)
ReadFrame/1MiB-4                    1.721Gi ± 1%    1.801Gi ± 1%   +4.65% (p=0.000 n=10)
WriteFrame/1KiB-4                   740.9Mi ± 0%   1042.9Mi ± 0%  +40.75% (p=0.000 n=10)
WriteFrame/1MiB-4                   980.4Mi ± 0%   1711.1Mi ± 1%  +74.54% (p=0.000 n=10)
geomean                             1.059Gi         1.350Gi       +27.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.016 n=10)
WriteFrame/1KiB-4                   1.125Ki ± 0%   1.125Ki ± 0%       ~ (p=1.000 n=10) ¹
WriteFrame/1MiB-4                   1.008Mi ± 0%   1.008Mi ± 0%  -0.00% (p=0.001 n=10)
geomean                             34.37Ki        34.37Ki       -0.00%
¹ 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%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                               2.236        2.236       +0.00%
¹ all samples are equal

Note: All intermediate results are combined into the edit history of #55 (comment), which shows only the latest results.

@github-actions
Copy link

github-actions bot commented May 8, 2025

🔥 Run benchmarks comparing b082698 against main:

gh workflow run bench.yaml -f pr_number=55

Note: this comment will update with each new commit.

@codecov
Copy link

codecov bot commented May 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.94%. Comparing base (6526127) to head (b082698).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #55      +/-   ##
==========================================
+ Coverage   92.81%   92.94%   +0.13%     
==========================================
  Files           2        2              
  Lines         487      496       +9     
==========================================
+ Hits          452      461       +9     
  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.

@github-actions
Copy link

github-actions bot commented May 8, 2025

benchstats: 6526127...b082698

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                     911.2n ± 1%   888.6n ± 3%   -2.49% (p=0.009 n=10)
ReadFrame/1MiB-4                     567.4µ ± 1%   542.2µ ± 1%   -4.44% (p=0.000 n=10)
WriteFrame/1KiB-4                   1329.5n ± 0%   944.6n ± 0%  -28.95% (p=0.000 n=10)
WriteFrame/1MiB-4                   1020.1µ ± 0%   584.4µ ± 1%  -42.71% (p=0.000 n=10)
geomean                              28.94µ        22.71µ       -21.52%

                  │ ./baseline/bench-results.txt │       ./head/bench-results.txt        │
                  │             B/s              │      B/s       vs base                │
ReadFrame/1KiB-4                    1.056Gi ± 1%    1.083Gi ± 3%   +2.55% (p=0.009 n=10)
ReadFrame/1MiB-4                    1.721Gi ± 1%    1.801Gi ± 1%   +4.65% (p=0.000 n=10)
WriteFrame/1KiB-4                   740.9Mi ± 0%   1042.9Mi ± 0%  +40.75% (p=0.000 n=10)
WriteFrame/1MiB-4                   980.4Mi ± 0%   1711.1Mi ± 1%  +74.54% (p=0.000 n=10)
geomean                             1.059Gi         1.350Gi       +27.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.016 n=10)
WriteFrame/1KiB-4                   1.125Ki ± 0%   1.125Ki ± 0%       ~ (p=1.000 n=10) ¹
WriteFrame/1MiB-4                   1.008Mi ± 0%   1.008Mi ± 0%  -0.00% (p=0.001 n=10)
geomean                             34.37Ki        34.37Ki       -0.00%
¹ 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%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                               2.236        2.236       +0.00%
¹ all samples are equal

@mccutchen mccutchen force-pushed the perf/marshalframe branch from 1af05bd to 00024a3 Compare May 9, 2025 04:27
@mccutchen mccutchen merged commit 658421b into main May 9, 2025
12 checks passed
@mccutchen mccutchen deleted the perf/marshalframe branch May 9, 2025 12:53
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