-
Notifications
You must be signed in to change notification settings - Fork 249
[nar info cache] Only fillNarInfoCache in perf-sensitive code path #1468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Current dependencies on/for this PR: This comment was auto-generated by Graphite. |
a3d5673
to
117341a
Compare
117341a
to
019abe6
Compare
internal/devpkg/narinfo_cache.go
Outdated
@@ -146,6 +163,7 @@ func (p *Package) fillNarInfoCache() error { | |||
return nil | |||
} | |||
|
|||
// isEligibleForBinaryCache returns true if the package is eligible for the binary cache. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, I'll improve this to be less self-referential.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
) | ||
if !statusExists { | ||
// Fallback to synchronously filling the nar info cache | ||
if err := p.fillNarInfoCache(); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For potentially simpler code. By definition, fillNarInfoCache()
only returns a nil error if it succeeded. So you could do:
err := p.fillNarInfoCache()
return err == nil, err
Additionally, if fillNarInfoCache
does
if isNarInfoInCache.status[p.Raw] {
return nil
}
(no need to lock because we never remove from cache)
Then this entire function could be:
func IsInBinaryCache()
if eligible, err := p.isEligibleForBinaryCache(); err != nil {
return false, err
} else if !eligible {
return false, nil
}
err := p.fillNarInfoCache()
return err == nil, err
}
(maybe rename fillNarInfoCache
to fillNarInfoCacheIfNeeded
for clarity)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, this doesn't quite work for two reasons:
-
fillNarInfoCache stores the boolean of whether the value is stored or not. A false value for a key in
isNarInfoInCache
does not indicate an error. -
Golang maps are not safe for concurrent reads and writes, so we will need a lock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@savil you are correct on (1) but not on (2).
For (2), reading a true
is only unsafe if we remove and/or change values. Since we should never remove/change values within a single command, I think this is safe and improves code readability quite a bit.
For (1), we can modify fillNarInfoCache()
to return (status, error)
. The early return continues to be safe because if something is set to true it never changes.
An additional benefit of this approach is that we limit cache access to a single function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clarified why (2) is correct in #1473 (comment)
Summary
In this PR, we loosen the conditions under which
devpkg.Package.IsInBinaryCache
can be called. Internally, it checks an in-memory map for the status of the package in the binary cache. Previously, it was required that callers pre-compute this status by invokingFillNarInfoInCache
. The motivation then was to ensure that we avoid a synchronous http request ifIsInBinaryCache
is called within a loop.However, this condition and the fact that golang lacks async-await support resulted in a loosely coupled load-and-check-later design whereby calls to
FillNarInfoInCache
would be sprinkled in various parts of the codebase and thenIsInBinaryCache
checked in other parts. This loose-coupling made it hard to reason about why any particular FillNarInfoInCache was present.The straw that broke the proverbial camel's back was the issue that #1463 was seeking to fix.
In this PR, we add a fallback http request inside
IsInBinaryCache
and remove all but one of theFillNarInfoInCache
calls. Reason:shellenv
.NOTE: this PR also re-enables Remove Nixpkgs feature flag.
How was it tested?