]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Precompute File.DisplayPath
authorMatt Joiner <anacrolix@gmail.com>
Sun, 1 Aug 2021 12:01:24 +0000 (22:01 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 1 Aug 2021 12:01:24 +0000 (22:01 +1000)
This is an optimization for dir lookups in torrentfs.

file.go
t.go

diff --git a/file.go b/file.go
index 37c185c1aa72b7c7802b863347731f4de2200047..60317a23ce2adf9931a1608fefb7380578c982d2 100644 (file)
--- a/file.go
+++ b/file.go
@@ -1,8 +1,6 @@
 package torrent
 
 import (
-       "strings"
-
        "github.com/anacrolix/missinggo/v2/bitmap"
 
        "github.com/anacrolix/torrent/metainfo"
@@ -10,12 +8,13 @@ import (
 
 // Provides access to regions of torrent data that correspond to its files.
 type File struct {
-       t      *Torrent
-       path   string
-       offset int64
-       length int64
-       fi     metainfo.FileInfo
-       prio   piecePriority
+       t           *Torrent
+       path        string
+       offset      int64
+       length      int64
+       fi          metainfo.FileInfo
+       displayPath string
+       prio        piecePriority
 }
 
 func (f *File) Torrent() *Torrent {
@@ -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
-// single-file torrent.
+// single-file torrent. Dir separators are '/'.
 func (f *File) DisplayPath() string {
-       fip := f.FileInfo().Path
-       if len(fip) == 0 {
-               return f.t.info.Name
-       }
-       return strings.Join(fip, "/")
+       return f.displayPath
 }
 
 // The download status of a piece that comprises part of a File.
diff --git a/t.go b/t.go
index a3be2d266c3cff9a2922746edfa3a3765e43917b..a593a6bbf9a0c669945e111514860e9d89f4f857 100644 (file)
--- a/t.go
+++ b/t.go
@@ -203,12 +203,17 @@ func (t *Torrent) initFiles() {
                } else {
                        path = fi.Path
                }
+               dp := t.info.Name
+               if len(fi.Path) != 0 {
+                       dp = strings.Join(fi.Path, "/")
+               }
                *t.files = append(*t.files, &File{
                        t,
                        strings.Join(append([]string{t.info.Name}, path...), "/"),
                        offset,
                        fi.Length,
                        fi,
+                       dp,
                        PiecePriorityNone,
                })
                offset += fi.Length