Four Months of Zig

(gracefulliberty.com)

55 points | by ibobev 3 hours ago

8 comments

  • i2talics 48 minutes ago
    Why does everyone love being able to control the allocator in Zig? I rarely want this low level control over the allocator, I would rather have an easy to use interface to malloc/free (such as RAII) or would prefer the language push me to avoiding allocations when unnecessary. C and Zig users are always talking about how arena allocators are easier to use than the alternative but I disagree because the alternative is extremely easy to use when the language makes it so.

    Arenas have a lot of footguns like growing out of control if you don't have space in your logic to delete them (e.g. suppose every new HTTP connection required obtaining a new allocation from the primary arena -- now you have a memory leak). They also aren't necessarily faster than malloc: I remember hearing a case where mimalloc was faster than an arena for a one-shot program like a linker because reclaiming space improved cache locality. Plus it's difficult to implement things like growable vectors or hashmaps using an arena without wasting space.

    I just don't see how "futz about with the allocator constantly" and "use an arena" is actually a good default.

    • the-tomster 23 minutes ago
      If you don't like arenas then that will push you towards avoiding allocations :)

      I don't love malloc / free, managing frees one-to-one with allocations is a nightmare. An HTTP server is actually a great example of a good fit for arenas... you can have an arena created per connection, and then free the arena when the connection closes. Now all of your allocations during that connection are guaranteed to be freed instead of having to keep track of them individually. Need something to live across connections? Use the general purpose allocator for those particular things.

      For one shot programs, why bother freeing anything at all? The gpa or malloc or whatever else is fine there.

      Personally I mostly lean on fixed-size buffers, but if I'm writing Zig it's just little toy programs where blowing up when the buffer runs out of room is fine. But for big non-toy programs it might be good to use fixed-size buffers anyway and define your limits in terms of things actually relevant to your domain.

      RAII and smart pointers are fine too, but C++ has you covered if you want those. Zig is specifically avoiding that kind of invisible code that destructors bring you.

      • i2talics 13 minutes ago
        > If you don't like arenas then that will push you towards avoiding allocations

        I mean push in the sense of a pit-of-success, not making me do things because the alternative is unpalatable.

        > managing frees one-to-one with allocations is a nightmare

        It really isn't a nightmare if your language has tools to help you. I'm never particularly concerned about frees in Rust outside of thinking about room to optimize.

        > Need something to live across connections? Use the general purpose allocator for those particular things.

        Well, this is a determination you have to make very carefully and if you screw it up (like in the situation I was trying to outline) you have a subtle bug.

        > Personally I mostly lean on fixed-size buffers, but if I'm writing Zig it's just little toy programs where blowing up when the buffer runs out of room is fine.

        What's also fine is using malloc, which will have perfectly reasonable performance for your toy programs.

        This is reinforcing my belief that Zig programmers actually just have a sort of aesthetic attachment to the idea of managing their allocations themselves and this has very little to do with the actual practical concerns of writing software that works. I'm a lot more sympathetic to the other reply that talked about library code improving flexibility for those relying on the library, than this comment that only thinks about the case where everything goes perfectly and using a fixed size buffer for no reason.

    • awesan 43 minutes ago
      It is a good default for lib code because it makes that code truly re-usable. The same is true for great C libraries, they let you provide a block of memory instead of calling malloc inside. The library should not get to decide if/when allocations should be done.

      In Zig app code you don't have to pass allocators all over if you don't want to, that's your code so do what you want. But for libraries it's the right thing to do because it puts the caller in control.

      The reason you have to pass allocators instead of making this implicit like in e.g. Odin, is because Zig just takes a firm stance against implicit logic, firmer than any other language I've ever used. The end result is that people who like Zig tend to like passing allocators around even in their own code.

    • elendilm 27 minutes ago
      mimalloc aggressively reclaims space and it benefits our usecase in our Dip database built on rust.

      This is for a long running process where consuming compute and memory should only occur on processing a request and must revert to a baseline memory consumption once the request is completed.

      malloc with default allocator always gradually increases the memory consumption. This continues with enough request bursts on our testing until the process runs out of memory. mimalloc doesn't.

      On mobile, we are stuck with the default allocator. On mobiles, the phenomenon of the default allocator consuming memory is reproducible even with normal Qt apps. It is what it is.

  • 22c 35 minutes ago
    First I'm hearing of TigerStyle.

    > The time to solve performance, and get the 1000x wins, is in the design phase, when you can't profile.

    How does this make practical sense? I find it immensely easier to fix performance issues by profiling, benchmarking, finding hotspots. Things that weren't obviously performance bottlenecks during design become obvious when you profile.

    Interesting write-up though. I haven't properly tried zig but I like reading these "initial impressions" style articles about it.

    • chrisco255 27 minutes ago
      If you understand how the CPU, memory, network, and disk are processing your program and its structures, you can design your programs to optimize for the hardware.

      Full guide for ref: https://github.com/tigerbeetle/tigerbeetle/blob/main/docs/TI...

      • austin-cheney 0 minutes ago
        This is also true for garbage collected languages, JavaScript in my case, but it requires a custom profiler and a bit more digging while asking better questions.
      • elendilm 2 minutes ago
        True. The guide is an interesting read. When you are deep in the trenches, most of what the github link says is just common sense.

        Eg: < Optimize for the slowest resources first (network, disk, memory, CPU) in that order, after compensating for the frequency of usage, because faster resources may be used many times more. For example, a memory cache miss may be as expensive as a disk fsync, if it happens many times more.>

        This is just obvious common sense. Great read.

  • alexboehm 32 minutes ago
    I agree with anyzig, it's amazing.

    I made a PR to add a default zig, which was rejected, I've been using it ever since though.

    https://github.com/marler8997/anyzig/pull/71

  • michidk 1 hour ago
    Pretty nice! I wrote a similar piece two years ago: https://blog.lohr.dev/after-a-day-of-programming-in-zig Great to see things are moving in the right direction.
  • Atlas48 1 hour ago
    I can't wait until Zig is a little more stable, I'd love to get into it more because the metaprogramming seems like such a draw.
  • paaloeye 2 hours ago
    Does ReTangled have anything to do with https://tangled.org?
    • contact9879 57 minutes ago
      it’s hyperlinked to the repo where you can see it has nothing to do with tangled
      • paaloeye 44 minutes ago
        I know but the name is so sus
  • LoganDark 2 hours ago
    > In Rust you can use ?, which is quicker than unwrap, but you also have to do something like Result<Type, Box<dyn std::error::Error>

    No, you don't! When returning Result<T, E>, you can use ? on any Result<T2, E2> where E: From<E2>. In other words E doesn't have to be Box<dyn>, it just has to satisfy From<E2>.

    Crates like thiserror make it easy to define your own error types that can be easily converted from all the other error types you can encounter in your functions! I do this whenever it doesn't make sense to use an inner error type directly, or whenever a function does its own validation that deserves its own error variant.

    On top of that, I tend to also have error variants for each place in the function rather than only for each type of inner error. At error sites I do something like `.map_err(MyError::PlaceInTheFunction)?` rather than only `?`, and then errors become like a very efficient backtrace :)

    • Graziano_M 2 hours ago
      I think you're proving the author's point.
      • GreenDolphinSys 1 hour ago
        They're saying that you don't always have to return Result<T, Box<dyn Error>>, you can implement the From<Error> trait on the type and use ? as usual.

        In other words implementing that trait lets you set a return type like Result<T, CustomAppError> use ? on it.

        • LoganDark 1 hour ago
          I think they're saying the author's point is that it's more fiddly in Rust (even if there are strong, well-regarded fiddles to use)
          • GreenDolphinSys 1 hour ago
            I don't think errors are fiddly in rust. They're easy to compose.
          • Graziano_M 1 hour ago
            Yes, and your other part about the crate.

            > There's also Zig-style composable errors with yet another library.

            • LoganDark 1 hour ago
              Ah, it's behind a link!

              While I'd push back against implying From<E2> is part of the crate, thiserror is an idiomatic approach these days, so I can understand the confusion. I can totally see how they could come to favor Zig if this behavior is "the default", as opposed to Rust where you'd usually see it done with "yet another library".

              And so I see "the author's point" has another meaning now: that Zig-style errors are good :)

              Edit: oh, after looking into Zig-style errors more, this is actually not how they work! They don't nest, they just exist in a global namespace, so they don't come with any location data themselves, but you can compose sets of them. Less powerful, but certainly more appealing to some :) I think out-parameters can be used as a side channel for error context.

              • Graziano_M 1 hour ago
                Yeah I 1. wish they were properly composable and 2. think Rust would get less flak if these 'idiomatic' crates were just pulled into the standard library to avoid some of the dependency mayhem.
      • tcfhgj 1 hour ago
        how so?
  • paaloeye 3 hours ago
    $curl: (6) Could not resolve host: gracefulliberty.com HN effect?