Skip to content Skip to footer
0 items - $0.00 0

Zig’s new LinkedList API (it’s time to learn fieldParentPtr) by todsacerdoti

Zig’s new LinkedList API (it’s time to learn fieldParentPtr) by todsacerdoti

13 Comments

  • Post Author
    roetlich
    Posted April 14, 2025 at 10:50 am

    This looks somewhat horrifying. How do I safely write a function that takes a linked list as a parameter?

  • Post Author
    spiffyk
    Posted April 14, 2025 at 11:14 am

    For better or worse, the Zig community has long been embracing the `@fieldParentPtr` builtin as the primary way to do things like OOP-ish inheritance, so this change feels pretty in-character for the language to me.

  • Post Author
    steventhedev
    Posted April 14, 2025 at 11:22 am

    This feels like a net negative result. It removes some of the complexity of using generics, but it couples between the data type and the collections it can be indexed by.

    What are the benefits of this approach? Is it limited to data alignment, or is it out of a greater desire to remove generics?

  • Post Author
    DeathArrow
    Posted April 14, 2025 at 11:48 am

    I wonder why should a language implement in its libraries a SingleLinkedList and a DoubleLinkedList.

    I do get why you need an array-like list, a dictionary/hashmap, a stack and a queue. I got the impression that linked lists aren't used very often. Or maybe it's a different case with Zig?

  • Post Author
    ralferoo
    Posted April 14, 2025 at 11:56 am

    I don't use Zig, but one advantage of having a genericised intrusive linked list where the next pointer doesn't have to be the first thing in the structure is when you want to use larger types, such as 128-bit fields. Sticking a pointer at the beginning would mean the compiler would have to insert alignment padding after the pointer or break the default alignment.

  • Post Author
    mastax
    Posted April 14, 2025 at 12:03 pm

    > The new version isn't generic. Rather, you embed the linked list node with your data. This is known as an intrusive linked list and tends to perform better and require fewer allocations.

    I don’t get this. In the generic version the data is embedded with the linked list node, so there’s only one allocation per node. ‘T’ isn’t a pointer (or doesn’t have to be) there’s no indirection between data and node.

  • Post Author
    lightingthedark
    Posted April 14, 2025 at 12:07 pm

    Can someone explain how the claim of higher performance works here? In C, which lacks generics, an intrusive list is preferred because otherwise you end up with each node having a void pointer to the data it holds. The previous Zig version was a generic, so the data type could easily be a value type. Given that the compiler is allowed to rearrange the layout of both structs unless data is packed or extern (in which case it almost certainly won't want to have a linked list node in the middle anyway) isn't the resulting type exactly the same in memory unless you intentionally made T a pointer type?

  • Post Author
    hoseja
    Posted April 14, 2025 at 12:13 pm

    Linked lists should be obscure niche data structures for when you absolutely need their unique characteristics, not some front-and-center default.

  • Post Author
    esjeon
    Posted April 14, 2025 at 12:24 pm

    Hmph, is there any rationale against a generic wrapper over it? Exposing the internal details can be irky in higher level application development. It would not hurt to have some inline functions.

  • Post Author
    MawKKe
    Posted April 14, 2025 at 12:29 pm

    Isn't the new one also horribly type-unsafe? Since you can put (parent) objects of any kind into the list without any mechanism to detect which is which?

  • Post Author
    mgaunard
    Posted April 14, 2025 at 12:40 pm

    There are several advantages to intrusive node-based data structures that I haven't seen stated in the article or the comments:

    – the same object can move between containers with no allocation and no need for a dedicated complex API

    – the same object can be part of multiple containers at once; particularly useful for intrusive binary trees, for indexing data with different criteria.

    – the container can be fully polymorphic, no need for all elements to be the same dynamic type.

    – no need for complex allocators, you can just store the objects as you see fit.

  • Post Author
    kajika91
    Posted April 14, 2025 at 1:46 pm

    Couldn't we have a function to return the data like this?

      pub const SinglyLinkedList(comptime T: type) type {
        return struct {
          first: ?*Node = null,
    
          pub const Node = struct {
            next: ?*Node = null,
          };
    
          const Self = @This(); 
          pub fn data(self: Self) *T {
            return @fieldParentPtr("node", self);
          }
      };

  • Post Author
    budmichstelk
    Posted April 14, 2025 at 2:30 pm

    [dead]

Leave a comment

In the Shadows of Innovation”

© 2025 HackTech.info. All Rights Reserved.

Sign Up to Our Newsletter

Be the first to know the latest updates

Whoops, you're not connected to Mailchimp. You need to enter a valid Mailchimp API key.