Skip to content

Commit

Permalink
Reduce stack pressure due to array copies
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Jan 14, 2017
1 parent e3b7981 commit b5ed4dd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ It offers slightly better compression at lower compression settings, and up to 3
[![Build Status](https://travis-ci.org/klauspost/compress.svg?branch=master)](https://travis-ci.org/klauspost/compress)

# changelog
* Jan 14, 2017: Reduce stack pressure due to array copies. See [Issue #18625(https://github.com/golang/go/issues/18625).
* Oct 25, 2016: Level 2-4 have been rewritten and now offers significantly better performance than before.
* Oct 20, 2016: Port zlib changes from Go 1.7 to fix zlib writer issue. Please update.
* 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.
Expand Down
6 changes: 4 additions & 2 deletions flate/deflate.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ func (d *compressor) fillDeflate(b []byte) int {
delta := d.hashOffset - 1
d.hashOffset -= delta
d.chainHead -= delta
for i, v := range d.hashPrev {
// Iterate over slices instead of arrays to avoid copying
// the entire table onto the stack (Issue #18625).
for i, v := range d.hashPrev[:] {
if int(v) > delta {
d.hashPrev[i] = uint32(int(v) - delta)
} else {
d.hashPrev[i] = 0
}
}
for i, v := range d.hashHead {
for i, v := range d.hashHead[:] {
if int(v) > delta {
d.hashHead[i] = uint32(int(v) - delta)
} else {
Expand Down
6 changes: 3 additions & 3 deletions flate/snappy.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (e *snappyL2) Encode(dst *tokens, src []byte) {

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

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

// Protect against e.cur wraparound.
if e.cur > 1<<30 {
for i := range e.table {
for i := range e.table[:] {
e.table[i] = tableEntryPrev{}
}
e.snappyGen = snappyGen{cur: maxStoreBlockSize, prev: e.prev[:0]}
Expand Down

0 comments on commit b5ed4dd

Please sign in to comment.