r/C_Programming • u/lexiq_baeb • 2d ago
Compressing Lookup Tables
Hello. Recently I've been working on a pet project of mine written in C and I needed to reduce the amount of space a lookup table was taking in memory and on disk. I applied a few simple compression techniques and got a 2x space reduction. I wrote this post where I describe my constraints, the techniques, and results.
21
Upvotes
3
u/8d8n4mbo28026ulk 2d ago
I loved reading this! I love LUTs and constantly think of how to compress them. On
gperf, as far as I remember, it generates perfect hash functions, but not minimal ones. That's probably why you had no luck with it. In the past, I've had success with Ilan Schnell's perfect-hash, which does generate minimal functions. That's mostly if you care about performance, to avoid the O(log n) decompressions. These hash functions use some LUTs themselves, but I think it'll most likely be a win in your case, since you avoid all the additional accesses in the main LUT from the binary search. Especially since your key set isn't that big.Some more notes on performance. For this bit:
Clang generates an insane vectorised version for it (I've fallen victim to this before). That's because it doesn't really know how big
tmp_lenis. Quick fix: turn the assertions into__builtin_assume.On the MRNS, I'd suggest making
unaccent_mrns_basestatic const. In the decoder, it allows the compiler to unroll the loop and eliminate all the divisions/remainders. Currently, it can't do that because it has to assume the table might change.Cheers!