refactor: complete rewrite of the frame type#39
Merged
Conversation
With this experimental refactor, a decoded websocket frame is represented in memory as a single header byte and a payload byte slice. The single header byte gives us all the info we need at the application level (opcode, fin bit, RSV bits). Because we unmask the payload at read time and already require callers to specify a mask when writing a frame, we do not need to store the masked bit or the mask key in the frame itself. (This does, however, require passing a mode into ReadFrame so that we can still reject unmasked client frames.) Because correctly constructing the header byte for a frame is tricky, a new `NewFrame` constructor is now the only way to create a frame.
|
🔥 Run benchmarks comparing df776d4 against gh workflow run bench.yaml -f pr_number=39Note: this comment will update with each new commit. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #39 +/- ##
==========================================
+ Coverage 92.44% 92.65% +0.20%
==========================================
Files 3 3
Lines 437 449 +12
==========================================
+ Hits 404 416 +12
Misses 26 26
Partials 7 7 ☔ View full report in Codecov by Sentry. |
benchstats: ef31006...40c87b9View full benchmark output on the workflow summary. |
mccutchen
commented
Feb 5, 2025
Comment on lines
-39
to
+44
| OpcodeContinuation Opcode = 0x0 | ||
| OpcodeText Opcode = 0x1 | ||
| OpcodeBinary Opcode = 0x2 | ||
| OpcodeClose Opcode = 0x8 | ||
| OpcodePing Opcode = 0x9 | ||
| OpcodePong Opcode = 0xA | ||
| OpcodeContinuation Opcode = 0b0000_0000 // 0x0 | ||
| OpcodeText Opcode = 0b0000_0001 // 0x1 | ||
| OpcodeBinary Opcode = 0b0000_0010 // 0x2 | ||
| OpcodeClose Opcode = 0b0000_1000 // 0x8 | ||
| OpcodePing Opcode = 0b0000_1001 // 0x9 | ||
| OpcodePong Opcode = 0b0000_1010 // 0xA |
Owner
Author
There was a problem hiding this comment.
Tbh this extra verbose binary representation makes it easier for me to understand all the bitwise operations happening here.
Owner
Author
|
Interesting that the ReadMessage and ReadFrame benchmarks got slower, consistent across a sample size of two runs. Probably need to try some profiling here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With this experimental refactor, a decoded websocket frame is represented in memory as a single header byte and a payload byte slice.
The single header byte gives us all the info we need at the application level (opcode, fin bit, RSV bits).
Because we unmask the payload at read time and already require callers to specify a mask when writing a frame, we do not need to store the masked bit or the mask key in the frame itself. (This does, however, require passing a mode into ReadFrame so that we can still reject unmasked client frames.)
Because correctly constructing the header byte for a frame is tricky, a new
NewFrameconstructor is now the only way to create a frame.