It's all about the allocator

(aarol.dev)

60 points | by ibobev 5 days ago ago

18 comments

  • Panzerschrek 8 hours ago ago

    I don't think it's that necessary to return back to manual memory management (Zig-style) and abandoning proper RAII (Rust-style) to spare some memory. With a little bit effort it's possible to achieve good memory efficiency without sacrificing safety (by manually managing allocators).

    Chromium-based applications consume so much memory, not because they don't use manual memory management, but because JS used for application code isn't nearly as efficient in memory usage as any other proper language. Also the browser engine itself is optimized in such a way, that it sacrifices memory usage to gain more speed.

  • yeureka 20 hours ago ago

    Bloomberg writes all of its C++ using an allocator aware version of the STL because of this issue. It’s open source and very high quality: https://github.com/bloomberg/bde

    • ibobev 20 hours ago ago

      What do you mean by "allocator-aware version of the STL"? The standard STL is also allocator-aware, and its containers have versions with a template allocator parameter and a polymorphic allocator.

      • yeureka 19 hours ago ago

        That is correct. However at Bloomberg all functions that potentially allocate memory have to have an allocator passed as an argument or use a local allocator since the BDE requires allocator parameters to always be passed, there are no default allocator arguments like in the STL.

      • whorleater 18 hours ago ago

        https://bloomberg.github.io/bde/white_papers/unleashing-aa-s...

        lakos was one of the primary drivers of this

    • denverllc 17 hours ago ago

      Given how far allocators have come (like mimalloc or jemalloc) I would be interested to see if they make up some of the gap between the standard allocators (easy but not performant) and intrusive container allocators like in BDE

  • KolmogorovComp 16 hours ago ago

    This is not an issue with Rust the language but with the standard library which assumes allocation is not a big deal.

    Traditional where allocations matters, like for embedded devices you would use no-std.

    An alternative standard library with a different focus is entirely possible.

    Or as a middle ground a lint flagging allocations.

    As an aside I believe the last ‘expect’ issue to triggers an existing clippy lint.

  • krukah 17 hours ago ago

    It's where Zig outshines Rust the most, IMO.

    TBF Rust v1.100 makes a big leap in permitting custom allocators, but mandating would be ideal in the spirit of "explicit is better implicit". I like adding lints to defend against some of these footguns, but more compiler-driven transparency around quiet allocations would be so nice.

    • imtringued 5 hours ago ago

      Another thing is that passing in the IO backend to mark whether a function is pure or not is also kind of genius.

      This would unlock even more function reordering in Rust, but the ship has sailed on that one.

  • fwlr 20 hours ago ago

    Oh, wow, this is a fantastic explanation for why “Zig makes you pass the allocator around as an argument to all your functions” is A Good Thing, Actually. Kudos to the author!

    • mac3n 18 hours ago ago

      This argues for dynamically scoped variables - bindings that are scoped and inherited by all called functions without having to be passed explicitly. After all, why should everyone in the call chain need to know if some deep call allocates temporary storage.

      It's always bothered me that the only such feature in many languages is exception handlers (I'm also bothereed by functions that throw things).

      I want "late" bindings, implemented as "shallow" bindings. "Late" as in binding is established at runtime in the call chain. "Shallow" as in you don't need to crawl the call chain to find it. Something like

          extern Allocater *theAllocater = &DefaultAllocater;
          ...
          // allocater scope
          { Allocater *prev = theAllocater;
            theAllocater = NewAllocater();
            ...
            theAllocater->free();
            theAllocater = prev;
          }
      
      with a deep call using the current allocater

          // deep call
          { tmp = theAllocater->alloc(size);
            ...
          }
      • guipsp 18 hours ago ago
      • nagaiaida 13 hours ago ago

        yeah once you get used to reaching for dynamic scoping for thorny crosscutting concerns it's really rough when you don't have it. clojure/raku ruined me in that regard; some things simply don't need to get plumbed through every intervening layer explicitly.

        i have some generic command dispatch stuff that handles sudo/doas or other similar cli prefix wrapper utilities by just passing that info all the way down to the eventual external proc invocation as a dynamic variable (and actually it wouldn't even let me get away with sticking it all into the signatures because of potential argument conflicts in the general case). that way everything external this tool dispatches to can be defined as if run unprivileged for concision/flexibility but you still never need to invoke the tool itself with root privileges to sneak a sudo in for the eventual command because there's a mechanism for indicating desired privilege escalation that can quietly ripple out to everywhere it's needed without cluttering already complex signatures even more.

        personally i find dynamic scoping immensely useful specifically in conjunction with exception handling: it can help you turn resumable exceptions into exceptions that return values to the thrown context in just a couple lines for e.g. an incredibly lightweight effect handling implementation or whatever you wanna do with that.

        that said, without something readily visually distinguishing like raku's explicit twigils indicating a dynamic variable or something like clojure's conventional earmuffs on names to indicate the same, i do get why it's often seen as more trouble than it's worth by those less used to it.

    • pdlclay 20 hours ago ago

      [flagged]

  • ngriffiths 19 hours ago ago

    > So, if you want to write truly memory-efficient code, just asking AI to do it for you might not be enough.

    In hindsight, it would be pretty funny if "hey can you rewrite this game in Rust" is sufficient instructions to end up with memory-efficient code. In fact it would be pretty funny if it were enough to get this thing written in Rust, for all the specific details of what you actually mean when you say "this game"

  • daxfohl 18 hours ago ago

    I knew it's not the point of the article, but would AI be any better at translating code to zig? Or would it make the same slow design, just with all the allocations being explicit?

  • alfiedotwtf a day ago ago

    “It’s all about the Pentiums, baby”

  • ElUncivio 17 hours ago ago

    [dead]