Skip to content

Commit

Permalink
Use Concurrent::Map#fetch_or_store
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
eregon committed Dec 19, 2022
1 parent 2d454bc commit 4e74e3a
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/sprockets/cached_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e74e3a

Please sign in to comment.