From 953944e54ed8623d890a6736682901de6e93c2c7 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 31 Jul 2025 16:48:04 +0200 Subject: [PATCH] chore(fs): slightly reduce memory usage of IsParent (#10223) ### Purpose Small optimizations for IsParent and IsInternal, to avoid needless allocations. --- lib/fs/filesystem.go | 7 ++++--- lib/fs/util.go | 12 +++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/fs/filesystem.go b/lib/fs/filesystem.go index 368ba1ca5..3cafc8cfc 100644 --- a/lib/fs/filesystem.go +++ b/lib/fs/filesystem.go @@ -285,13 +285,14 @@ func NewFilesystem(fsType FilesystemType, uri string, opts ...Option) Filesystem return fs } +// fs cannot import config or versioner, so we hard code .stfolder +// (config.DefaultMarkerName) and .stversions (versioner.DefaultPath) +var internals = []string{".stfolder", ".stignore", ".stversions"} + // IsInternal returns true if the file, as a path relative to the folder // root, represents an internal file that should always be ignored. The file // path must be clean (i.e., in canonical shortest form). func IsInternal(file string) bool { - // fs cannot import config or versioner, so we hard code .stfolder - // (config.DefaultMarkerName) and .stversions (versioner.DefaultPath) - internals := []string{".stfolder", ".stignore", ".stversions"} for _, internal := range internals { if file == internal { return true diff --git a/lib/fs/util.go b/lib/fs/util.go index 94a0a5416..e4de09e45 100644 --- a/lib/fs/util.go +++ b/lib/fs/util.go @@ -169,10 +169,16 @@ func IsParent(path, parent string) bool { // not be caught below. return path != "/" } - if parent[len(parent)-1] != PathSeparator { - parent += pathSeparatorString + if parent[len(parent)-1] == PathSeparator { + return strings.HasPrefix(path, parent) } - return strings.HasPrefix(path, parent) + if !strings.HasPrefix(path, parent) { + return false + } + if len(path) <= len(parent) { + return false + } + return path[len(parent)] == PathSeparator } func CommonPrefix(first, second string) string {