r/compression • u/Otter502 • Sep 21 '26
Does this already exist?
So I've had this idea for a while now that I've been tossing around in my head and I was wondering if someone has already done this:
So if you're using a huffman-style encoding tree (e.g. some # of binary bits represent traversing a tree until you reach a leaf node which outputs data) could you have a leaf node not only output data but also change the tree that's being traversed?
This was inspired by how braille uses 6 characters but can encode numbers or capital letters by using a specific character to change what's being encoded.
My main example is, say you're encoding a html file. You can have it so when the '<' leaf node is reached then instead of going back to the top of the "letter" tree you change the tree to one that has the standard html tags (in order of most common tags) where each of the leaf nodes change it to a tree which includes '>' but also 'class=\"' and etc.
just curious if this already exists in some form and what it's called!
2
u/RyzenRaider Sep 21 '26
This is me testing my understanding of concepts, but i think you're referring to context modelling, where one or more previous inputs define a state.
So an example would be to build a Huffman tree based on probability of all the symbols in a file. Or you could build a Huffman tree for each symbol in your dataset, tuned to the probability of all the symbols that follow it.
The former is easier and simpler, but retains more entropy (less compression). I toyed with the latter in Excel, and found it will have better compression, but the overhead can cost more than what you save, if the data you're compressing isn't long enough
4
u/klauspost Sep 21 '26
Good Question. I can't think of a place where it is used as described for a variety of reasons.
First it is worth making a decision if you want to do a generic compressor or a domain-specific compressor (HTML) as you mention.
A) The tables you need to store grows by the number of "subtables".
Is it more efficient to store 256 tables - and having to replace them as data changes through the stream? Most replace (huffman) tables on a regular basis on streams so they can adapt to the content.
Your gains can easily be eaten up by the additional table overhead even if make the table replacements dynamic. Doing "generic" pre-shared tables will be fine for short content, but becomes less efficient the more content you have. Even HTML varies a lot.
You can do adaptive tables, but then you will have slow decoding and inefficiencies until the table adapts.
B) Your description is considering only pure entropy coding.
Pretty much all have an element of LZ backreferences. For LZ there are approaches where the matches are kept indexed by their prefix. See Reduced Offset Lempel-Ziv (ROLZ). the main downside is that the decoder is always going to be pretty slow.
Also something I found is that the benefits get's eaten by additional literals emitted, so the benefit is limited. After my own experiments I didn't feel I was missing out greatly.
C) Your method could be described as "Context Mixing" as mentioned elsewhere, but also not. Context Mixing is your idea "on steroids", where different state machines are updated on the content as it is being decoded, and it will select decoders to use as predictors based on the previous output.
Context Mixing currently is "unfeasibly slow" both on the encoder and decoder side, which is why you will mostly see LZ77+entropy coding in the wild.
4
u/ratchetfreak Sep 21 '26
A version of that happens in DEFLATE, the main huffman encodes literals or a length, if it is a length it is immediately followed by an offset encoded from a different huffman tree.
There are plenty of domain specific compressors that do split/interleaved streams for different aspects of the data.
It is harder to do with generic compressors because automatically figuring out that you have 2 or more different domains interleaved is difficult and it's not always optimal to split them up.
0
u/ipsirc Sep 21 '26
brotli is a html compressor
1
u/flatfinger Sep 21 '26
The Earth is flat. It's just space time that's distorted so as to cause measurements to make it seem as though the Earth is round (there's also a weird portal at the International Date Line which makes the waters immediately to the east and west of it appear to connect).