Skip to content

perf: eliminate bounds checks in applyMask#53

Merged
mccutchen merged 1 commit intomainfrom
perf/bounds-checks
Apr 30, 2025
Merged

perf: eliminate bounds checks in applyMask#53
mccutchen merged 1 commit intomainfrom
perf/bounds-checks

Conversation

@mccutchen
Copy link
Owner

@mccutchen mccutchen commented Apr 30, 2025

Context

A small follow-up to #50, inspired by this blog post by @jub0bs that walks through optimizing away bounds checks (as well as enabling inlining).

Note, there are still two bounds checks per applyMask(), on line 494 and line 504:

$ go build -gcflags '-d=ssa/check_bce/debug=1'
# github.com/mccutchen/websocket
./proto.go:322:28: Found IsInBounds
./proto.go:368:60: Found IsSliceInBounds
./proto.go:439:49: Found IsSliceInBounds
./proto.go:494:19: Found IsSliceInBounds
./proto.go:504:22: Found IsSliceInBounds

But that's an improvement on the 9 bounds checks we were seeing previously:

$ go build -gcflags '-d=ssa/check_bce/debug=1'
# github.com/mccutchen/websocket
./proto.go:322:28: Found IsInBounds
./proto.go:368:60: Found IsSliceInBounds
./proto.go:439:49: Found IsSliceInBounds
./proto.go:493:10: Found IsInBounds
./proto.go:494:10: Found IsInBounds
./proto.go:495:10: Found IsInBounds
./proto.go:496:10: Found IsInBounds
./proto.go:497:10: Found IsInBounds
./proto.go:498:10: Found IsInBounds
./proto.go:499:10: Found IsInBounds
./proto.go:500:10: Found IsInBounds
./proto.go:503:10: Found IsInBounds

Results

A nice little ~4.5% improvement to throughput according to the results posted below:

                  │ ./baseline/bench-results.txt │      ./head/bench-results.txt       │
                  │            sec/op            │   sec/op     vs base                │
ReadFrame/1KiB-4                     982.4n ± 1%   906.8n ± 2%   -7.71% (p=0.000 n=10)
ReadFrame/1MiB-4                     628.9µ ± 0%   561.0µ ± 1%  -10.79% (p=0.000 n=10)
WriteFrame/1KiB-4                    1.341µ ± 0%   1.360µ ± 1%   +1.42% (p=0.000 n=10)
WriteFrame/1MiB-4                    1.039m ± 1%   1.035m ± 1%        ~ (p=0.105 n=10)
geomean                              30.46µ        29.08µ        -4.51%

                  │ ./baseline/bench-results.txt │       ./head/bench-results.txt        │
                  │             B/s              │      B/s       vs base                │
ReadFrame/1KiB-4                   1002.8Mi ± 1%   1086.4Mi ± 2%   +8.35% (p=0.000 n=10)
ReadFrame/1MiB-4                    1.553Gi ± 0%    1.741Gi ± 1%  +12.10% (p=0.000 n=10)
WriteFrame/1KiB-4                   735.1Mi ± 0%    724.5Mi ± 1%   -1.44% (p=0.000 n=10)
WriteFrame/1MiB-4                   962.5Mi ± 1%    966.6Mi ± 1%        ~ (p=0.105 n=10)
geomean                             1.006Gi         1.054Gi        +4.71%

Edit: realized after the fact that I didn't actually link to the blog post that showed me how to do this!

@github-actions
Copy link

🔥 Run benchmarks comparing c168278 against main:

gh workflow run bench.yaml -f pr_number=53

Note: this comment will update with each new commit.

@codecov
Copy link

codecov bot commented Apr 30, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.81%. Comparing base (3cb163b) to head (c168278).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #53      +/-   ##
==========================================
+ Coverage   92.76%   92.81%   +0.04%     
==========================================
  Files           2        2              
  Lines         484      487       +3     
==========================================
+ Hits          449      452       +3     
  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

benchstats: 3cb163b...c168278

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                     982.4n ± 1%   906.8n ± 2%   -7.71% (p=0.000 n=10)
ReadFrame/1MiB-4                     628.9µ ± 0%   561.0µ ± 1%  -10.79% (p=0.000 n=10)
WriteFrame/1KiB-4                    1.341µ ± 0%   1.360µ ± 1%   +1.42% (p=0.000 n=10)
WriteFrame/1MiB-4                    1.039m ± 1%   1.035m ± 1%        ~ (p=0.105 n=10)
geomean                              30.46µ        29.08µ        -4.51%

                  │ ./baseline/bench-results.txt │       ./head/bench-results.txt        │
                  │             B/s              │      B/s       vs base                │
ReadFrame/1KiB-4                   1002.8Mi ± 1%   1086.4Mi ± 2%   +8.35% (p=0.000 n=10)
ReadFrame/1MiB-4                    1.553Gi ± 0%    1.741Gi ± 1%  +12.10% (p=0.000 n=10)
WriteFrame/1KiB-4                   735.1Mi ± 0%    724.5Mi ± 1%   -1.44% (p=0.000 n=10)
WriteFrame/1MiB-4                   962.5Mi ± 1%    966.6Mi ± 1%        ~ (p=0.105 n=10)
geomean                             1.006Gi         1.054Gi        +4.71%

                  │ ./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%       ~ (p=0.121 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%       ~ (p=0.481 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 merged commit 6526127 into main Apr 30, 2025
12 checks passed
@mccutchen mccutchen deleted the perf/bounds-checks branch April 30, 2025 22:28
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