threads / discuss / 5282

Re: Compression and dictionaries

Subject: Re: Compression and dictionaries

## tl;dr

10 messages between Aug 15, 2006 and Aug 17, 2006.

replies: 9people: 4as markdown or json

linux@horizon.com· Aug 15, 2006, 08:33 UTC · lore

Just to inform this discussion, a brief description of the zlib "deflate" algorithm. A full description is in RFC1951.

This is LZ77 compression, meaning that what is actually encoded is
a series of instruction to either
- Insert a specified byte, or
- Copy a run of n bytes, starting m bytes ago.
I.e. roughly
union {
	struct { bool is_literal, char literal};
	struct { bool is_literal; uint8_t length; uint16_t distance; } copy;
};

Note that 100 repetitions of a character can be unambiguously encoded as the character, followed by "copy 99 chars starting at offset -1".

There are also 257 possible literals, the 256 bytes plus a special EOF token.
The valid copy lengths are 3..258, and the valid distances are 1..32768.
Anyway, the deflate algorithm operates in a couple of steps:
- Translate the input into an (uncompressed) series of literals and
  copies.
- Take the literals and the lengths and put them into one big Huffman tree.
- Take the distances (actually, the simplified distances; large
  distances are grouped into ranges, which are followed by a binary suffix)
  and put them in a separate Huffman tree.
- A Huffman tree can be transmitted by encoding the number of bits assigned
  to each symbol, with a apecial value (infinity is mathematically
  the most logical but deflate calls it zero) for a symbol that does
  not appear in the tree at all.
- The way that deflate sends the lengths involves run-length encoding
  and a third Huffman tree, but I won't go into it now.

The finding of the copies is the most expensive part. The minimum copy length is 3 bytes, so the 3 bytes starting at each position are hashed and the result put in a hash table, then you search the hash chain looking for the longest match. There are various heuristics for aborting the search early, which the compression level adjusts. -1 doesn't look very hard at all, while -9 always does exhaustive search.

