Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Bolt Journal

## 2024-05-24 - Initial setup
**Learning:** Initializing Bolt journal.
**Action:** Always document critical learnings here.

## 2024-05-24 - Zig jump tables for packet classification
**Learning:** In Zig, standard `switch` statements on sequential integers compile to jump tables. On hot paths (like packet classification), if there is a dominant case (e.g. data-plane packets, which make up 99.9% of traffic), extracting it into an explicit `if` branch *before* the `switch` prevents jump table overhead and improves branch prediction.
**Action:** Always extract dominant cases from switches when on critical hot paths like packet forwarding.
6 changes: 5 additions & 1 deletion src/wireguard/device.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ pub const PacketType = enum {

// WireGuard messages: first byte is type, next 3 are zeros
const msg_type = std.mem.readInt(u32, data[0..4], .little);

// Optimization: Extract dominant case (data-plane packets) to improve
// branch prediction and avoid jump table overhead on the hot path.
if (msg_type == 4) return .wg_transport;

return switch (msg_type) {
1 => .wg_handshake_init,
2 => .wg_handshake_resp,
3 => .wg_cookie,
4 => .wg_transport,
else => blk: {
// STUN: check for magic cookie at bytes 4-7
if (data.len >= 8) {
Expand Down