summaryrefslogtreecommitdiff
path: root/algorithm.txt
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2011-09-09 23:21:47 -0700
committerMark Adler <madler@alumni.caltech.edu>2011-09-09 23:21:47 -0700
commit7c2a874e50b871d04fbd19501f7b42cff55e5abc (patch)
tree1879cd29182ababb17cde77cee5ce74505db4006 /algorithm.txt
parenta383133c4e7b93113cee912f213cf9502d785fa7 (diff)
downloadzlib-1.2.0.tar.gz
zlib-1.2.0.tar.bz2
zlib-1.2.0.zip
zlib 1.2.0v1.2.0
Diffstat (limited to 'algorithm.txt')
-rw-r--r--algorithm.txt34
1 files changed, 15 insertions, 19 deletions
diff --git a/algorithm.txt b/algorithm.txt
index cdc830b..f64f7c3 100644
--- a/algorithm.txt
+++ b/algorithm.txt
@@ -59,10 +59,10 @@ but saves time since there are both fewer insertions and fewer searches.
59 59
602.1 Introduction 602.1 Introduction
61 61
62The real question is, given a Huffman tree, how to decode fast. The most 62The key question is how to represent a Huffman code (or any prefix code) so
63important realization is that shorter codes are much more common than 63that you can decode fast. The most important characteristic is that shorter
64longer codes, so pay attention to decoding the short codes fast, and let 64codes are much more common than longer codes, so pay attention to decoding the
65the long codes take longer to decode. 65short codes fast, and let the long codes take longer to decode.
66 66
67inflate() sets up a first level table that covers some number of bits of 67inflate() sets up a first level table that covers some number of bits of
68input less than the length of longest code. It gets that many bits from the 68input less than the length of longest code. It gets that many bits from the
@@ -77,20 +77,16 @@ table took no time (and if you had infinite memory), then there would only
77be a first level table to cover all the way to the longest code. However, 77be a first level table to cover all the way to the longest code. However,
78building the table ends up taking a lot longer for more bits since short 78building the table ends up taking a lot longer for more bits since short
79codes are replicated many times in such a table. What inflate() does is 79codes are replicated many times in such a table. What inflate() does is
80simply to make the number of bits in the first table a variable, and set it 80simply to make the number of bits in the first table a variable, and then
81for the maximum speed. 81to set that variable for the maximum speed.
82 82
83inflate() sends new trees relatively often, so it is possibly set for a 83For inflate, which has 286 possible codes for the literal/length tree, the size
84smaller first level table than an application that has only one tree for 84of the first table is nine bits. Also the distance trees have 30 possible
85all the data. For inflate, which has 286 possible codes for the 85values, and the size of the first table is six bits. Note that for each of
86literal/length tree, the size of the first table is nine bits. Also the 86those cases, the table ended up one bit longer than the ``average'' code
87distance trees have 30 possible values, and the size of the first table is 87length, i.e. the code length of an approximately flat code which would be a
88six bits. Note that for each of those cases, the table ended up one bit 88little more than eight bits for 286 symbols and a little less than five bits
89longer than the ``average'' code length, i.e. the code length of an 89for 30 symbols.
90approximately flat code which would be a little more than eight bits for
91286 symbols and a little less than five bits for 30 symbols. It would be
92interesting to see if optimizing the first level table for other
93applications gave values within a bit or two of the flat code size.
94 90
95 91
962.2 More details on the inflate table lookup 922.2 More details on the inflate table lookup
@@ -158,7 +154,7 @@ Let's make the first table three bits long (eight entries):
158110: -> table X (gobble 3 bits) 154110: -> table X (gobble 3 bits)
159111: -> table Y (gobble 3 bits) 155111: -> table Y (gobble 3 bits)
160 156
161Each entry is what the bits decode to and how many bits that is, i.e. how 157Each entry is what the bits decode as and how many bits that is, i.e. how
162many bits to gobble. Or the entry points to another table, with the number of 158many bits to gobble. Or the entry points to another table, with the number of
163bits to gobble implicit in the size of the table. 159bits to gobble implicit in the size of the table.
164 160