소스 검색

chore(fs): slightly reduce memory usage of IsParent (#10223)

### Purpose

Small optimizations for IsParent and IsInternal, to avoid needless
allocations.
Daniil Gentili 2 달 전
부모
커밋
953944e54e
2개의 변경된 파일13개의 추가작업 그리고 6개의 파일을 삭제
  1. 4 3
      lib/fs/filesystem.go
  2. 9 3
      lib/fs/util.go

+ 4 - 3
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

+ 9 - 3
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 {