Use bytes buffer to reduce allocs

This commit is contained in:
Ben Sarmiento
2023-12-10 21:24:58 +01:00
parent 9e181c2b19
commit 97161862ee

View File

@@ -1,6 +1,7 @@
package dav package dav
import ( import (
"bytes"
"fmt" "fmt"
"path/filepath" "path/filepath"
"sort" "sort"
@@ -32,8 +33,9 @@ func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *l
return nil, fmt.Errorf("cannot find directory %s", directory) return nil, fmt.Errorf("cannot find directory %s", directory)
} }
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" var buf bytes.Buffer
davDoc += dav.Directory("", "") buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.BaseDirectory(directory, ""))
var allTorrents []*torrent.Torrent var allTorrents []*torrent.Torrent
torrents.IterCb(func(_ string, tor *torrent.Torrent) { torrents.IterCb(func(_ string, tor *torrent.Torrent) {
if tor.AllInProgress() { if tor.AllInProgress() {
@@ -45,10 +47,10 @@ func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *l
return allTorrents[i].AccessKey < allTorrents[j].AccessKey return allTorrents[i].AccessKey < allTorrents[j].AccessKey
}) })
for _, tor := range allTorrents { for _, tor := range allTorrents {
davDoc += dav.Directory(tor.AccessKey, tor.LatestAdded) buf.WriteString(dav.Directory(tor.AccessKey, tor.LatestAdded))
} }
davDoc += "</d:multistatus>" buf.WriteString("</d:multistatus>")
return []byte(davDoc), nil return buf.Bytes(), nil
} }
func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) {
@@ -61,7 +63,9 @@ func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManag
return nil, fmt.Errorf("cannot find torrent %s", torrentName) return nil, fmt.Errorf("cannot find torrent %s", torrentName)
} }
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" + dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded) var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded))
filenames := tor.SelectedFiles.Keys() filenames := tor.SelectedFiles.Keys()
sort.Strings(filenames) sort.Strings(filenames)
for _, filename := range filenames { for _, filename := range filenames {
@@ -69,10 +73,10 @@ func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManag
if !ok || !strings.HasPrefix(file.Link, "http") { if !ok || !strings.HasPrefix(file.Link, "http") {
continue continue
} }
davDoc += dav.File(filename, file.Bytes, file.Ended) buf.WriteString(dav.File(filename, file.Bytes, file.Ended))
} }
davDoc += "</d:multistatus>" buf.WriteString("</d:multistatus>")
return []byte(davDoc), nil return buf.Bytes(), nil
} }
func HandlePropfindFile(directory, torrentName, fileName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { func HandlePropfindFile(directory, torrentName, fileName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) {
@@ -88,8 +92,11 @@ func HandlePropfindFile(directory, torrentName, fileName string, torMgr *torrent
if !ok || !strings.HasPrefix(file.Link, "http") { if !ok || !strings.HasPrefix(file.Link, "http") {
return nil, fmt.Errorf("cannot find file %s", fileName) return nil, fmt.Errorf("cannot find file %s", fileName)
} }
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" + dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded)
davDoc += dav.File(fileName, file.Bytes, file.Ended) var buf bytes.Buffer
davDoc += "</d:multistatus>" buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
return []byte(davDoc), nil buf.WriteString(dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded))
buf.WriteString(dav.File(fileName, file.Bytes, file.Ended))
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
} }