Hacker News new | past | comments | ask | show | jobs | submit login

I had some fun recently using (abusing?) `constexpr` to process string literals at compile time to ‘compress’ then in the binary and save a few bytes in my microcontroller.

https://gist.github.com/th-in-gs/7f2104440aa02dd36264ed6bc38...

I’m just shaving some bits off - but I guess in principle you could do anything that’s `constexpr` evaluatable. Gzip compression of static buffers?…

Godbolt example:

https://godbolt.org/z/qc7jhKoGc




This is the intended purpose of the feature. gzip may be a little aggressive given that you have to unzip it again at the other end but its very possible (and potentially not even as expensive as one might expect given that these types of compression algorithms can be tuned on a speed/size tradeoff and backoff when struggling to compress).

https://github.com/PhilippeSigaud/Pegged is a D library that generates a parser generator for you based on a grammar (string) at compile time.


I think a lot hangs on what the data is ultimately for— if you have twenty compressed blobs and you only need one of them at a time, then it's perfect, or maybe if you have a single large blob that you just need targeted random access to.

But if you're going to uncompress it all right at startup, than it's not worth it at all— microcontroller flash is much cheaper and more plentiful than RAM.


The trick is to not even have strings in the binary. Take a look at trice, defmt and logging in ESP-IDF.


It's a little insane they don't have run length encoding for statically initialized data.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: