Skip to content

perf: pre-compile glob patterns in config filter matching #58

Description

@pageton

Summary

matchAnyName in generator.go:148-158 performs shell glob matching for every pattern on every interface/struct during config filtering:

matchAnyName := func(name string, patterns []any) bool {
    name = filePkgPath + "." + stripGeneric(name)
    for _, p := range patterns {
        if stripGeneric(fmt.Sprint(p)) == name {
            return true
        }
        if ok, _ := filepath.Match("*"+stripGeneric(fmt.Sprint(p)), filepath.Base(name)); ok {
            return true
        }
    }
    return false
}

Each call to filepath.Match parses the glob pattern from scratch. For 100 structs × 5 patterns, that's 500+ glob matches. fmt.Sprint(p) is also called twice per pattern iteration.

Proposed Fix

Pre-compile patterns once before the filtering loop:

type compiledPattern struct {
    exact string // for exact match
    glob  string // for filepath.Match
}

Compile all patterns once, then match against the compiled list. Also cache fmt.Sprint(p) results.

Impact

Minor — only noticeable in monorepos with many structs/interfaces and many filter patterns. The O(n × m) matching is fine for typical use (5–20 types, 1–3 patterns), but pre-compilation is the right pattern for correctness and makes the code cleaner.

Note

The outer loop at generator.go:117-135 does O(files × configFiles) prefix matching. For typical CLI usage this is negligible, but if the codebase grows to support large monorepos with many config files, consider sorting config paths and using binary search for prefix matching.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions