From ebb07854cc0b6a3e7adf3eae4eff6987a220ae70 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 28 Mar 2025 11:31:27 +0100 Subject: [PATCH] Memoize the match table --- utils/modifycommand.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/utils/modifycommand.go b/utils/modifycommand.go index f15056f..2ca9453 100644 --- a/utils/modifycommand.go +++ b/utils/modifycommand.go @@ -37,14 +37,29 @@ func (c *ModifyCommand) Validate() error { return nil } +// Ehh.. Not much better... Guess this wasn't the big deal +var matchesMemoTable map[string]bool = make(map[string]bool) +func Matches(path string, glob string) (bool, error) { + key := fmt.Sprintf("%s:%s", path, glob) + if matches, ok := matchesMemoTable[key]; ok { + logger.Debug("Found match for file %q and glob %q in memo table", path, glob) + return matches, nil + } + matches, err := doublestar.Match(glob, path) + if err != nil { + return false, fmt.Errorf("failed to match glob %s with file %s: %w", glob, path, err) + } + matchesMemoTable[key] = matches + return matches, nil +} + func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) { associationCount := 0 fileCommands := make(map[string][]ModifyCommand) for _, file := range files { for _, command := range commands { for _, glob := range command.Files { - // TODO: Maybe memoize this function call - matches, err := doublestar.Match(glob, file) + matches, err := Matches(file, glob) if err != nil { logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err) continue