diff options
Diffstat (limited to 'hugofs')
-rw-r--r-- | hugofs/glob/glob.go | 34 | ||||
-rw-r--r-- | hugofs/glob/glob_test.go | 9 |
2 files changed, 29 insertions, 14 deletions
diff --git a/hugofs/glob/glob.go b/hugofs/glob/glob.go index 124a3d50e..88485e1f0 100644 --- a/hugofs/glob/glob.go +++ b/hugofs/glob/glob.go @@ -23,30 +23,36 @@ import ( "github.com/gobwas/glob/syntax" ) +type globErr struct { + glob glob.Glob + err error +} + var ( - globCache = make(map[string]glob.Glob) + globCache = make(map[string]globErr) globMu sync.RWMutex ) func GetGlob(pattern string) (glob.Glob, error) { - var g glob.Glob + var eg globErr globMu.RLock() - g, found := globCache[pattern] + var found bool + eg, found = globCache[pattern] globMu.RUnlock() - if !found { - var err error - g, err = glob.Compile(strings.ToLower(pattern), '/') - if err != nil { - return nil, err - } - - globMu.Lock() - globCache[pattern] = g - globMu.Unlock() + if found { + return eg.glob, eg.err } - return g, nil + var err error + g, err := glob.Compile(strings.ToLower(pattern), '/') + eg = globErr{g, err} + + globMu.Lock() + globCache[pattern] = eg + globMu.Unlock() + + return eg.glob, eg.err } diff --git a/hugofs/glob/glob_test.go b/hugofs/glob/glob_test.go index cca8e4e0f..5a6ef5b7e 100644 --- a/hugofs/glob/glob_test.go +++ b/hugofs/glob/glob_test.go @@ -73,5 +73,14 @@ func TestGetGlob(t *testing.T) { g, err := GetGlob("**.JSON") c.Assert(err, qt.IsNil) c.Assert(g.Match("data/my.json"), qt.Equals, true) +} + +func BenchmarkGetGlob(b *testing.B) { + for i := 0; i < b.N; i++ { + _, err := GetGlob("**/foo") + if err != nil { + b.Fatal(err) + } + } } |