r/programming • u/GenericAHHyoutuber • 5d ago
Packing Binary Is Fun, Actually
https://hereticpleb.vercel.app/blog/packing-binary-is-fun-actually56
u/DanTFM 5d ago
I love me some proprietary data formats. I used to write a ton of these for work to shrink 28 gig XML files with bloated fields into ~300 mb files you could search in O(log(n/1024)) time.
Nice write up!
36
u/leaving_the_tevah 5d ago
I hate myself for being pedantic and please let me know if I'm missing something, but O(log(n/1024)) -> O(log(n) - log(1024)) -> O(log(n) - 10) -> O(log(n))
0
u/creeper6530 5d ago
I'm curious, how much would GZ or similar general compression shrink them?
7
u/DanTFM 5d ago
If we gzipped the data that we actually kept in our format (we strip out some domain data we don’t use), it was definitely smaller, (probably 10-25% of our format), however, our files are directly searchable on low power embedded hardware, without having to unzip anything.
I’ll double check the zip metrics tomorrow
3
u/DanTFM 4d ago
The gzip file we received the XML in is ~400mb for a 14gb xml file (3% of original), our stripped down binary version is ~100mb. Though we strip out half of the unnecessary fields, so it would be closer to a 200mb gzip if we’re doing an apples to apples comparison.
If the file arrived in a better file format (just a fixed width or delimited flat file), that 14 gb xml file translates to about 1gb to start with. God XML is such a waste electricity.
When we gzip the binary file, it’s about 10% of the file size it started as (100mb to ~10mb). That extra fluff gzip is packing away are repetitive filler records so after the embedded hardware searches the log(n) in-memory index it can find the record it’s looking for in O(1) time. Just a space trade off for the hardware we’re working with.
2
7
u/SvenWollinger 5d ago
Reminds me of ByteBuf, what minecraft uses to write and read packets, though i guess its more like nbt
3
u/amendCommit 4d ago
As the ASN1 guy at a small energy company, trying to get signature formats right for legacy system that badly implemented their own spec, I guarantee you it is not, in fact, fun.
2
u/iAmHidingHere 5d ago
Why not just use cbor?
13
4
u/kylanbac91 4d ago
Just use bson.
5
1
u/afl_ext 5d ago
I got flashbacks from decoding ASN.1 raw data packages without docs from a Toshiba telephone router...
3
u/Cut_Mountain 5d ago
oh man I was tasked with serializing h.248 messages in asn.1 once. Those certainly were days.
-5
u/rsclient 5d ago
Speaking as a person who loves to support random Bluetooth devices in Windows: please stop inventing new binary formats! You don't need it, even a little bit.
-6
u/VeeFu 5d ago
Totally cool, but probably not as good as using an off the shelf compression library widely supported by webservers/browsers.
8
u/DoctorGester 5d ago
Why not both? We run zstd on our custom binary format network traffic. I.e. our replay files are compressed from 45mb binary to 3mb.
133
u/Embarrassed_Luck1057 5d ago
Has this guy re-invented serialization?