Skip to content

Commit b5ed4dd

Browse files
committed
Reduce stack pressure due to array copies
Ports golang/go#18625
1 parent e3b7981 commit b5ed4dd

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ It offers slightly better compression at lower compression settings, and up to 3
1212
[![Build Status](https://travis-ci.org/klauspost/compress.svg?branch=master)](https://travis-ci.org/klauspost/compress)
1313

1414
# changelog
15+
* Jan 14, 2017: Reduce stack pressure due to array copies. See [Issue #18625(https://github.com/golang/go/issues/18625).
1516
* Oct 25, 2016: Level 2-4 have been rewritten and now offers significantly better performance than before.
1617
* Oct 20, 2016: Port zlib changes from Go 1.7 to fix zlib writer issue. Please update.
1718
* Oct 16, 2016: Go 1.7 changes merged. Apples to apples this package is a few percent faster, but has a significantly better balance between speed and compression per level.

flate/deflate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,16 @@ func (d *compressor) fillDeflate(b []byte) int {
136136
delta := d.hashOffset - 1
137137
d.hashOffset -= delta
138138
d.chainHead -= delta
139-
for i, v := range d.hashPrev {
139+
// Iterate over slices instead of arrays to avoid copying
140+
// the entire table onto the stack (Issue #18625).
141+
for i, v := range d.hashPrev[:] {
140142
if int(v) > delta {
141143
d.hashPrev[i] = uint32(int(v) - delta)
142144
} else {
143145
d.hashPrev[i] = 0
144146
}
145147
}
146-
for i, v := range d.hashHead {
148+
for i, v := range d.hashHead[:] {
147149
if int(v) > delta {
148150
d.hashHead[i] = uint32(int(v) - delta)
149151
} else {

flate/snappy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func (e *snappyL2) Encode(dst *tokens, src []byte) {
255255

256256
// Protect against e.cur wraparound.
257257
if e.cur > 1<<30 {
258-
for i := range e.table {
258+
for i := range e.table[:] {
259259
e.table[i] = tableEntry{}
260260
}
261261
e.cur = maxStoreBlockSize
@@ -416,7 +416,7 @@ func (e *snappyL3) Encode(dst *tokens, src []byte) {
416416

417417
// Protect against e.cur wraparound.
418418
if e.cur > 1<<30 {
419-
for i := range e.table {
419+
for i := range e.table[:] {
420420
e.table[i] = tableEntryPrev{}
421421
}
422422
e.snappyGen = snappyGen{cur: maxStoreBlockSize, prev: e.prev[:0]}
@@ -625,7 +625,7 @@ func (e *snappyL4) Encode(dst *tokens, src []byte) {
625625

626626
// Protect against e.cur wraparound.
627627
if e.cur > 1<<30 {
628-
for i := range e.table {
628+
for i := range e.table[:] {
629629
e.table[i] = tableEntryPrev{}
630630
}
631631
e.snappyGen = snappyGen{cur: maxStoreBlockSize, prev: e.prev[:0]}

0 commit comments

Comments
 (0)