NHacker Next
  • new
  • past
  • show
  • ask
  • show
  • jobs
  • submit
▲The state of Rust allocators in 2026: stabilised (cetra3.github.io)
atombender 11 hours ago [-]
I use Jemalloc in a specific project because it keeps per-thread allocation counters, so I can both monitor how much memory a thread is actively using (and how much it has allocated and deallocated), and I can limit it.

This is an incredible feature, and it's a hard requirement for this particular project, and a good fit because the workloads map to threads. Manually managing such counters with the standard Rust allocator is not a practical option.

But one problem I see with the new Rust allocators is that it doesn't seem like there's any kind of thread-local allocator? If so, you have to pass an allocator through every nook and cranny of the system as opposed to just riding on the thread context.

Even if the allocator is available as a "global" thread-local variable, which might be an option, that's a lot of places that need to be updated with .new_in(). And you have to introduce some kind of linting rule that rejects any code that isn't using the custom allocator.

I don't suppose a solution to this problem exists or is planned?

elehack 10 hours ago [-]
I don't think it's possible to implement thread-local “global” allocators. Since you need to use the same allocator to allocate and deallocate memory, transferring such objects across threads could transfer them to a thread without the right allocator — any data allocated with that allocator would need to be !Send, but the type system doesn't allow types to vary like that based on what allocator is active.
reallyreason 10 hours ago [-]
Not sure I understand what you're looking for exactly, but couldn't you just wrap a thread-local allocator (the design you're talking about) in a global allocator by referencing a static thread-local in that static global allocator? Then you wouldn't have to .new_in(), but you would still have thread-local allocation.

I'm not saying this is a good idea, just that it seems like Rust does maybe accommodate what you're talking about already.

atombender 9 hours ago [-]
Sure, but that has the overhead of a thread-local variable, and requires using threads.

I guess where I'm getting as is that there are genuine use cases where you want to enforce an allocator throughout a subsystem without rewriting absolutely every line that can allocate. But Rust does not have any way to pass "implicit" context down the stack, from what I know.

With Zig, allocators are already explicit, but Rust didn't start out that way, and it doesn't really fit into how Rust code is written today.

reallyreason 9 hours ago [-]
Oh I see, the thing that would work for your use case is essentially a dynamically-scoped allocator variable that all the functions which use the global allocator today would use instead.

You kind of can do dynamically scoped variables (though the only way I know to do this itself uses a thread-local stack and doesn't play nice with async), but you wouldn't be able to rewrite e.g. Box::new to use it instead of the thing bound to the global allocator. You could, however, provide an implementation of a global allocator that resolves your dynamically-scoped allocator for each allocation.

Yeah you would pay for the overhead of a thread-local on every allocation. Rust nightly does at least support a faster version of TLS (#[thread_local]) instead of the thread_local! macro. Not ideal, but there would necessarily be _some_ overhead involved with any strategy for resolving a dynamically-bound Allocator impl at runtime instead of a static one.

ozgrakkurt 17 hours ago [-]
Good luck to all people who are going to try implementing allocators!

Last time I tried, even the standard containers were giving different layouts to allocate and deallocate.

Also as a bonus, you get to put another template parameter to all of your containers!

tialaramex 15 hours ago [-]
> Last time I tried, even the standard containers were giving different layouts to allocate and deallocate.

Do you have some concrete examples? I couldn't reproduce this but maybe I'm looking in the wrong places.

ozgrakkurt 14 hours ago [-]
It was in this code.

https://github.com/ozgrakkurt/s_alloc

Test were failing until adding this

https://github.com/ozgrakkurt/s_alloc/blob/42adbd31e9835cc17...

Also I remember finding that allocator trait explicitly mentions this after finding the error.

tialaramex 14 hours ago [-]
Ah I see, that makes sense.
lenkite 16 hours ago [-]
Yet, in the lang adoption game, many who were looking at Zig thanks to native support for allocators will now re-look at Rust to solve their problem.
hiccuphippo 14 hours ago [-]
And many who said they'd never use custom allocators when discussing using Zig will see how neat it is now that they can do it in Rust.
tialaramex 14 hours ago [-]
Maybe. I don't expect this feature to have wide appeal. It's not useless, there are cases where you want this and it's great that it stabilized, but I think they're not very common and so in most cases as with everything else you shouldn't do this until you've got a problem it can solve.
kibwen 11 hours ago [-]
People have been able to do this in Rust for longer than Zig has existed. What this new development brings is a standardized interface to streamline interoperability, not any new language capability. The bumpalo crate has been the standard bearer here for nearly a decade now, currently counting 778 million downloads on crates.io.
lenkite 9 hours ago [-]
Without standardized interfaces, custom allocators have limiteduse. bumpalo could only be used seamlessly with its own custom types (like bumpalo::collections::Vec or bumpalo::boxed::Box.

bumpalo::collections::Vec<'bump, T> is a completely distinct type from std::vec::Vec<T>. You cannot pass an arena-allocated Vec or String to third-party libraries that expect standard library types without copying or converting them to slices first

kibwen 9 hours ago [-]
> You cannot pass an arena-allocated Vec or String to third-party libraries that expect standard library types without copying or converting them to slices first

Slices are already the standard for interoperability between Rust libraries, and always have been. There may very well be libraries out there whose APIs expect you to pass Vecs into them, but that would be highly atypical.

lenkite 7 hours ago [-]
> There may very well be libraries out there whose APIs expect you to pass Vecs into them, but that would be highly atypical.

    arrow       Buffer::from_vec(Vec<T>)
    ndarray     Array::from_vec(Vec<T>)
    ndarray     Array::from_shape_vec(shape, Vec<T>)
    bytes       Bytes::from(Vec<u8>)
    reqwest     Body::from(Vec<u8>)
    serde_json  Value::Array(Vec<Value>)
 
Dozens of example of very popular rust libraries. Usage is regular, not atypical. The slice variants generally require allocating and copying data or produces a borrowed object. Ownership transfer without copying is a problem.

allocator-aware APIs can enable zero-copy ownership transfer across library boundaries, even when the memory was allocated using a custom allocator. This is wowza power.

tialaramex 5 hours ago [-]
That's six, which is hardly dozens. All of the examples you gave aren't allocator aware. They will reject a Vec<T,Goose> unless Goose is actually the Global allocator. Even if Goose is equivalent by Rust's allocator rules that's not good enough, the type matching wants an exact match not just allocator equivalence.

And this makes some sense because Vec<T,Global> has properties they may care about since they're the new owners of this Vec, they don't want a Vec<T,Goose> which might have very different ideas about allocation.

As your parent says, most APIs care about slices and thus do not communicate an allocator, so they're agnostic to allocation.

couchand 13 hours ago [-]
> Last time I tried

For the benefit of the reader, this was two years ago.

The OP references updates made in Rust 1.100, released three weeks ago.

tialaramex 12 hours ago [-]
I think what they're seeing is the deallocation layout does "fit" but it has a different size. Rust's design for Allocator says that's fine, but if your code needs to know the size to find an allocation that's an issue for you because Rust doesn't promise it has the same size.

Rust says allocation A will "fit" in allocation B if A's alignment is equal to B's and A's size is no larger than B's size.

stingraycharles 15 hours ago [-]
do those of us who come from a c++ background this all looks oddly familiar and equally painful
tialaramex 5 hours ago [-]
AIUI the problem in C++ was that until C++ 20 it didn't have a slice type and so you couldn't communicate that you aren't allocating or de-allocating anything and so you don't care about allocators. Which means if you use either Allocator mechanism in C++ every part of the software needs to care about allocators. Rust doesn't have that problem

Vec<u8,Goose> and Vec<u8,Global> both implement AsRef<[u8]> and AsMut<[u8]> which means code which doesn't want to grow or shrink the Vec needn't care about the Goose allocator or interact with it in any way.

In C++ today you can write this way using std::span<T> from C++ 20, and enough people write C++ 20 that it probably won't cause overwhelming anger from people you've shut out, but in C++ 17 it could not be expressed at all, so that means even today some places cannot use your code.

gwbas1c 11 hours ago [-]
> Instead, what if we knew that we would have a period of time in our code, where we're going to allocate a lot of stuff, do something with it, and then free everything? A good example of this is a web request, or a database query.

> A request comes in, fills in some data structures, and then when the request finishes, the memory isn't needed anymore so it's dropped. This sort of "burst" pattern of allocation, where you allocate a lot up front, and then tear things down all at once at the end, is quite common.

FYI: Generational garbage collectors, such as the one used in the dotnet framework since the beginning (~2002), are very good at handling this.

Basically, the 0 generation starts as a large chunk of memory, and before a slow "mark and sweep" the reachable objects that are in the 0 generation are copied to the 1 generation. This implicitly deallocates unreachable objects in the 0 generation.

(Then again, there are a lot of tradeoffs to using a garbage collected language vs. Rust. I find that a program in Rust, vs an equivalent in C#, has a significantly lower RAM footprint.)

steveklabnik 11 hours ago [-]
I wrote a blog post about this connection a while back: https://steveklabnik.com/writing/borrow-checking-escape-anal...
gwbas1c 7 hours ago [-]
In practice, "garbage collected" means a tracing garbage collector, as opposed to other automatic memory management systems. IE, When Objective C added automatic reference counting, they didn't call it "garbage collection" because you still had to manage cyclic loops; and because "garbage collection" was a deprecated option for an experimental tracing GC. They called it "automatic reference counting" because, in practice, garbage collection means a tracing garbage collector. (The best reference I can find on this is https://stackoverflow.com/a/6385904)

I also would never call Rust a garbage collected language. It's more like a language where the compiler enforces memory safety. The difference is that it's on the programmer (or AI) to write correct code; and correct code includes implicit deallocations; but you still need to manage cyclic loops in RC or ARC.

slopinthebag 20 hours ago [-]
at this point I don't see much argument for languages that make allocators explicit. the escape hatch is there in rust for the rare case when you need it, and you get safety and all of rust's other benefits now too. well done to the rust core team to get this stabilised.
tialaramex 17 hours ago [-]
The time-and-again application cited for the explicit allocators in the Handmade Community languages is the once-per-frame allocations inside a video game or similar software.

So there's probably value in having some Rust examples which use a trivial Allocator for Vecs and Boxes in such a loop. And then in looking at which other collection types are most often used in this context. String is the most obvious thing people might want. String is analogous to Java's StringBuilder or maybe to C++ std::string although neither is a perfect analogy, it's a Vec<u8> inside, but of course today that's Vec<u8,Global> and so to make it use Allocator that needs to be modified to Vec<u8,A: Allocator> instead and then all the related API changes made.

I think in practice the community and to some extent the Auteurs behind each of the popular Handmade languages lean too hard on their custom allocators and the equivalent Rust just wouldn't allocate in many cases. An Odin programmer will idiomatically cause the program to allocate a temporary growable array of strings with two strings in it where the Rust programmer would str::split_once to get both strings without allocating. The Odin allocation was "cheap" but the Rust didn't allocate.

ozgrakkurt 16 hours ago [-]
This is a bit of a misunderstanding. Using a bump allocator is also useful for avoiding things like SmallVec. It can also good for reducing page faults or using the CPU cache better.

It has a lot of applications but people just don't use it for those applications.

It is just difficult to understand why people use it unless you actually understand what effects it has. I think a lot of people are making the mistake of assuming they would have realized the need for it naturally, but it is pretty hard to just stumble into it as an idea.

I understood the importance because of writing storage engines in rust for some years where I had a lot of trouble avoiding memory fragmentation with global allocators and also having page faults destroying performance. Even then you don't immediately think "oh I should use a bump allocator", you would still need to understand how the design of that allocator effects the problems you have.

tialaramex 14 hours ago [-]
A SmallVec and the SSO types I've spent more time thinking about seem like poor use cases for an Allocator mechanism.

After some thought, the best use case I could come up with was as follows: a set of K growable arrays such that in total they have M entries but each might have N = 0..=M entries of its own and so with SmallVec that needs K*M space but with an Allocator you're sure it all fits in M space which may be a significant improvement.

Except I think the correct representation for that is a single Boxed array or similar and K slices of that Boxed array instead.

ozgrakkurt 12 hours ago [-]
The way I use it is to have a more long lived bump allocator and a "temporary" bump allotor which I regularly roll back to some earlier offset.

If a function doesn't know the size it needs for a vector for example. It will at least have an upper bound, then it can allocate with the upper bound on the temporary alloc and construct the thing. After it is constructed it gets moved to the more long lived allocator. So the program is really only accessing three sections of memory at a time including the stack. Allocation/deallocation are just integer increment/decrement. And I can put an upper bound on the amount of memory a section of the program will use.

This way you don't need the SSO types at all.

tialaramex 5 hours ago [-]
> This way you don't need the SSO types at all.

Where it's applicable SSO is much better than your strategy and yet much less intrusive and with better cache locality. Suppose we want to make a growable array of Hacker News usernames, the naive Vec<String> is 24 bytes per user plus a heap allocation for each username

But Vec<CompactString> is 24 bytes per user in total because of SSO.

You might be able to get close with a single Box<[u8]> full of bytes from usernames and then slices into those bytes as a Vec<&str> of only 16 bytes per user, but I suspect that Box is pretty big, if it's more than 8 bytes per user the total is larger and you've got much worse cache locality.

On the other hand if usernames are tiny (or if your text is say, individual English words) you might do better with Vec<ColdString> at only 8 bytes per user. ColdString has a much more extreme SSO than CompactString, for which it pays in CPU cycles, but if RAM is precious and your texts are often short that's a price worth paying.

ColdString is significantly smaller than string slice references popular in modern languages including C++ and Zig, the only competitor if you needed this feature is the raw char* slim pointer from C, which is the same size as a ColdString but less capable.

tliltocatl 20 hours ago [-]
When you need it, you need it for the whole program (including all the dependencies).
spacechild1 17 hours ago [-]
Not really. Case in point: (soft)realtime audio programming. If you need to allocate memory in the audio callback (or DSP helper threads), you must use a real-time safe allocator (bounded worst case, no mutexes). For the rest of the program you may still use the default system allocator.
LoganDark 19 hours ago [-]
My toy kernel says otherwise. There are many stages of boot at varying levels of abstraction and being able to conveniently express each one is wonderful.

Also think of things like embedded scripting languages. Or having a special allocator for physics objects in a game as opposed to GUI objects. Yes, you can use a global allocator tuned to the workload. You can also tune allocators for each one. You don't have to, but you can.

logicchains 19 hours ago [-]
>My toy kernel says otherwise. There are many stages of boot at varying levels of abstraction and being able to conveniently express each one is wonderful.

You need tight allocation control and arena usage when squeezing the most performance possible out of your system is non-negotiable. Most people don't need to write such code.

eptcyka 16 hours ago [-]
Rust is kind of targeting those users. I want Rust to cater to this usecase. It is fine not to use custom allocators if you don’t want to.
tliltocatl 16 hours ago [-]
It's not only about performance. It is also about embedded system that don't have much memory, and have no fallback when they are out of memory. When ending up in some untested and faulty state you'd prefer getting some logs to just panicking and rebooting.

When the code uses dynamic allocation liberally, there is no such option: once a malloc fails you can only panic. You can reserve a piece of memory for recovery action, but it doesn't help much if recovery path engages most of the codebase. E. g. on IoT stuff most of the complexity is in the network stack - and you need the network stack if you want to get your logs.

And yes, recursion and stack is also a problem, even if more manageable than malloc. You can avoid recursion completely, but there still a risk of stack overflow on an untested code path. Zig promised staticaly-known stack depth once, but, afaik, didn't get there.

gf000 17 hours ago [-]
Even performance critical applications have non-performance critical parts.

E.g. in the aforementioned audio programming you may do the setup, init phase normally and only go for custom allocators afterwards.

wongarsu 17 hours ago [-]
Object pools are somewhat popular. Hugely popular in game development. And they are essentially a custom allocator for languages that don't have custom allocators
slopinthebag 20 hours ago [-]
rust has GlobalAlloc
tliltocatl 19 hours ago [-]
Which is useless if you don't want any dynamic allocation at all.
stymaar 19 hours ago [-]
If you don't want any allocation at all, you don't need an allocator API.

But the global allocator is the response to your previous comment:

> When you need it, you need it for the whole program (including all the dependencies).

chiffaa 19 hours ago [-]
Then you should generally go for `no_alloc`, because std allocates, so no_std + no_alloc leaves you with core + a bunch of crates for no_std data manipulation
LoganDark 19 hours ago [-]
Huh, that's a thing? I thought there was just `no_std` and then you can simply leave out `extern crate alloc`.
chiffaa 17 hours ago [-]
Frankly I'm not sure syntactically, didn't have to touch no-std in a bit, so I assume you're right. I do know that no_alloc is a crate tag people use and it's a readable enough way to phrase it
Ygg2 13 hours ago [-]
It's exactly as you describe. No_alloc is just no_std without using alloc crate.
tliltocatl 19 hours ago [-]
And then I have to deal with writing tons of code to please the borrow checker myself. At which point I might as well go for a language that doesn't need so much ritual.
chiffaa 17 hours ago [-]
...in what way is this related? Ownership is ownership, it's always a factor. Allocation is different because if you don't want any dynamic allocation, you don't want an allocator in the first place, ownership is an orthogonal concern
LoganDark 19 hours ago [-]
Why not use an LLM to help with the actual syntax while you only need to worry about the data flow? I've found they're quite good with that.
logicchains 19 hours ago [-]
It's not just syntax. Depending on the allocation scheme used it can be very difficult to prove to the borrow checker that it's all safe, or even impossible (the same way some data structures in Rust can only be implemented with `unsafe`).
ethin 7 hours ago [-]
Agreed, this is why I don't write kernels or things like that in Rust. The borrow checker will assume that your code is running in a parallel (multicore/multithreaded) capable environment, even if it isn't, and there is no way to explicitly tell the borrow checker to stfu, so you have to do a bunch of hack abstractions (Ref<T>, Rc<T>, ...) just to make it shut up.
surajrmal 12 hours ago [-]
If you stick to single ownership and stack tied or static lifetimes it's not too bad. If you're trying to pass parameters with lifetimes that extend beyond the stack frame and aren't static, then yes, that is painful. Callbacks or async style code in particular is challenging.
dnautics 19 hours ago [-]
Postgres has 11 allocators, erlang has at least 8. Databases and VMs are canonical use cases for systems PLs.
simonask 19 hours ago [-]
Allocators and things that look like allocators are pretty common in large projects. But allocators that can serve a standard container are significantly less common.

For example, you sometimes want a bump allocator (like the bumpalo crate). You almost never want to use it for a HashMap.

ozgrakkurt 16 hours ago [-]
You do always want to use for something like a hashmap. Why else would you want a custom allocator otherwise? Those things are what consume the majority of the memory in a program.

To give a concrete example, if you are using bump allocators for a web server to limit the amount of memory a request handler can use or to maximize CPU cache usage or avoid doings syscalls or mutexes while handling the request. Then you want the allocator to apply to everything that the request does. Otherwise there is no point in using it in the first place.

If you mean that it is difficult to use it for a hashmap. It is a bit weird to program in that way but it isn't really difficult if you understand it.

You may think "I'll have a special hashmap for the side of the program that uses the most memory". Then the problem here is that, if you are writing a large project in rust, you have to use the standard library and some dependencies. You can't write all that yourself because rust specifically makes writing that stuff very difficult.

If you are writing in C you can write a bump allocator in 10 minutes if you know how to do it. It is basically impossible to do that in rust.

tialaramex 15 hours ago [-]
> If you are writing in C you can write a bump allocator in 10 minutes if you know how to do it. It is basically impossible to do that in rust.

Your bump allocator is just a type with a "how much is left?" usize and a blob of reserved memory it owns, plus an implementation of the Allocator trait which has two (unsafe) functions.

The first one is easy, we're a bump allocator, deallocation is a no-op:

  deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
So that just leaves implementing

  allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { todo!() }
In our bump allocator that's just not very much work. Decide if there's enough space, if there isn't fail early, otherwise make a NonNull<[u8]> and decrease our "How much is left" counter.

Is it slightly more work than in C? Yeah, I guess so, but not much.

ozgrakkurt 15 hours ago [-]
You have to manage the lifetimes too, also you need to be able to set_offset() on bump allocator to use it effectively in my experience. You can't just increment/increment then drop it. Having a way of rolling it back to point x makes it a lot better.
simonask 9 hours ago [-]
The reason you really do not want a bump allocator for your hash map is that rehashing will reallocate everything, and there's no way to reclaim the freed memory blocks.

Know your algorithms and data structures.

dnautics 15 hours ago [-]
If youre writing in zig, literally any std datatype works with any custom allocator, and most 3p libraries will too, and if you go through std's allocator interface, you'll be able to do static analysis on that too!
15 hours ago [-]
dnautics 16 hours ago [-]
Idk. If i were writing a web server to service server generated content, id want to do a bump allocator per user request, and it's hard to imagine not needing hashmap for many such servers.
imtringued 16 hours ago [-]
Ambient authority was a mistake, for everything.
xiaogui917 16 hours ago [-]
The part I find most important here is the ecosystem effect: once Allocator is stable, libraries can be allocator-agnostic without splitting the ecosystem between nightly and stable crates. That should make it much easier for a database, game engine, or embedded project to plug in its own allocation strategy without forking every dependency. For most applications the default allocator will still be the right choice, but having a stable, shared interface is what turns custom allocation from a niche trick into ordinary infrastructure.
NooneAtAll5 19 hours ago [-]
so... what exactly was changed from straight-forward implementation of the feature that has been "unstable" for so many years?
theemathas 18 hours ago [-]
* Getting all the bajillion stakeholders to agree on what to stabilize. (I think this is the hardest part.)

* Fixing a pile of soundness bugs, (the fixes spawned a couple new still-unstable traits in the process: AllocatorClone and StaticAllocator).

* Specifying that panicking isn't allowed.

* Rewriting the safety docs of the Allocator trait to be more airtight.

the8472 16 hours ago [-]
Whether it should be dyn-safe or have associated types (mutually exclusive choice) was also a big point of contention.
YKupriyanov 15 hours ago [-]
[dead]
ReDress 19 hours ago [-]
[dead]
speedgoose 17 hours ago [-]
> It's going to take a bit of time for the ecosystem to catch up to custom allocators.

I havn't published any popular rust crate. I think my crates are downloaded by me and robots only, so I don't have a lot of authority. I have no wishes to think about custom memory allocators. I don't want them. I consider those as unnecessary complexity for a niche use case, and I'm a bit annoyed that they will pollute the Rust landscape. I understand why people need them though, but I fear that Rust is going to be the next C++: a bloated programming language with features to please everyone and a lack of simplicity. It's already complex enough.

koverstreet 16 hours ago [-]
This isn't something baked into the core language like C++ features tend to be. This is standardization of library code, thoroughly vetting what people already had to do but in incompatible ways.

This is the kind of work that is making Rust far easier and more productive for systems work, including even kernel development, than the bad old days of when every project was a monolith and everyone was reinventing the wheel.

Over the past several months I've been converting bcachefs to Rust, and as part of that I have a whole truckload of code that I can now release as small self contained crates that anyone can use: safe RCU, an async runtime suitable for kernel IO paths, a fully lockless implementation of Cuckoo - custom allocators getting standardized is directly pertinent because now all that code (RCU, especially) will compose much better with other people's crates.

GuB-42 11 hours ago [-]
> This isn't something baked into the core language like C++ features tend to be.

Not much of C++ is baked into the core language, most of it is just the standard library, it includes custom allocators. If you look at libboost, it has always been some kind of a trial ground for future C++ versions, with the best parts ending into the standard, and it is just a library.

Not so different from what is happening with Rust.

steveklabnik 11 hours ago [-]
C++ has new/delete as keywords, whereas allocation is purely a library concern in Rust.
spacechild1 14 hours ago [-]
> I consider those as unnecessary complexity for a niche use case

Custom allocators are certainly no niche use case, they are very common low level programming, from microcontrollers to audio software to video games.

pjmlp 16 hours ago [-]
As proven multiple times since JOVIAL, complex features are there for a reason.
speedgoose 16 hours ago [-]
It's an art to decide which use cases should be denied or not.
vlovich123 13 hours ago [-]
Sure, but “I don’t need it” isn’t the bar and features that aren’t paid for when not used are great since they stretch the language in an orthogonal direction that doesn’t represent any tradeoffs for anyone else
ozgrakkurt 17 hours ago [-]
It already is bloated.

I'm not saying it is a bad language as I use it since 9 years and still write new code in it.

But if you compare writing an bump allocator in c or zig vs rust, it is comically bloated. Same applies to anything like a vector or a hashmap or anything else low level.

You get a binary that is multiple megabytes if you just write a webserver on the path of least resistence.

chiffaa 16 hours ago [-]
May I ask if you're comparing apples to apples (statically linked everything) in the last case? I'm not very well versed in C or C++, but I do know that a common practice there is dynamically linking the world, so I'm curious if the size difference stays if you link statically in both cases?
atiedebee 8 hours ago [-]
Statically linking C code with glibc is more bloated than rust in my experience. However, using musl instead gives you binaries that easily fit in 10s of KB (compared to rust which I got to 300KB on stable without sacrificing the standard library)
ozgrakkurt 15 hours ago [-]
I write zig like C++ code where there is no headers (I use modules only) and I don't depend on libc or use any standard headers. The output binaries are extremely small if you also enable the linker garbage collection.

So everything is statically linked and only depends on linux syscall API. It is not apples to apples compared to rust but you can't practically do that in rust anyway.

dwattttt 9 hours ago [-]
What's stopping you? I do that on Windows; the trivial example of "add two numbers" is a 4kb DLL.

You don't use the standard library, the equivalent of the kernel syscall API is there (the win32 API), and you have the ecosystem of no_std libraries available.

koverstreet 16 hours ago [-]
the Rust version of bcachefs has an object size 50% bigger than the C version, without trying to optimize for object size and with a whole bunch of new code to replace things that we used to get from the kernel but weren't Rust.

Even if that doesn't shrink I consider that a very good tradeoff for what we're getting.

lenkite 16 hours ago [-]
Rust's async is 1000x more complicated than mere custom allocators, so the complexity argument is invalid. You are complaining about a mote of sand when there is a gigantic mammoth stomping on your head.
speedgoose 14 hours ago [-]
Whataboutism.
lenkite 13 hours ago [-]
It is indeed a mote of sand that will actually simplify Rust libs. Arena or bump allocators can tie memory lifetimes to a scope/region, transforming complex ownership and object-lifetime problems into simpler region-lifetime problems.
throwaway17_17 11 hours ago [-]
Can Rust’s borrow checker actually take notice of those transformations. I was not aware that using a bump allocator via the allocator API would allow for the static checks to reason about all intra-region allocations as a single entity.

If you know where there is any information on the borrow checker’s ability to check ‘under’ an allocator Id love to read about their strategy.

steveklabnik 11 hours ago [-]
Yes, this is already possible and done in crates like bumpalo.
Joker_vD 14 hours ago [-]
> I have no wishes to think about custom memory allocators. I don't want them. I consider those as unnecessary complexity for a niche use case,

Well, not to be rude, but Python and Go exist, you know. But if you're picking Rust (or Zig) — you will have to think about lifetimes, ownership, and allocations, that's the language's raison d'être.

> with features to please everyone and a lack of simplicity.

Like async! Compared to that barrel of worms, the new allocators are a complete non-issue. A two-functions trait, that Vec's and Box's "new_in" method use under the hood, with straightforward semantics — that's as complicated as having xmalloc()/xfree() in your C project.

speedgoose 14 hours ago [-]
That’s rude.
neumeisterfrank 16 hours ago [-]
[flagged]
Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact
Rendered at 03:03:18 GMT+0000 (Coordinated Universal Time) with Vercel.