perf(controller): eliminate redundant treehash storage reads - #306
Open
Arjunmehta312 wants to merge 2 commits into
Open
perf(controller): eliminate redundant treehash storage reads#306Arjunmehta312 wants to merge 2 commits into
Arjunmehta312 wants to merge 2 commits into
Conversation
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.
xytan0056
approved these changes
Aug 27, 2026
xytan0056
reviewed
Aug 27, 2026
| // 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. |
Contributor
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.
xytan0056
self-requested a review
August 27, 2026 23:19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.