From dc5f836cc76ea1355effa49a665b9091a888541b Mon Sep 17 00:00:00 2001 From: aimuz Date: Wed, 25 Sep 2024 14:14:56 +0800 Subject: [PATCH] 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. --- src/internal/zstd/zstd.go | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/src/internal/zstd/zstd.go b/src/internal/zstd/zstd.go index 0370f601cbf..d4eac399aff 100644 --- a/src/internal/zstd/zstd.go +++ b/src/internal/zstd/zstd.go @@ -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 }