Back to News
Advertisement
Advertisement

⚡ Community Insights

Discussion Sentiment

50% Positive

Analyzed from 238 words in the discussion.

Trending Topics

#rust#zig#allocations#function#allocator#don#pass#via#find#usually

Discussion (5 Comments)Read Original on HackerNews

Georgelemental•about 2 hours ago
> In most languages (Java, Go, C#, Rust, Zig, OCaml, etc.) the process is reversed: you take a profiler to try and find allocations (usually in loops that happen millions of times). Then you go and eliminate or minimize the allocations.

This isn't fully correct. In Zig, the common pattern is that any function which allocates accepts an explicit allocator parameter; if you don't pass one explicitly, you don't get any heap allocations.

Rust doesn't make things quite that visible. But, if you restrict yourself to the standard library and crates designed with this in mind, allocations are usually not too hard to find. And you can always use `#![no_std]` without `alloc` if you want to be sure. Neither language is ever going to insert a heap allocation if it's not somewhere in the source.

scritty-dev•about 2 hours ago
could you clarify for me, because isn't that a different guarantee? from my understanding, Rust and Zig make allocations explicit, but [@zero_alloc] lets you declare "this function (and everything it calls) must not allocate" and have compiler enforce it.
eatonphil•21 minutes ago
They are indeed different. In Zig you might get the allocator via a struct field or just via importing the global allocator. The allocator via function parameter is only a convention and only even one convention.

Recent Rust also has the ability to pass allocators but that's the same thing. And even if you use no_std in Rust you might call into some other library's no_std function that allocates and you wouldn't be able to grep for that.

wmedrano•12 minutes ago
If you don't pass an allocator param in Zig, then the function basically can't allocate.
xg15•about 1 hour ago
Can we extend this to more properties, such as: Does IO, makes system calls, launches or interacts with threads, may block or has an unexpected space/time complexity?