1
0
mirror of https://github.com/golang/go synced 2024-11-25 20:17:57 -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.
This commit is contained in:
aimuz 2024-09-25 14:14:56 +08:00
parent ad6ee21bbf
commit dc5f836cc7

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
}