Repository navigation
Add CompactConcurrentQueue (8-byte variant) with benchmarks - #58
Conversation
Implements a space-optimized version of ConcurrentQueue that fits in 8 bytes by storing only the head pointer. The tail pointer is stored in the head node itself, rather than as a separate field. Key features: - Queue structure: 8 bytes (only atomic head pointer) - Compatible with std.Io.Mutex/Condition 64-bit state - Same O(1) operations as standard ConcurrentQueue - Nodes require additional 'tail' field (8 bytes per node) Trade-offs: + Fits in 64-bit atomic word + Maintains all functionality of standard queue - Each node 8 bytes larger (tail field mostly unused) - Slightly more complex operations (tail copying on updates) Includes comprehensive tests and benchmark suite comparing both implementations for single-threaded and concurrent scenarios. Co-authored-by: Lukáš Lalinský <[email protected]>
WalkthroughAdds an 8-byte CompactConcurrentQueue implementation, updates sync primitives to use it (and adds a tail field to WaitNode), exposes it via the public zio API, and adds a benchmark suite and build entry to compare ConcurrentQueue vs CompactConcurrentQueue in single- and multi-threaded scenarios. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller as Caller (bench/test)
participant Q as CompactConcurrentQueue
participant Worker as Worker Thread
participant Head as Head Node Memory
Note over Caller,Q: Setup
Caller->>Q: initWithState(.sentinel1)
Note over Worker,Q: Concurrent push phase (many threads)
par
Worker->>Q: acquireMutationLock() %% spin and CAS head pointer with mutation bit
Worker->>Head: append item to head.tail list
Worker->>Q: releaseMutationLock()
and
Worker->>Q: push(item) %% high-level
end
Note over Caller,Q: Pop/Drain phase (single thread)
Caller->>Q: pop()
Q->>Head: read head + next
Q->>Q: possibly transition sentinel states / carry tail
Q-->>Caller: return item / null
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (3)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
🔇 Additional comments (7)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (4)
src/utils/concurrent_queue.zig (3)
823-872: Add compile-time validation for T’s fields and alignmentEnforce presence of next/prev/tail and require at least 4‑byte alignment to guarantee two tag bits. Prevents UB in release builds.
pub fn CompactConcurrentQueue(comptime T: type) type { return struct { const Self = @This(); + const meta = std.meta; + comptime { + // Require structural fields + if (meta.fieldIndex(T, "next") == null or + meta.fieldIndex(T, "prev") == null or + meta.fieldIndex(T, "tail") == null) + { + @compileError("CompactConcurrentQueue(T) requires fields: next, prev, tail"); + } + // Require at least 2 tag bits => 4-byte alignment + if (@alignOf(T) < 4) { + @compileError("CompactConcurrentQueue(T) requires @alignOf(T) >= 4 (two tag bits)."); + } + }
992-1052: Optional: strengthen debug invariants in remove()Consider asserting item.in_list in Debug to catch accidental double-removals earlier (tests already cover behavior). Purely debug-time.
- if (builtin.mode == .Debug) { - item.in_list = false; - } + if (builtin.mode == .Debug) { + std.debug.assert(item.in_list); + item.in_list = false; + }
1147-1149: Make size assertion portable across 32/64-bit targetsOn 32‑bit, the queue will be 4 bytes. Gate by pointer size.
- // The whole point: verify it's only 8 bytes! - try testing.expectEqual(8, @sizeOf(Queue)); + if (@sizeOf(usize) == 8) { + try testing.expectEqual(8, @sizeOf(Queue)); + } else { + try testing.expectEqual(@sizeOf(usize), @sizeOf(Queue)); + }benchmarks/concurrent_queue_bench.zig (1)
278-286: Optional: scale threads with CPU count and allow overridesImproves portability and out-of-the-box relevance.
- const num_threads = 4; + const num_threads = std.math.max(1, std.Thread.getCpuCount() orelse 4); const ops_per_thread = 2_500; - const concurrent_iterations = 50; + const concurrent_iterations = 50;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
benchmarks/concurrent_queue_bench.zig(1 hunks)build.zig(1 hunks)src/utils/concurrent_queue.zig(1 hunks)src/zio.zig(1 hunks)
🔇 Additional comments (4)
src/utils/concurrent_queue.zig (1)
924-955: push path looks correct with lock handoff via store(.release)Initialization, tail carry, and unlock semantics are sound. Nice.
build.zig (1)
48-48: LGTM: benchmark wired into build graphEntry is correct; integrates with existing loop and zio import.
src/zio.zig (1)
37-41: LGTM: clean re-exportsPublic surface exposes both variants without breaking existing imports.
benchmarks/concurrent_queue_bench.zig (1)
1-86: Bench logic and accounting look solidPush/pop counts validated; timing aggregation is reasonable.
Also applies to: 88-186, 198-251
|
@claude use this variant in mutex and condition |
|
Claude finished @lalinsky's task —— View job Completed: Integrated CompactConcurrentQueue into Mutex and ConditionChanges Made
Both Mutex and Condition primitives now use the 8-byte CompactConcurrentQueue variant, fitting within the 64-bit state requirement for Test ResultsAll 108 tests passed successfully ✓ CommitPushed commit Branch: claude/issue-57-20251019-0756 |
- Replace ConcurrentQueue with CompactConcurrentQueue in Mutex and Condition - Add tail field to WaitNode for CompactConcurrentQueue compatibility - Both primitives now fit within 64-bit state requirement Co-authored-by: Lukáš Lalinský <[email protected]>
* Add file_stat operation Add FileStat and FileStatPath operations for retrieving file metadata: - FileStat: stat by file descriptor (fd-based) - FileStatPath: stat by path relative to directory Returns FileStatInfo struct with fields: inode, size, mode, kind, atime, mtime, ctime (timestamps in nanoseconds). io_uring backend uses native statx() syscall; other backends use thread pool fallback with blocking fstat()/fstatat(). * Fix macOS build: use atime/mtime/ctime methods for cross-platform Stat access * Use statx on Linux for file stat operations * Fix Windows build: bitcast inode to i64 * Use statx on Linux for fsize as well * Merge FileStat and FileStatPath into single operation Consolidate the two stat operations into one by making the path parameter optional (null = stat fd itself, non-null = stat path relative to fd). API changes: - FileStat.init(fd) -> FileStat.init(fd, null) - FileStatPath.init(dir, path) -> FileStat.init(dir, path) - FileStatPath type removed * Fix memory leak when stat path is empty string
Summary
Implements a space-optimized version of ConcurrentQueue that fits in 8 bytes for compatibility with
std.Io.MutexandCondition64-bit state.Key features:
tailfieldTrade-offs:
Closes #57
Generated with Claude Code
Summary by CodeRabbit
New Features
Updates
Chores