1
0
mirror of https://github.com/golang/go synced 2024-11-25 19:57:56 -07:00

internal/zstd: optimize skipFrame by using io.CopyN

Replaced the manual byte skipping logic with io.CopyN
to improve performance and reduce memory allocation.
This change simplifies the code by directly discarding
the bytes read, enhancing readability and efficiency.

Change-Id: Id11496d072fb554c394947d08e63616ca48ecab4
GitHub-Last-Rev: dc5f836cc7
GitHub-Pull-Request: golang/go#69619
Reviewed-on: https://go-review.googlesource.com/c/go/+/615716
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
aimuz 2024-09-25 14:25:58 +00:00 committed by Gopher Robot
parent ba42120723
commit ca1123f9c5

View File

@ -358,30 +358,12 @@ func (r *Reader) skipFrame() error {
return nil
}
var skip []byte
const chunk = 1 << 20 // 1M
for size >= chunk {
if len(skip) == 0 {
skip = make([]byte, chunk)
}
if _, err := io.ReadFull(r.r, skip); err != nil {
return r.wrapNonEOFError(relativeOffset, err)
}
relativeOffset += chunk
size -= chunk
n, err := io.CopyN(io.Discard, r.r, int64(size))
relativeOffset += int(n)
if err != nil {
return r.wrapNonEOFError(relativeOffset, err)
}
if size > 0 {
if len(skip) == 0 {
skip = make([]byte, size)
}
if _, err := io.ReadFull(r.r, skip); err != nil {
return r.wrapNonEOFError(relativeOffset, err)
}
relativeOffset += int(size)
}
r.blockOffset += int64(relativeOffset)
return nil
}