Zig: Pointer Stability for ArrayLists

(ziglang.org)

37 points | by tosh 3 hours ago

2 comments

  • amluto 9 minutes ago
    This seems weak.

    In a language like Rust, the compiler will “lock” the pointers for you, and you can’t forget.

    In a language like C++ (and presumably Zig), one could, in theory at least, have the iterators and slices that reference the storage of a dynamic array hold some sort of lock that pins the storage.

    But this API requires the programmer to remember to lock the pointers and also requires the programmer to keep the lock alive for the correct region of code. And it looks to me like even the example in the blog post has the lock taken completely outside the function that requires stability, so there is nothing whatsoever that gets the lock scoping right. Even the type system can’t help — the offending parse function can’t declare that it wants a pointer-locked ArrayList parameter.

  • _bohm 1 hour ago
    It's a nice feature but I can't help feeling like, if you need a stable pointer to an item in a collection, ArrayList is the wrong data structure to use? Maybe someone can chime in and give me an example of when you'd do this instead of, e.g., just storing an index. Alternatively, you could use an Unrolled Linked List (FKA SegmentedList in Zig before it was removed in 0.16, not sure why).
    • bvrmn 3 minutes ago
      ArrayList is a very generic (pun not intended) structure and could be stretched quite freely in any direction with useful property of owning underlying slice. Like readonly preallocated ArrayList is a thing.
    • the__alchemist 12 minutes ago
      My mental model of Zig is that it is explicitly the language for developers who prefer using pointers in business logic (instead of just in MMIMO, and are looking for something with improvements over C); i.e. exactly this class of abstraction.
    • nvme0n1p1 1 hour ago
      SegmentedList had a weird API, especially the way you control the list growth factor by the size of an inline array. And it hadn't kept up with stdlib norms in recent versions. I do hope it comes back eventually with an improved API.

      At least we got Deque in exchange. I use that far more often than I used SegmentedList.

      • _bohm 59 minutes ago
        Aha, gotcha. To tell you the truth, I don't think I ever used it. I have a custom implementation I wrote because I didn't realize at the time that SegmentedList was an unrolled linked list :p
    • beepbooptheory 21 minutes ago
      Maybe one use case is if you are interfacing with external C library and you're stuck with pointers?