Repository navigation
Add blockInPlace for zero-allocation blocking operations - #311
Conversation
Add a new blockInPlace function that runs blocking functions on the thread pool without allocating. Unlike spawnBlocking, all state is kept on the stack. Supports cancellation by attempting to cancel the work and waiting for completion. Also adds task.getThreadPool() helper and updates HostName.lookup to use blockInPlace instead of spawnBlocking.
📝 WalkthroughWalkthroughThis PR introduces Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~28 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
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
🤖 Fix all issues with AI agents
In `@src/common.zig`:
- Around line 372-384: The test calls blockInPlace directly on the main thread
so Waiter.init sees getCurrentTaskOrNull() == null and takes the fast
direct-call path, leaving the thread-pool submission/completion/cancellation
logic untested; update the test to initialize the Runtime with a thread_pool
(Runtime.init with .{ .thread_pool = .{} }) and spawn a task that calls
blockInPlace (use Runtime.spawn to run a Test.run that invokes blockInPlace on
the worker), then join the handle so the test executes the thread-pool code
paths (submission, completion callback and cancellation) rather than the
main-thread fast path.
🧹 Nitpick comments (1)
src/common.zig (1)
370-370: Import buried at the bottom of the file.All other imports live at lines 4-16. This one at line 370 is easy to miss. Consider moving it to the top with the rest.
| test "blockInPlace: basic computation" { | ||
| var rt = try Runtime.init(std.testing.allocator, .{}); | ||
| defer rt.deinit(); | ||
|
|
||
| const double = struct { | ||
| fn call(x: i32) i32 { | ||
| return x * 2; | ||
| } | ||
| }.call; | ||
|
|
||
| const result = blockInPlace(double, .{21}); | ||
| try std.testing.expectEqual(42, result); | ||
| } |
There was a problem hiding this comment.
Test doesn't exercise the thread pool path.
This test runs blockInPlace on the main thread with no task context. Waiter.init() inside blockInPlace will see getCurrentTaskOrNull() == null, so it takes the fast path (line 349-351) and calls the function directly. The thread pool submission, completion callback, and cancellation logic are all untested.
To actually hit the interesting codepath, you'd need to run blockInPlace from inside a spawned task, similar to how the HostName: lookup tests do it:
const rt = try Runtime.init(std.testing.allocator, .{ .thread_pool = .{} });
defer rt.deinit();
const Test = struct {
fn run() !void {
const result = blockInPlace(double, .{21});
try std.testing.expectEqual(42, result);
}
};
var handle = try rt.spawn(Test.run, .{});
try handle.join();🤖 Prompt for AI Agents
In `@src/common.zig` around lines 372 - 384, The test calls blockInPlace directly
on the main thread so Waiter.init sees getCurrentTaskOrNull() == null and takes
the fast direct-call path, leaving the thread-pool
submission/completion/cancellation logic untested; update the test to initialize
the Runtime with a thread_pool (Runtime.init with .{ .thread_pool = .{} }) and
spawn a task that calls blockInPlace (use Runtime.spawn to run a Test.run that
invokes blockInPlace on the worker), then join the handle so the test executes
the thread-pool code paths (submission, completion callback and cancellation)
rather than the main-thread fast path.
There was a problem hiding this comment.
Runtime.init() turns the main thread into task
Summary
blockInPlacefunction that runs blocking functions on the thread pool without allocations (all state kept on stack)task.getThreadPool()helper methodHostName.lookupto useblockInPlaceinstead ofspawnBlockingTest plan
blockInPlace: basic computationtest added