Memoize the match table

This commit is contained in:
2025-03-28 11:31:27 +01:00
parent 8a86ae2f40
commit ebb07854cc

View File

@@ -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