logoalt Hacker News

ozgrakkurt • yesterday at 3:05 PM • 1 reply • view on HN

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.


Replies

tialaramex • yesterday at 10:18 PM

> 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.