i love unions in C. they allow you to pack different types of data into the same memory address.

here's an example of an 64-bit string type that you can pass around as a native word on 64-bit systems. it can either be a binmoji or a 6-character null-terminated string.

this enabes the nostrdb metadata table to be super smol. caching reaction counts for any emoji or 6-length string can now be done inplace, only requiring two 64-bit integers worth of storage for each metadata entry, no string tables!

succinct, cache efficient data structures are my happy place

Reply to this note

Please Login to reply.

Discussion

Pretty cool!

Python sort of exposes these through their tuple structure?

python data types are pretty far detached from the hardware. they are all dynamic objects. I don't think python has memory unions.

I must have been thinking of Golang's structs w/ unsafe pointers.

Although Python 3 is getting closer:

https://docs.python.org/3/library/struct.html

Wait until you hear about tagged unions / algebraic data types

🎯

Yes!! One of the first C things I did was make a dll to allow me to do all kinds of unholy reinterpretations of the same data in c#. I can't for the life of me remember why though.

Probably something to do with wanting a single data column in a database that could be used by arbitrary types defined after the fact.

The terrible sins I have committed trying to make a relational database that could handle future types without creating ad hoc tables or columns every time a product was added or changed.

moar posts like these !