diff options
| author | Ivan Krasin <krasin@golang.org> | 2012-01-23 09:26:14 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2012-01-23 09:26:14 -0500 |
| commit | b35cef670453a02c94ffb814729cf26ae7186a97 (patch) | |
| tree | 50d1b617d367eee77607b8d6a79bde037b18ec2e /src | |
| parent | d7e34051fcbd61a2f603b00f98d2bb3ca16c105d (diff) | |
| download | go-b35cef670453a02c94ffb814729cf26ae7186a97.tar.xz | |
compress/flate: use append instead of slice+counter.
R=rsc, nigeltao
CC=golang-dev
https://golang.org/cl/5561056
Diffstat (limited to 'src')
| -rw-r--r-- | src/pkg/compress/flate/deflate.go | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/src/pkg/compress/flate/deflate.go b/src/pkg/compress/flate/deflate.go index b69fa056e8..5745336cc8 100644 --- a/src/pkg/compress/flate/deflate.go +++ b/src/pkg/compress/flate/deflate.go @@ -82,9 +82,8 @@ type compressor struct { blockStart int // window index where current tokens start byteAvailable bool // if true, still need to process window[index-1]. - // queued output tokens: tokens[:ti] + // queued output tokens tokens []token - ti int // deflate state length int @@ -196,12 +195,11 @@ func (d *compressor) initDeflate() { d.hashPrev = make([]int, windowSize) d.window = make([]byte, 2*windowSize) d.hashOffset = 1 - d.tokens = make([]token, maxFlateBlockTokens, maxFlateBlockTokens+1) + d.tokens = make([]token, 0, maxFlateBlockTokens+1) d.length = minMatchLength - 1 d.offset = 0 d.byteAvailable = false d.index = 0 - d.ti = 0 d.hash = 0 d.chainHead = -1 } @@ -233,15 +231,14 @@ Loop: // Flush current output block if any. if d.byteAvailable { // There is still one pending token that needs to be flushed - d.tokens[d.ti] = literalToken(uint32(d.window[d.index-1])) - d.ti++ + d.tokens = append(d.tokens, literalToken(uint32(d.window[d.index-1]))) d.byteAvailable = false } - if d.ti > 0 { - if d.err = d.writeBlock(d.tokens[0:d.ti], d.index, false); d.err != nil { + if len(d.tokens) > 0 { + if d.err = d.writeBlock(d.tokens, d.index, false); d.err != nil { return } - d.ti = 0 + d.tokens = d.tokens[:0] } break Loop } @@ -275,11 +272,10 @@ Loop: // There was a match at the previous step, and the current match is // not better. Output the previous match. if d.fastSkipHashing != skipNever { - d.tokens[d.ti] = matchToken(uint32(d.length-minMatchLength), uint32(d.offset-minOffsetSize)) + d.tokens = append(d.tokens, matchToken(uint32(d.length-minMatchLength), uint32(d.offset-minOffsetSize))) } else { - d.tokens[d.ti] = matchToken(uint32(prevLength-minMatchLength), uint32(prevOffset-minOffsetSize)) + d.tokens = append(d.tokens, matchToken(uint32(prevLength-minMatchLength), uint32(prevOffset-minOffsetSize))) } - d.ti++ // Insert in the hash table all strings up to the end of the match. // index and index-1 are already inserted. If there is not enough // lookahead, the last two strings are not inserted into the hash @@ -313,12 +309,12 @@ Loop: d.hash = (int(d.window[d.index])<<hashShift + int(d.window[d.index+1])) } } - if d.ti == maxFlateBlockTokens { + if len(d.tokens) == maxFlateBlockTokens { // The block includes the current character if d.err = d.writeBlock(d.tokens, d.index, false); d.err != nil { return } - d.ti = 0 + d.tokens = d.tokens[:0] } } else { if d.fastSkipHashing != skipNever || d.byteAvailable { @@ -326,13 +322,12 @@ Loop: if d.fastSkipHashing != skipNever { i = d.index } - d.tokens[d.ti] = literalToken(uint32(d.window[i])) - d.ti++ - if d.ti == maxFlateBlockTokens { + d.tokens = append(d.tokens, literalToken(uint32(d.window[i]))) + if len(d.tokens) == maxFlateBlockTokens { if d.err = d.writeBlock(d.tokens, i+1, false); d.err != nil { return } - d.ti = 0 + d.tokens = d.tokens[:0] } } d.index++ |
