Remix.run Logo
quotemstr 3 days ago

Zig articles tend to get a little too excited about rediscovering longstanding techniques.

The author has described a metaprogramming utility for allocating a contiguous hunk of memory, carving this hunk into fields (in the article's example, a fixed-sized Client header, then some number of bytes for host, then some number of bytes for read_buffer, and then some for write_buffer). I'll acknowledge the syntax is convenient, but

1. we've done this since time immemorial in C. See https://learn.microsoft.com/en-us/windows/win32/api/evntcons...

2. you can implement this pattern ergonomically in C++, and even moreso once the C++26 reflection stuff comes online

3. the zig implementation is inefficient. It desugars to

  const Connection = struct {
    ptr: [*]u8,
    lens: struct {
      host: usize,
      read_buffer: usize,
      write_buffer: usize,
    }
  }
That first pointer is needless indirection and probably a cache miss. You should (unless you have specific performance data showing otherwise) store the sizes in the object header, not in an obese pointer to it. (It's bigger than even a fat pointer.)
rvrb 3 days ago | parent | next [-]

You have raised a good discussion point or two, but I am not inclined to engage with them due to the tone you've created with the rest of your comment.

Would it be productive to jump into a thread on a Ruby article and puff your feathers about how you've always been able to do this in Perl, and also in Python 4 you can do XYZ? I don't think so.

For whatever reason, inevitably in threads on systems languages, someone comes in and postures themselves defensively like this. You might want to reflect on that.

3 days ago | parent | next [-]
[deleted]
quotemstr 3 days ago | parent | prev [-]

[flagged]

rvrb 3 days ago | parent [-]

I assure you, I am not the one who appears to be reacting in a hair trigger hypersensitive manner in this thread :)

AndyKelley 3 days ago | parent | prev [-]

I think you're misinterpreting OP's excitement here. The technique isn't novel but the ergonomics are.

> you can implement this pattern ergonomically in C++,

lmao

rvrb 3 days ago | parent | next [-]

> I think you're misinterpreting OP's excitement here. The technique isn't novel but the ergonomics are.

This, of course, but I also consciously made the decision to write for an audience less familiar with the language and concepts being discussed. That excitement can translate to engaging a less disgruntled and more curious reader than the OP.

pjmlp 2 days ago | parent | prev [-]

While C++ isn't without warts, and WG21 does indeed a good job adding a few more, many time people aren't able to do ergonomic stuff in C++, because they insist in using it like C with Classes.