From: Olivier Mengué Date: Thu, 6 Mar 2025 18:44:22 +0000 (+0100) Subject: archive/zip: preallocate fileList size for Reader.Open X-Git-Tag: go1.25rc1~798 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=812a44ebd0a34ba2985f7f10c451fd210d5bab15;p=gostls13.git archive/zip: preallocate fileList size for Reader.Open When building the index of file entries for Reader.Open (when the Reader is used as an io/fs.FS), reduce reallocations by pre-allocating the count of entries based on the count of file entries. Change-Id: I05048337cb5e752054b3e984a8a5ec5199c4589b Reviewed-on: https://go-review.googlesource.com/c/go/+/655476 LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor Reviewed-by: Junyang Shao Auto-Submit: Ian Lance Taylor --- diff --git a/src/archive/zip/reader.go b/src/archive/zip/reader.go index 2246d56558..963526db11 100644 --- a/src/archive/zip/reader.go +++ b/src/archive/zip/reader.go @@ -804,6 +804,9 @@ func toValidName(name string) string { func (r *Reader) initFileList() { r.fileListOnce.Do(func() { + // Preallocate the minimum size of the index. + // We may also synthesize additional directory entries. + r.fileList = make([]fileListEntry, 0, len(r.File)) // files and knownDirs map from a file/directory name // to an index into the r.fileList entry that we are // building. They are used to mark duplicate entries.