Memoize the glob statement

Because we were doing the same work many times :(
This commit is contained in:
2025-12-02 16:46:02 +01:00
parent eacc92ce4b
commit a18573c9f8
2 changed files with 63 additions and 4 deletions

View File

@@ -62,6 +62,7 @@ func (c *ModifyCommand) Validate() error {
// Ehh.. Not much better... Guess this wasn't the big deal
var matchesMemoTable map[string]bool = make(map[string]bool)
var globMemoTable map[string][]string = make(map[string][]string)
func Matches(path string, glob string) (bool, error) {
matchesLogger := modifyCommandLogger.WithPrefix("Matches").WithField("path", path).WithField("glob", glob)
@@ -225,10 +226,16 @@ func ExpandGlobs(patterns map[string]struct{}) ([]string, error) {
for pattern := range patterns {
expandGlobsLogger.Debug("Processing glob pattern: %q", pattern)
static, pattern := SplitPattern(pattern)
matches, err := doublestar.Glob(os.DirFS(static), pattern)
if err != nil {
expandGlobsLogger.Warning("Error expanding glob %q in %q: %v", pattern, static, err)
continue
key := static + "|" + pattern
matches, ok := globMemoTable[key]
if !ok {
var err error
matches, err = doublestar.Glob(os.DirFS(static), pattern)
if err != nil {
expandGlobsLogger.Warning("Error expanding glob %q in %q: %v", pattern, static, err)
continue
}
globMemoTable[key] = matches
}
expandGlobsLogger.Debug("Found %d matches for pattern %q", len(matches), pattern)
expandGlobsLogger.Trace("Raw matches for pattern %q: %v", pattern, matches)