Skip to content

Commit

Permalink
refactor: try dashmap raw-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 11, 2024
1 parent 8eb11e5 commit f886fa6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ name = "resolver"

[dependencies]
cfg-if = "1"
dashmap = "6"
dashmap = { version = "6", features = ["raw-api"] }
indexmap = { version = "2", features = ["serde"] }
json-strip-comments = "1"
once_cell = "1" # Use `std::sync::OnceLock::get_or_try_init` when it is stable.
Expand Down
13 changes: 13 additions & 0 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ impl<Fs: FileSystem> Cache<Fs> {
array.hash(&mut hasher);
hasher.finish()
};

// A DashMap is just an array of RwLock<HashSet>, sharded by hash to reduce lock contention.
// This uses the low level raw API to avoid cloning the value when using the `entry` method.
// First, find which shard the value is in, and check to see if we already have a value in the map.
let shard = self.paths.determine_shard(hash as usize);
{
// Scope the read lock.
let map = self.paths.shards()[shard].read();
if let Some((entry, _)) = map.get(hash, |v| v.0.path() == path) {
return entry.clone();
}
}

if let Some(cache_entry) = self.paths.get((hash, path).borrow() as &dyn CacheKey) {
return cache_entry.clone();
}
Expand Down

0 comments on commit f886fa6

Please sign in to comment.