From 4e74e3a032a5b004b32ae57756f5bdd06941e732 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Mon, 19 Dec 2022 18:42:29 +0100 Subject: [PATCH] Use Concurrent::Map#fetch_or_store * This does not hold any lock while computing the missing key, so avoids the problem of having a lock while recursing when using #compute_if_absent. * As the docs say: https://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Map.html#fetch_or_store-instance_method "The store can overwrite other concurrently stored value.". Which was already possibly before, Hash#fetch does not have the guarantee to execute the block only once per key, even on CRuby. --- lib/sprockets/cached_environment.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/sprockets/cached_environment.rb b/lib/sprockets/cached_environment.rb index 3dfd75064..27aa9fba2 100644 --- a/lib/sprockets/cached_environment.rb +++ b/lib/sprockets/cached_environment.rb @@ -31,27 +31,27 @@ def cached # Internal: Cache Environment#entries def entries(path) - @entries.compute_if_absent(path) { super(path) } + @entries.fetch_or_store(path) { super(path) } end # Internal: Cache Environment#stat def stat(path) - @stats.compute_if_absent(path) { super(path) } + @stats.fetch_or_store(path) { super(path) } end # Internal: Cache Environment#load def load(uri) - @uris.compute_if_absent(uri) { super(uri) } + @uris.fetch_or_store(uri) { super(uri) } end # Internal: Cache Environment#processor_cache_key def processor_cache_key(str) - @processor_cache_keys.compute_if_absent(str) { super(str) } + @processor_cache_keys.fetch_or_store(str) { super(str) } end # Internal: Cache Environment#resolve_dependency def resolve_dependency(str) - @resolved_dependencies.compute_if_absent(str) { super(str) } + @resolved_dependencies.fetch_or_store(str) { super(str) } end private