In Zig app code you don't have to pass allocators all over if you don't want to, that's your code so do what you want. But for libraries it's the right thing to do because it puts the caller in control.
The reason you have to pass allocators instead of making this implicit like in e.g. Odin, is because Zig just takes a firm stance against implicit logic, firmer than any other language I've ever used. The end result is that people who like Zig tend to like passing allocators around even in their own code.
[–] latch 2 days ago
[–] bunderbunder 1 day ago
Zig seems to be laser focused on systems programming domains where there is value in keeping unusually tight control over everything the program does. Aside from tinkering with Arduino, never been me. If it were, though, then I would probably not want to use a library that wants to be able to arbitrarily grow ArrayLists. I would probably want to structure my program so that it acquires pages of memory (actual memory, not just overcommit) in as few places as possible - ideally one - and from a location very close to the entry point. And a library that doesn’t play nice with that wouldn’t be much more palatable than a library that wants to bring its own memory management runtime along for the ride.
I don’t think that’s totally unreasonable? It’s more restrictive than what most of us are used to, sure. But it’s also quite a bit less restrictive than completely banning dynamic heap allocation as is common in aerospace applications.
[–] elcritch 2 days ago
I bought that as Zig's M.O., but then came the automatic "async/IO" colorless functions which is a huge chunk of implicit and hidden logic.
That's more implicit logic than what's in say JavaScript async AFAICT. Arbitrary int sizes with implicit casting rules, etc. More recently was the attempts of memory safety via an some sort of (optional) implicit GC mode. Personally I don't get it.
[–] Mond_ 2 days ago
The async design changed multiple times, and the current approach is that of requiring passing an I/O interface that encapsulates the logic, same approach as for passing around an allocator.
This effectively makes all I/O bound code "quasi-async", and it's all explicit and the caller is in control.
[–] simonask 1 day ago
This has a lot of implications, especially for function pointers and other forms of dynamic dispatch.
My personal take is that async code is fundamentally different from sync code, because the weights of various tradeoffs change dramatically. In my view, "colorless" functions are not really a worthwhile goal in the first place. But I've been wrong before, so it may turn out to be great, we'll see.
[–] lukaslalinsky 1 day ago
[–] elcritch 1 day ago
Yeah so that's a lot of hidden magic and implicit logic. ¯\_(ツ)_/¯
[–] Zambyte 1 day ago
[–] tialaramex 1 day ago
If you look at how it's implemented you can see that the classic "XOR a register with itself to zero that register" trick is in fact basically free in a modern CPU rather than using up the ALU because of how register renaming is implemented. But hopefully we're not pretending that "You could buy the CPU vendor's technical manuals" makes this now explicit.
[–] lukaslalinsky 1 day ago
[–] imtringued 23 hours ago
const std = @import("std");
fn performTasks(io: std.Io) !void {
var group: std.Io.Group = .init;
errdefer group.cancel(io);
// Launch concurrent background jobs
try group.async(io, simulateWorker, .{ io, 1 });
try group.async(io, simulateWorker, .{ io, 2 });
// Wait for all group tasks to complete
try group.await(io);
}
fn simulateWorker(io: std.Io, id: usize) void {
std.log.info("Worker {d} started...", .{id});
// Any blocking operation (like sleeping) routes through 'io'
// Under the hood, this will either block an OS thread or yield an event loop stack.
io.sleep(.fromSeconds(1), .awake) catch {};
std.log.info("Worker {d} finished!", .{id});
}
pub fn main() !void {
var gpa_alloc = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa_alloc.deinit();
const allocator = gpa_alloc.allocator();
// Initialize the Threaded I/O backend
var threaded = std.Io.Threaded.init(allocator, .{
.async_limit = std.Io.Limit.limited(4), // Max concurrent tasks in the pool
});
defer threaded.deinit();
// Extract the generic I/O interface pointer
const io = threaded.io();
std.log.info("Running on Threaded backend:", .{});
try performTasks(io);
}
pub fn main() !void {
var gpa_alloc = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa_alloc.deinit();
const allocator = gpa_alloc.allocator();
// Initialize the Evented (non-blocking) backend
var evented: std.Io.Evented = undefined;
try evented.init(allocator, .{
.thread_limit = 4, // Worker threads running the event loop
});
defer evented.deinit();
// Extract the exact same generic interface
const io = evented.io();
std.log.info("Running on Evented backend:", .{});
try performTasks(io);
}
std.Io.Threaded and std.Io.Evented are two types of IO backends. std.Io.Threaded uses blocking IO primitives and Evented uses asynchronous IO primitives. Switching between backends is done via dynamic dispatch, the used backend is passed in via an explicit function parameter.The only real question you should have is, how does the "non-blocking" case work when the interface is still a synchronous call? Well, the answer is quite boring... You just call into the scheduler and it runs the other task inside the blocked operation.
I was promised hidden magic and implicit logic! Now I want a refund!
I don't think it's a huge problem, but it's more subtle than an explicit allocator is a better because it provides more flexibility.