Precompute File.DisplayPath

This is an optimization for dir lookups in torrentfs.
This commit is contained in:
Matt Joiner 2021-08-01 22:01:24 +10:00
parent 555cb064dd
commit e04c9a13f1
2 changed files with 14 additions and 14 deletions

11
file.go
View File

@ -1,8 +1,6 @@
package torrent package torrent
import ( import (
"strings"
"github.com/anacrolix/missinggo/v2/bitmap" "github.com/anacrolix/missinggo/v2/bitmap"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
@ -15,6 +13,7 @@ type File struct {
offset int64 offset int64
length int64 length int64
fi metainfo.FileInfo fi metainfo.FileInfo
displayPath string
prio piecePriority prio piecePriority
} }
@ -89,13 +88,9 @@ func (f *File) bytesLeft() (left int64) {
} }
// The relative file path for a multi-file torrent, and the torrent name for a // The relative file path for a multi-file torrent, and the torrent name for a
// single-file torrent. // single-file torrent. Dir separators are '/'.
func (f *File) DisplayPath() string { func (f *File) DisplayPath() string {
fip := f.FileInfo().Path return f.displayPath
if len(fip) == 0 {
return f.t.info.Name
}
return strings.Join(fip, "/")
} }
// The download status of a piece that comprises part of a File. // The download status of a piece that comprises part of a File.

5
t.go
View File

@ -203,12 +203,17 @@ func (t *Torrent) initFiles() {
} else { } else {
path = fi.Path path = fi.Path
} }
dp := t.info.Name
if len(fi.Path) != 0 {
dp = strings.Join(fi.Path, "/")
}
*t.files = append(*t.files, &File{ *t.files = append(*t.files, &File{
t, t,
strings.Join(append([]string{t.info.Name}, path...), "/"), strings.Join(append([]string{t.info.Name}, path...), "/"),
offset, offset,
fi.Length, fi.Length,
fi, fi,
dp,
PiecePriorityNone, PiecePriorityNone,
}) })
offset += fi.Length offset += fi.Length