(For example, when searching at all aggressively, when you've found a match, you search starting at the next byte position in the input as well. If that produces a longer match, you emit the skipped byte as a literal and take the longer match and start looking for an improvement on it.)

Anyway, input is divided into blocks.  Each block is has a 2-bit
prefix that encodes one of three possibilities:
- Send uncompressed
- Send compressed, with fixed (specified in the standard) literal/length &
  distance Huffman code.  This "static Huffman tree" case is useful for
  short blocks where the overhead of encoding the Huffman tree is
  significant.
- Send compressed, using dynamic (encoded in the file) Huffman codes.
  This is the most commonly used case for large, compressible files.

Note that "copy" intructions can span blocks; the block boundaries are only opportunities to change the way that the copy instructions are coded if the file changes character (like text vs. data parts of an executable). Deciding when to end one block and start a new one is another set of heuristics.

Only thing like a "dictionary" is the codes used for Huffman compression.

Now, what you *could* do is pre-load the "copy from this text" buffer (hash table) with up to 32K of example data, before you start encoding data for real. Basically, run some example text through the compresser to warm it up, but throw away the compressed form.

This isn't a "dictionary" in the LZ78 sense (to start with, there are no boundaries between entries), but serves the same purpose. Note that since the distance to copy from is limited to 32768, there's no point to having more warm-up text than that.

Jon Smirl· Aug 15, 2006, 13:29 UTC · re: linux@horizon.com · lore
On 15 Aug 2006 04:33:03 -0400, linux@horizon.com <linux@horizon.com> wrote:
Show 7 quoted lines
> Just to inform this discussion, a brief description of the zlib
> "deflate" algorithm.  A full description is in RFC1951.
>
> This is LZ77 compression, meaning that what is actually encoded is
> a series of instruction to either
> - Insert a specified byte, or
> - Copy a run of n bytes, starting m bytes ago.

Shawn and I did some testing overnight. A simple 4KB preload table yielded a 4% size reduction in my 850MB pack. I'm still hoping for 10% using an optimal 32K table.

Paper on computing the optimal preload table: http://www.eecs.harvard.edu/~michaelm/postscripts/dcc2001a.pdf

If anyone is interested in doing some research, it would be interesting to build a pack engine based on full-text search technology. For example cLucene, http://clucene.sourceforge.net/index.php/Main_Page The full-text search app contains the code for extracting the dictionary and then encoding all of the blobs using it.

My prediction is that a large global dictionary based scheme will compress significantly better than zlib is capable of.

-- 
Jon Smirl
jonsmirl@gmail.com
linux@horizon.com· Aug 16, 2006, 00:37 UTC · re: Jon Smirl · lore
> I explained our situation to Mark Adler, zlib author, and he recommend
> checking out this book for techniques that can be used.
> 
> http://www.cs.mu.oz.au/mg/
I was about to suggest the same thing!  It's an excellent book.

It's about a piece of software and the choices made there, but it explains in detail many alternatives and why they weren't chosen for the mg software, but what they might be useful for.

The mg software itself is designed for human-language text, and does indexing as well. So it does a fixed breakdown into alternate word and non-word tokens, builds a lexicon with frequency tables, then uses the frequencies to build Huffman trees, and Huffman-compresses each token. The "word" dictionary is also used as part of the search index, as each word has a (very cleverly compressed to less than one byte on average) pointer to each document it appears in with a count of the number of appearances.

The word encoding is straight zero-order Huffman, so inter-word correlations are not used.

For software, with lots of punctuation and multi-word idioms ("for (i = 0; i < n; i++) {"), the basic design is not very well suited. (To say nothing of binary data, like some people who want to check images or multi-megabyte video files into git.)

But there is a good description of many possible algorithms, with lots of emphasis on practicalities.

And the software *does* support dynamic collections, via auxiliary indexes and escape codes for any new words not found in the main dictionary. In fact, generating the Huffman tree from as little as 1/8 of the material to be compressed only loses you 5.7% of the compression. (Table 9011, p. 360.)

However, that is for English text, with a pretty Zipf-like distribution. In code, we generate new words (new function names) frequently, and proceed to use them heavily.

It's worth noting the similarity between generating a good base dictionary with finding a good base version of a file for delta-encoding. You may end up having to divide the documents into classes (different source languages for example - C vs. asm vs. perl vs. python vs. docs vs. GIFs), and use a different dictionary per class. But that drives up the cost of compression (you have to see which class the file to be compressed falls into).

Anyway, highly recommended.
Jon Smirl· Aug 16, 2006, 05:50 UTC · lore
On 8/16/06, John Rigby <jcrigby@gmail.com> wrote:
> Sorry if this is off topic, but could the dictionary be used to make
> git-grep alot faster?
It would be almost instant.
Inverted full-text indices are what Google uses to grep 20B web pages in 1 sec.
-- 
Jon Smirl
jonsmirl@gmail.com
Johannes Schindelin· Aug 16, 2006, 06:33 UTC · re: Jon Smirl · lore
Hi,
On Wed, 16 Aug 2006, Jon Smirl wrote:
Show 5 quoted lines
> On 8/16/06, John Rigby <jcrigby@gmail.com> wrote:
> > Sorry if this is off topic, but could the dictionary be used to make
> > git-grep alot faster?
> 
> It would be almost instant.
But only if you are not using a regular expression, but a single word.

Ciao, Dscho

Shawn Pearce· Aug 16, 2006, 06:55 UTC · re: Johannes Schindelin · lore
Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
Show 11 quoted lines
> Hi,
> 
> On Wed, 16 Aug 2006, Jon Smirl wrote:
> 
> > On 8/16/06, John Rigby <jcrigby@gmail.com> wrote:
> > > Sorry if this is off topic, but could the dictionary be used to make
> > > git-grep alot faster?
> > 
> > It would be almost instant.
> 
> But only if you are not using a regular expression, but a single word.

Yes and no. If the inverted index contains terms broken by some known pattern (e.g. break on word-type boundaries) and the regex in question has constant sections (it should, otherwise it might as well just be '.') then you can reduce your search space to a fraction of the overall data by looking at the inverted index to select likely terms, select the related revisions containing those possible terms, then run the regex only on those revisions.

Sure you would be possibly pulling out a number of false positives but if the constant sequence(s) in the regex reduce your search space to below 1/2 of the overall data that's probably a lot less I/O and CPU required to complete the query, even if you have to read the entire dictionary and apply each term in the dictionary to the regex to look for those possible matches.

-- 
Shawn.
Johannes Schindelin· Aug 16, 2006, 07:09 UTC · re: Shawn Pearce · lore
Hi,
On Wed, 16 Aug 2006, Shawn Pearce wrote:
Show 27 quoted lines
> Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> > Hi,
> > 
> > On Wed, 16 Aug 2006, Jon Smirl wrote:
> > 
> > > On 8/16/06, John Rigby <jcrigby@gmail.com> wrote:
> > > > Sorry if this is off topic, but could the dictionary be used to make
> > > > git-grep alot faster?
> > > 
> > > It would be almost instant.
> > 
> > But only if you are not using a regular expression, but a single word.
> 
> Yes and no.  If the inverted index contains terms broken by some
> known pattern (e.g. break on word-type boundaries) and the regex
> in question has constant sections (it should, otherwise it might
> as well just be '.') then you can reduce your search space to a
> fraction of the overall data by looking at the inverted index to
> select likely terms, select the related revisions containing those
> possible terms, then run the regex only on those revisions.
> 
> Sure you would be possibly pulling out a number of false positives
> but if the constant sequence(s) in the regex reduce your search
> space to below 1/2 of the overall data that's probably a lot less
> I/O and CPU required to complete the query, even if you have to
> read the entire dictionary and apply each term in the dictionary
> to the regex to look for those possible matches.

So it would speed up the search, but no, in case of regular expressions, particularly any interesting one, the result would not be instantaneous.

Ciao, Dscho

Jon Smirl· Aug 16, 2006, 14:43 UTC · re: Johannes Schindelin · lore
On 8/16/06, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
Show 34 quoted lines
> Hi,
>
> On Wed, 16 Aug 2006, Shawn Pearce wrote:
>
> > Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> > > Hi,
> > >
> > > On Wed, 16 Aug 2006, Jon Smirl wrote:
> > >
> > > > On 8/16/06, John Rigby <jcrigby@gmail.com> wrote:
> > > > > Sorry if this is off topic, but could the dictionary be used to make
> > > > > git-grep alot faster?
> > > >
> > > > It would be almost instant.
> > >
> > > But only if you are not using a regular expression, but a single word.
> >
> > Yes and no.  If the inverted index contains terms broken by some
> > known pattern (e.g. break on word-type boundaries) and the regex
> > in question has constant sections (it should, otherwise it might
> > as well just be '.') then you can reduce your search space to a
> > fraction of the overall data by looking at the inverted index to
> > select likely terms, select the related revisions containing those
> > possible terms, then run the regex only on those revisions.
> >
> > Sure you would be possibly pulling out a number of false positives
> > but if the constant sequence(s) in the regex reduce your search
> > space to below 1/2 of the overall data that's probably a lot less
> > I/O and CPU required to complete the query, even if you have to
> > read the entire dictionary and apply each term in the dictionary
> > to the regex to look for those possible matches.
>
> So it would speed up the search, but no, in case of regular expressions,
> particularly any interesting one, the result would not be instantaneous.

Instant is a relative term. Google is instant compared to running grep over 10TB of data. How long would that take, a month?

Shawn is correct, the inverted indexes are used to eliminate as many files as possible. So the response time is a more of a function of how many hits you have instead of how big the data set is. Of course if you give it a pattern that matches everything it will just as slow as grep. Give it a pattern that is only in one file and detectable by the index and it will be very fast. If you are going to give it a bunch of patterns that aren't in the index, then we need to adjust how the index is built.

-- 
Jon Smirl
jonsmirl@gmail.com
linux@horizon.com· Aug 17, 2006, 22:33 UTC · re: Johannes Schindelin · lore

Just a note: there are index structures that support regular expression searching. In particular, a PAT tree, usually represented implicitly as a PAT array, can be walked by a finite automaton to find all the places it matches.

However, there's a lot of code complexity associated with that. And a PAT array assumes efficient random access to the text being indexed, as it does not keep a copy of the text.

Perhaps most importantly, this would be a big change to "git grep", as it would search every object in the database, not a particular commit. And mapping objects back to filenames in trees and commits requires another index.

Compression dictionaries and indexes have some opposing points. In a compression dictionary, you prefer common words that appear in many places. For an index, you prefer rare words that identify a small set of files.

← back to recent threads