Skip to content

Commit 46ca735

Browse files
committed
compress/flate: use built-in clear to simplify the code
The new bootstrap toolchain allows us to use the built-in clear. Updates #64751
1 parent 1b5ae45 commit 46ca735

File tree

3 files changed

+7
-21
lines changed

3 files changed

+7
-21
lines changed

src/compress/flate/deflate.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,8 @@ func (d *compressor) reset(w io.Writer) {
612612
d.bestSpeed.reset()
613613
default:
614614
d.chainHead = -1
615-
for i := range d.hashHead {
616-
d.hashHead[i] = 0
617-
}
618-
for i := range d.hashPrev {
619-
d.hashPrev[i] = 0
620-
}
615+
clear(d.hashHead[:])
616+
clear(d.hashPrev[:])
621617
d.hashOffset = 1
622618
d.index, d.windowEnd = 0, 0
623619
d.blockStart, d.byteAvailable = 0, false

src/compress/flate/deflatefast.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,7 @@ func (e *deflateFast) reset() {
286286
func (e *deflateFast) shiftOffsets() {
287287
if len(e.prev) == 0 {
288288
// We have no history; just clear the table.
289-
for i := range e.table[:] {
290-
e.table[i] = tableEntry{}
291-
}
289+
clear(e.table[:])
292290
e.cur = maxMatchOffset + 1
293291
return
294292
}

src/compress/flate/huffman_bit_writer.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ func (w *huffmanBitWriter) writeBytes(bytes []byte) {
198198
// numOffsets The number of offsets in offsetEncoding
199199
// litenc, offenc The literal and offset encoder to use
200200
func (w *huffmanBitWriter) generateCodegen(numLiterals int, numOffsets int, litEnc, offEnc *huffmanEncoder) {
201-
for i := range w.codegenFreq {
202-
w.codegenFreq[i] = 0
203-
}
201+
clear(w.codegenFreq[:])
204202
// Note that we are using codegen both as a temporary variable for holding
205203
// a copy of the frequencies, and as the place where we put the result.
206204
// This is fine because the output is always shorter than the input used
@@ -530,12 +528,8 @@ func (w *huffmanBitWriter) writeBlockDynamic(tokens []token, eof bool, input []b
530528
// and offsetEncoding.
531529
// The number of literal and offset tokens is returned.
532530
func (w *huffmanBitWriter) indexTokens(tokens []token) (numLiterals, numOffsets int) {
533-
for i := range w.literalFreq {
534-
w.literalFreq[i] = 0
535-
}
536-
for i := range w.offsetFreq {
537-
w.offsetFreq[i] = 0
538-
}
531+
clear(w.literalFreq)
532+
clear(w.offsetFreq)
539533

540534
for _, t := range tokens {
541535
if t < matchType {
@@ -621,9 +615,7 @@ func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte) {
621615
}
622616

623617
// Clear histogram
624-
for i := range w.literalFreq {
625-
w.literalFreq[i] = 0
626-
}
618+
clear(w.literalFreq)
627619

628620
// Add everything as literals
629621
histogram(input, w.literalFreq)

0 commit comments

Comments
 (0)