-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Extract dominant data-plane case to explicit if branch #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
|
|
||
| ## 2024-05-19 - Optimization: Dominant packet path extraction | ||
| **Learning:** Zig switch statements over integer values compile down to jump tables or sequential branches depending on the optimizer. In hot loops like `PacketType.classify(pkt)` checking, an enum `switch` can cause a pipeline stall on jump evaluation. | ||
| **Action:** Extracting the dominant data-plane case (e.g. `if (pkt_type == .wg_transport)`) explicitly before the `switch` statement forces the compiler to emit a direct branch instruction, which the CPU branch predictor handles much more efficiently, avoiding jump table overhead on the hot path. Remember to include the unreachable case within the switch so the code compiles. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,16 +24,20 @@ pub const PacketType = enum { | |
| stun, // STUN binding response | ||
| unknown, | ||
|
|
||
| pub fn classify(data: []const u8) PacketType { | ||
| pub inline fn classify(data: []const u8) PacketType { | ||
| if (data.len < 4) return .unknown; | ||
|
|
||
| // WireGuard messages: first byte is type, next 3 are zeros | ||
| const msg_type = std.mem.readInt(u32, data[0..4], .little); | ||
|
|
||
| // Optimization: Extract dominant data-plane case to explicit if branch to avoid jump table | ||
| if (msg_type == 4) return .wg_transport; | ||
|
Comment on lines
+27
to
+34
|
||
|
|
||
| return switch (msg_type) { | ||
| 1 => .wg_handshake_init, | ||
| 2 => .wg_handshake_resp, | ||
| 3 => .wg_cookie, | ||
| 4 => .wg_transport, | ||
| 4 => unreachable, | ||
| else => blk: { | ||
| // STUN: check for magic cookie at bytes 4-7 | ||
| if (data.len >= 8) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This note mixes “switch statements over integer values” with “an enum
switch”, which is a bit inconsistent/confusing relative to the actual implementation (the hot switch inPacketType.classifyis over an integermsg_type, and the event loops switch over thePacketTypeenum). Consider rewording to consistently describe which switch is being optimized (integermsg_typevsPacketTypeenum) so future readers don’t misapply the guidance.