Skip to content

Commit bcacb74

Browse files
committed
fix: add a hash cache
Hashes get recomputed often, which previously didn't matter because only file metadata was hashed. Now that bit is hashing content, it really impacts performance. This cache drops _listing_ time from 0.7s to 0.2s in a relatively small repo.
1 parent a9e689a commit bcacb74

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

engine/engine.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ type Target struct {
4848
type RefKey string
4949

5050
type Engine struct {
51-
cwd string
52-
globber *internal.Globber
53-
log *logging.Logger
54-
vars Vars
55-
db *HashDB
56-
targets []*Target
57-
outputs map[RefKey]*Target
58-
inputs map[RefKey]*Target
51+
cwd string
52+
globber *internal.Globber
53+
log *logging.Logger
54+
vars Vars
55+
db *HashDB
56+
targets []*Target
57+
outputs map[RefKey]*Target
58+
inputs map[RefKey]*Target
59+
cacheHash map[string]Hasher
5960
}
6061

6162
// Compile a Bitfile into an Engine ready to build targets.
@@ -88,6 +89,7 @@ func Compile(logger *logging.Logger, bitfile *parser.Bitfile) (*Engine, error) {
8889
vars: map[string]*parser.Block{
8990
"CWD": {Pos: bitfile.Pos, Body: cwd},
9091
},
92+
cacheHash: map[string]Hasher{},
9193
}
9294
engine.globber, err = internal.NewGlobber(os.DirFS(cwd), engine.Outputs)
9395
if err != nil {
@@ -542,6 +544,9 @@ func (e *Engine) dbRefHasher(target *Target, ref *parser.Ref) (Hasher, error) {
542544

543545
// Hash real files.
544546
func (e *Engine) realRefHasher(target *Target, ref *parser.Ref) (Hasher, error) {
547+
if h, ok := e.cacheHash[ref.Text]; ok {
548+
return h, nil
549+
}
545550
h := NewHasher()
546551
h.Str(ref.Text)
547552

@@ -570,6 +575,7 @@ func (e *Engine) realRefHasher(target *Target, ref *parser.Ref) (Hasher, error)
570575
}
571576
defer r.Close()
572577
_, _ = io.Copy(&h, r)
578+
e.cacheHash[ref.Text] = h
573579
return h, nil
574580
}
575581

0 commit comments

Comments
 (0)