refactor: use strings.Builder to improve performance
Some checks failed
build / windows (push) Has been cancelled
build / other_os (push) Has been cancelled
build / mac_amd64 (push) Has been cancelled
build / mac_arm64 (push) Has been cancelled
build / linux (push) Has been cancelled
build / go1.24 (push) Has been cancelled
build / linux_386 (push) Has been cancelled
build / lint (push) Has been cancelled
build / android-all (push) Has been cancelled
Build & Push Docker Images / Build Docker Image for linux/386 (push) Has been cancelled
Build & Push Docker Images / Build Docker Image for linux/amd64 (push) Has been cancelled
Build & Push Docker Images / Build Docker Image for linux/arm/v6 (push) Has been cancelled
Build & Push Docker Images / Build Docker Image for linux/arm/v7 (push) Has been cancelled
Build & Push Docker Images / Build Docker Image for linux/arm64 (push) Has been cancelled
Build & Push Docker Images / Merge & Push Final Docker Image (push) Has been cancelled

This commit is contained in:
reddaisyy
2025-10-23 23:40:30 +08:00
committed by GitHub
parent 71631621c4
commit 1d0e1ea0b5
4 changed files with 21 additions and 18 deletions

View File

@@ -514,11 +514,12 @@ func (f *Fs) mkDirs(ctx context.Context, path string) (err error) {
if apiErr.ErrorName != "DiskPathPointsToExistentDirectoryError" { if apiErr.ErrorName != "DiskPathPointsToExistentDirectoryError" {
// 2 if it fails then create all directories in the path from root. // 2 if it fails then create all directories in the path from root.
dirs := strings.Split(dirString, "/") //path separator dirs := strings.Split(dirString, "/") //path separator
var mkdirpath = "/" //path separator / var mkdirpath strings.Builder
mkdirpath.WriteString("/") //path separator /
for _, element := range dirs { for _, element := range dirs {
if element != "" { if element != "" {
mkdirpath += element + "/" //path separator / mkdirpath.WriteString(element + "/") //path separator /
_ = f.CreateDir(ctx, mkdirpath) // ignore errors while creating dirs _ = f.CreateDir(ctx, mkdirpath.String()) // ignore errors while creating dirs
} }
} }
} }

View File

@@ -98,7 +98,7 @@ func (b *bisyncTest) generateDebuggers() {
} }
variations := []string{"LocalRemote", "RemoteLocal", "RemoteRemote"} variations := []string{"LocalRemote", "RemoteLocal", "RemoteRemote"}
debuggers := "" var debuggers strings.Builder
for _, backend := range config.Backends { for _, backend := range config.Backends {
if backend.Remote == "" { if backend.Remote == "" {
@@ -113,17 +113,17 @@ func (b *bisyncTest) generateDebuggers() {
name := fmt.Sprintf("Test %s %s %s", backend.Remote, testcase, variation) name := fmt.Sprintf("Test %s %s %s", backend.Remote, testcase, variation)
switch variation { switch variation {
case "LocalRemote": case "LocalRemote":
debuggers += fmt.Sprintf(debugFormat, name, "local", backend.Remote, testcase) debuggers.WriteString(fmt.Sprintf(debugFormat, name, "local", backend.Remote, testcase))
case "RemoteLocal": case "RemoteLocal":
debuggers += fmt.Sprintf(debugFormat, name, backend.Remote, "local", testcase) debuggers.WriteString(fmt.Sprintf(debugFormat, name, backend.Remote, "local", testcase))
case "RemoteRemote": case "RemoteRemote":
debuggers += fmt.Sprintf(debugFormat, name, backend.Remote, backend.Remote, testcase) debuggers.WriteString(fmt.Sprintf(debugFormat, name, backend.Remote, backend.Remote, testcase))
} }
} }
} }
} }
out := fmt.Sprintf(docFormat, debuggers) out := fmt.Sprintf(docFormat, debuggers.String())
outpath := "./testdata/bisync_vscode_debuggers_launch.json" outpath := "./testdata/bisync_vscode_debuggers_launch.json"
err = os.WriteFile(outpath, []byte(out), bilib.PermSecure) err = os.WriteFile(outpath, []byte(out), bilib.PermSecure)
assert.NoError(b.t, err, "writing golden file %s", outpath) assert.NoError(b.t, err, "writing golden file %s", outpath)

View File

@@ -4,6 +4,7 @@ package cryptdecode
import ( import (
"errors" "errors"
"fmt" "fmt"
"strings"
"github.com/rclone/rclone/backend/crypt" "github.com/rclone/rclone/backend/crypt"
"github.com/rclone/rclone/cmd" "github.com/rclone/rclone/cmd"
@@ -67,32 +68,32 @@ command. See the documentation on the [crypt](/crypt/) overlay for more info.`,
// cryptDecode returns the unencrypted file name // cryptDecode returns the unencrypted file name
func cryptDecode(cipher *crypt.Cipher, args []string) error { func cryptDecode(cipher *crypt.Cipher, args []string) error {
output := "" var output strings.Builder
for _, encryptedFileName := range args { for _, encryptedFileName := range args {
fileName, err := cipher.DecryptFileName(encryptedFileName) fileName, err := cipher.DecryptFileName(encryptedFileName)
if err != nil { if err != nil {
output += fmt.Sprintln(encryptedFileName, "\t", "Failed to decrypt") output.WriteString(fmt.Sprintln(encryptedFileName, "\t", "Failed to decrypt"))
} else { } else {
output += fmt.Sprintln(encryptedFileName, "\t", fileName) output.WriteString(fmt.Sprintln(encryptedFileName, "\t", fileName))
} }
} }
fmt.Print(output) fmt.Print(output.String())
return nil return nil
} }
// cryptEncode returns the encrypted file name // cryptEncode returns the encrypted file name
func cryptEncode(cipher *crypt.Cipher, args []string) error { func cryptEncode(cipher *crypt.Cipher, args []string) error {
output := "" var output strings.Builder
for _, fileName := range args { for _, fileName := range args {
encryptedFileName := cipher.EncryptFileName(fileName) encryptedFileName := cipher.EncryptFileName(fileName)
output += fmt.Sprintln(fileName, "\t", encryptedFileName) output.WriteString(fmt.Sprintln(fileName, "\t", encryptedFileName))
} }
fmt.Print(output) fmt.Print(output.String())
return nil return nil
} }

View File

@@ -3,6 +3,7 @@ package lsf
import ( import (
"bytes" "bytes"
"context" "context"
"strings"
"testing" "testing"
_ "github.com/rclone/rclone/backend/local" _ "github.com/rclone/rclone/backend/local"
@@ -140,12 +141,12 @@ file3
require.NoError(t, err) require.NoError(t, err)
items, _ := list.DirSorted(context.Background(), f, true, "") items, _ := list.DirSorted(context.Background(), f, true, "")
var expectedOutput string var expectedOutput strings.Builder
for _, item := range items { for _, item := range items {
expectedOutput += item.ModTime(context.Background()).Format("2006-01-02 15:04:05") + "\n" expectedOutput.WriteString(item.ModTime(context.Background()).Format("2006-01-02 15:04:05") + "\n")
} }
assert.Equal(t, expectedOutput, buf.String()) assert.Equal(t, expectedOutput.String(), buf.String())
buf = new(bytes.Buffer) buf = new(bytes.Buffer)
format = "sp" format = "sp"