Join the discussion

Write your take first — we'll ask for email only when you're ready to publish.

  • Hacker News
  • Object pooling is just malloc that is a little cheaper and maybe a different failure case when you are full...

    If you have a good method to handle the equivalent of OOM then they can make a difference in how the program runs but normally they are just a performance optimization.

    Honestly with 64 bit addresses it would be nice if address reuse were eliminated but that requires memory movement of a different kind (probably just as dangerous) or some terrible paging work for the OS...

  • I'm an amateur of C and knows nothing about Zig, so I'm not sure if this is equivalent to a C Union. But shouldn't we leave these kinds of problems to the programmers, not the language designers? Or did I miss anything?

    > We have a tagged union, which can hold either A or B. We initialize the union as A, take a pointer to its internals, overwrite the original with B, and then use the pointer. The pointer is still typed as A, but the bytes it points to now belong to B: a type confusion.

  • C's union (and the one in Rust or C++) is a kind of user defined type that's literally just either A or B. It does not know whether it's an A or a B, ensuring you achieve type safety (not using it as an A when it was actually a B or vice versa) is your job as programmer. It's size is thus MAX(size_of(A), size_of(B))

    [This is a big part of why writing to Rust's union is safe, storing either an A or a B is fine, there's no safety problem, only reading the union has potential issues and thus needs an unsafe super power]

    But your quote was about a tagged union, which is a common idea found in more modern languages and which you could implement by hand in C easily enough (though it is tedious to work with). The tagged union also has a field (we can think of it as an enumeration and I believe in Zig that's always exactly what it is) which says either A or B, so we can check that field and know if it's an A or a B. This type is slightly bigger, to make space for that enumeration field‡

    So in your quote the problem is that from a type safety POV it was crucial to set that field to B, not just write a B where the A was and hope.

    ‡ One of the important ideas in Rust is that we can avoid having this extra field in some cases yet deliver the same behaviour as if it existed - and that makes an important size / efficiency difference to our program, this is called "Niche optimization" and to some extent a C++ program could do it "by hand" using specialization and indeed a C programmer could write lots of horrible macros to enforce this style in their C, I would not recommend that.

  • I never fully understood how to work with this "reserved" values being valid instead of errors when you can't allocate more memory, in either case you still need to check if you have a real entity or a reserved/error, right? Does it really make things simpler?
  • In the reserved case you do still need to check if you have a reserved entity, but you can put it (along with the other allowed "pseudotypes") in a switch/case block and have it just break back out immediately (the no-op mentioned in the article) rather than having to use a separate if/else to check for Null, or clutter things up with a try/catch wrapper.

    Does that address what you're asking about?

  • It helps avoid the happy path effect. You're always handling something and there's no hidden control flow from the program runtime creeping in because of the cases you missed.
  • static allocation is de-facto standard in embedded for obvious reasons, and works really well there. in operating systems with more complex memory models designed entirely around dynamic workloads, im not sure asking devs to adopt another slightly complicated design pattern that imposes new hard caps is any less of a cognitive load than before.

    i can't bash the functionality and correctness aspect of static allocation, but it is akin to the humble linked list in the sense that you should already know going into the problem that you need it.

  • Someone recently made the point to me that a lot of dynamic situations can be rewritten as locally static allocations with proper continuations. The idea being that you re-enter the continuation with more memory when you've exhausted your existing pools. The problems are obvious, but it's a neat middle ground.
  • I’ve been surprised how feasible static allocation has been. I thought it would be too restrictive and you’d be constantly hitting a wall. I’m writing WebAssembly in Zig/Odin/C and I’ve been able to have a linter reject the `memory.grow` wasm instruction and just use fixed-sized buffers in my code.

    It me you think “ok, how big do I want the maximum image to be?” I’ve settled on 25 megapixels, which in the hundreds of megabytes. Since most images are much smaller, I believe on all mainstream hosts the memory isn’t paged in until it is first read/write so the memory footprint is much smaller.

  • I want to work more with systems that always abide by such strict constraints and style / design guides, but at the same time I feel like the reality of building software at scale is teams ending up working in subsystems that don’t consider the holistic operating model of the program. So even with best practices locally, the system as a whole ends up fragmented and inefficient, and strict global constraints therefore feel limiting.
  • Interesting how everyone keeps rediscovering 8 and 16 bit home computer programming techniques, after all these years, mostly I guess caused by the scripting languages for everything during the last two decades.
  • > TigerStyle: All memory must be statically allocated at startup. No memory may be dynamically allocated (or freed and reallocated) after initialization. This avoids unpredictable behavior that can significantly affect performance, and avoids use-after-free.

    Maybe maintaining an array of NULL-orders satisfies the letter of the "no dynamic allocation" law, but I'm not convinced it satisfies the spirit.

    Haven't you just written a buffer of NULL-orders, which you proceed to loan out to callers (i.e. "allocate" and "reallocate"?).

    Someone else's battle-hardened allocator might be slow or buggy, so you write your own as part of the business logic implementation?

  • >Someone else's battle-hardened allocator might be slow or buggy, so you write your own as part of the business logic implementation?

    In infrastructure where speed and reliability are highly valued? Absolutely. The gains obtained from proper memory layout and specialized use are massive. As long as you have the reason to do it, it's an easy win. I believe that the Zig standard library has different specialized allocators, so you don't even have to write your own buggy implementation.

  • It’s (mostly) not about performance, it’s about minimizing failure. Static memory allocation makes you OOM-proof.
  • The second half of the article talks about avoiding this, by not keeping any separate index of the (un)allocated orders. All the orders are allocated, and all are processed by the same pipeline, it's just that some of them are nearly no-ops. Each order contains its own no-op/some-op state marker, so it's hardened by being self-describing, with no other data structure that can disagree.

    Seems wasteful to spin through lots of no-op orders? Yes it is, but if it runs at all, you've (i) proved you can iterate through the whole array, so fewer surprises when the active order count grows; and (ii) given the cache an easy life by maximizing locality.

  • TigerStyle is strictly concerned about dynamic allocation from the perspective of the OS.

    Once you have that pool of "objects" that can be recycled throughout the lifetime of the program, you have a guarantee that actual allocation can only be interpreted in a specific way, i.e. all objects have the same size, alignment, etc so you don't have nearly the same level of concern or detail of implementation as an actual allocator in the common understanding of the word. A simple free-list gets you pretty far.