Skip to content

Replace fetchToStore cache by sourcePathToHash cache#14769

Open
edolstra wants to merge 1 commit intomasterfrom
nar-hash-cache
Open

Replace fetchToStore cache by sourcePathToHash cache#14769
edolstra wants to merge 1 commit intomasterfrom
nar-hash-cache

Conversation

@edolstra
Copy link
Member

Motivation

Caching NAR hashes instead of store paths makes the cache more general, because we can always compute the store path from the NAR hash, but not the other way around. This is useful for lazy trees, where we want to compute the NAR hash of an input with caching.

This PR also cherry-picks DeterminateSystems#157, which adds a Store::maybeQueryPathInfo() method. This is preferred over isValidPath() because it has better caching.

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added store Issues and pull requests concerning the Nix store fetching Networking with the outside (non-Nix) world, input locking labels Dec 10, 2025
Comment on lines -26 to -27
// FIXME: add an optimisation for the case where the accessor is
// a `PosixSourceAccessor` pointing to a store path.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this still be done?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so, the path fetcher has an optimization that does something like that now.

Comment on lines +342 to +345
settings.getCache()->upsert(
makeSourcePathToHashCacheKey(
*accessor->fingerprint, ContentAddressMethod::Raw::NixArchive, CanonPath::root),
{{"hash", store.queryPathInfo(storePath)->narHash.to_string(HashFormat::SRI, true)}});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
settings.getCache()->upsert(
makeSourcePathToHashCacheKey(
*accessor->fingerprint, ContentAddressMethod::Raw::NixArchive, CanonPath::root),
{{"hash", store.queryPathInfo(storePath)->narHash.to_string(HashFormat::SRI, true)}});

Tests pass without this code. It could be dead or broken.
Make sure to cover this with the _NIX_TEST_BARF_ON_UNCACHEABLE trick.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_NIX_TEST_BARF_ON_UNCACHEABLE doesn't test that. It fails on inputs that don't have a fingerprint. If you remove the upsert, it disables caching, but there is nothing that tests that.

Copy link
Member

@roberth roberth Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that is a bit different. Not a bad idea though considering how unreliable the caches have been.

(does need a different name it seems)

fetchers::Cache::Key makeFetchToStoreCacheKey(
const std::string & name, const std::string & fingerprint, ContentAddressMethod method, const std::string & path)
fetchers::Cache::Key
makeSourcePathToHashCacheKey(std::string_view fingerprint, ContentAddressMethod method, const CanonPath & path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CanonPath is good

Caching NAR hashes instead of store paths makes the cache more
general, because we can always compute the store path from the NAR
hash, but not the other way around. This is useful for lazy trees,
where we want to compute the NAR hash of an input with caching.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fetching Networking with the outside (non-Nix) world, input locking store Issues and pull requests concerning the Nix store

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants