Skip to main content

Module compression

Module compression 

Source
Expand description

§Versioned compression helpers

Every compressed payload produced here is self-describing: the first byte is a compression version that names the algorithm (0 = passthrough, 1 = lz4, 2 = brotli). This lets decompress dispatch on the byte alone, so callers only need to know which kind of compression to aim for, not which algorithm they’ll actually get back.

Two algorithms are exposed, picked by the caller based on expected access pattern:

  • compress_for_speedlz4-flex. For RPC wire traffic: the payload is compressed once per request and decompressed once per response. Fast, low CPU, modest ratio. Falls back to passthrough if the input is below MIN_BYTES_FOR_LZ4 (~64 B) or if the compressed form would be larger than the input.
  • compress_for_sizebrotli q11. For write-once / read-many post storage on servers: spend CPU once at write time in exchange for the smallest bytes-on-disk and bytes-on-wire when the post is later served. Same passthrough fallback at MIN_BYTES_FOR_BROTLI (~128 B).

§Defence against decompression bombs

Both decompress paths cap output at MAX_DECOMPRESSED_SIZE (32 MiB, with headroom over the protocol response limit) so a malicious tiny payload cannot OOM a peer.

Functions§

compress_for_size
Compress using brotli (small). Use for post storage (compress once, read many).
compress_for_speed
Compress using lz4 (fast). Use for RPC wire transfer.
decompress
Decompress any payload produced by compress_for_speed or compress_for_size.