perf(controller): eliminate redundant treehash storage reads - #306
perf(controller): eliminate redundant treehash storage reads#306Arjunmehta312 wants to merge 3 commits into
Conversation
| // letting an orchestrator see it could poison the shared cache with | ||
| // stripped graphs. | ||
| func (c *controller) getGraph(ctx context.Context, e *metrics.Emitter, req entity.GetTargetGraphRequest) (storage.GraphReader, error) { | ||
| // If treehash is provided (non-empty), it is used instead of reading from storage. |
There was a problem hiding this comment.
threading "" as "not provided" through 4 function signatures is fragile — it conflates "caller didn't look it up" with "looked up, doesn't exist in storage."
Consider a separate resolver with memoization instead :
type treehashResolver struct {
storage storage.Storage
emitter *metrics.Emitter
op string
mu sync.Mutex
cache map[string]resolvedTreehash
}
type resolvedTreehash struct {
value string
err error
}
func (r *treehashResolver) resolve(ctx context.Context, build entity.BuildDescription) (string, error) {
key := cachekey.GetTreehashCachePath(build)
r.mu.Lock()
if entry, ok := r.cache[key]; ok {
r.mu.Unlock()
return entry.value, entry.err
}
r.mu.Unlock()
value, err := readTreehash(ctx, r.storage, build, r.emitter, r.op)
r.mu.Lock()
r.cache[key] = resolvedTreehash{value: value, err: err}
r.mu.Unlock()
return value, err
}Create it at the top of GetChangedTargets and let serveChangedTargetsFromCache, getGraph, and cacheComparedTargets call resolver.resolve() directly. Second call for the same build is a map hit , no "" sentinel, no conditional branches, and if a new code path needs the treehash later it just calls resolve() for free.
There was a problem hiding this comment.
Implemented this approach in the latest commit: replaced the "" sentinel parameters with a request-scoped treehashResolver, which memoizes successful resolutions without caching errors.
Thanks for the suggestion!
GetChangedTargets previously read the same treehash values multiple times during cache lookup and background cache population. Read the treehashes once and pass the values through the existing call chain so they can be reused. This reduces treehash storage.Get calls from 6 to 2 in the affected cache miss path.
Replace the empty-string treehash sentinel parameters with a request-scoped resolver that reuses successfully resolved treehash values. This avoids repeating the same storage reads during GetChangedTargets while keeping treehash resolution and error handling request-local.
Keep treehash resolution request-scoped while preserving the concurrent lookup behavior used by GetChangedTargets. Memoize successful treehash values without caching errors, and reuse the resolved values across the request pipeline.
b361c70 to
055f6e1
Compare
|
Rebased onto latest upstream/main (resolved conflict in What was done:
Implementation summary:
Test results: All 29 tests pass, race detector passes. Ready for re-review @xytan0056 |
Summary
Avoid rereading treehash values during
GetChangedTargetsby reading themonce and reusing them across cache lookup, graph lookup, and background
cache population.
Validation
path.
make testpasses.make lintpasses.