Skip to content

Commit

Permalink
perf: Don't use pools for streams with one file (#194)
Browse files Browse the repository at this point in the history
Use the noop pool for single files.
  • Loading branch information
bodgit authored Apr 5, 2024
1 parent 37c55ec commit b4cfdcf
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ var (
errTooMuch = errors.New("sevenzip: too much data")
)

//nolint:gochecknoglobals
var newPool pool.Constructor = pool.NewPool

// A Reader serves content from a 7-Zip archive.
type Reader struct {
r io.ReaderAt
Expand Down Expand Up @@ -422,19 +419,14 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {

z.si = header.streamsInfo

z.pool = make([]pool.Pooler, z.si.Folders())
for i := range z.pool {
if z.pool[i], err = newPool(); err != nil {
return err
}
}

// spew.Dump(header)

folder, offset := 0, int64(0)
z.File = make([]*File, 0, len(header.filesInfo.file))
j := 0

filesPerStream := make(map[int]int, z.si.Folders())

for _, fh := range header.filesInfo.file {
f := new(File)
f.zip = z
Expand All @@ -450,6 +442,8 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
// Make an exported copy of the folder index
f.Stream = f.folder

filesPerStream[f.folder]++

if f.folder != folder {
offset = 0
}
Expand All @@ -463,6 +457,21 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
z.File = append(z.File, f)
}

// spew.Dump(filesPerStream)

z.pool = make([]pool.Pooler, z.si.Folders())
for i := range z.pool {
var newPool pool.Constructor = pool.NewNoopPool

if filesPerStream[i] > 1 {
newPool = pool.NewPool
}

if z.pool[i], err = newPool(); err != nil {
return err
}
}

return nil
}

Expand Down

0 comments on commit b4cfdcf

Please sign in to comment.