r/ProgrammingLanguages • u/ExplodingStrawHat • Apr 29 '26
Blessed Syntax and Ergonomics
https://www.gingerbill.org/article/2026/04/29/blessed-syntax-and-ergonomics/(not my article, but thought y'all might find it interesting)
33
Upvotes
View all comments
Show parent comment
3
u/WittyStick Apr 29 '26 edited Apr 30 '26
I use the "16-byte fat-pointer" a for almost everything in C, because it is optimized on x64 SYSV to get returned in 2 hardware registers, so we can avoid touching the stack in many places.
If the "auxiliary" information attached to the pointer is an
intptr_t, then it can be used for an integer or pointer.Anything where we can't fit it into 64-bits, we just allocate a "header" structure and store the pointer to that header in the auxiliary field. Eg:
Would permit gap buffers larger than 4GiB, but using two 32-bit integers in the
auxfield is fine too if this isn't an issue.For dynamic arrays, many implementations in C use something like:
But this sucks because it's 24 bytes (which gets put on the stack for both argument passing and returns). We could limit the array to 4GiB by using 2 32-bit ints, or use a pointer to a header like above - but there's another option. Most of these dynamic arrays work by doubling the capacity when it is full. If we're doing this, then storing the capacity is just wasted space - we can compute the capacity from the length:
cap = __builtin_stdc_bit_ceil(len). We get rid of thecapfrom the structure and now it fits nicely into our optimized 16-byte fat pointer.I use a bunch of other tricks to basically make most data types a fat pointer.