r/C_Programming • u/lexiq_baeb • Aug 31 '26
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.
22
Upvotes
3
u/8d8n4mbo28026ulk Aug 31 '26
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!
2
u/alex-van-02 Aug 31 '26 edited Aug 31 '26
Good post and the write-up. I did something similar for the Unicode upper/lowercase conversion tables few years ago.
From the first glance, the unaccent.rules table is even more sparse so it should compress even better. I have a feeling it should be possible to get it under 10K if not smaller. I'll try and sketch it out in the next few days, will be interesting to compare.
Edit: to add -
The size of unaccent.rules is 17241 bytes.
Each line is an UTF8 symbol, followed by \t, followed by a string, followed by \n. Some lines have no substitution sting, there are 106 of them. This gives us 2556 tab symbols.
Since UTF8 symbols have known byte count, we can discard \t and the data will remain unambiguous. This means that the raw
unaccentdata set is 14685 bytes. Clearly, it's not optimized for lookups, but still it's substantially smaller than 26624 bytes mentioned as a baseline in your post.1
u/dstroy0 Aug 31 '26
If the lookup table is finite and regular, you will be able to represent it with a smaller weighting table, like how we do for needle frequency in parsing, using a specific weight produces 100% correct matching far faster than unweighted lookup tables. This is definitely applicable to your existing work in the funnel before the sieve to reduce branching overall and get to your index identifier sooner, in your case even with diacritics you could match the same like ASCII char unaccented, then enable the hard case sensitive char sensitive branch after if they want to sift literal index matches to truthy ones.
1
u/kun1z Sep 01 '26
Can you benchmark these two possible solutions to see if there is a speedup?
And as you mentioned unaccent_code_find() can be replaced with a binary search:
1
u/ericonr Aug 31 '26
Fun! I will have to take a deeper look when I have the time. I've wanted to develop a diacritics removal plugin for Neovim for a while, I miss it all the time.