Skip to content

Commit

Permalink
Merge pull request #723 from lsylvester/cached-environment-nils
Browse files Browse the repository at this point in the history
cache nil values in the CachedEnvironment
  • Loading branch information
rafaelfranca authored Jun 23, 2022
2 parents 894147f + e29f64a commit 06e1a8d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket
- Allow assets already fingerprinted to be served through `Sprockets::Server`
- Do not fingerprint files that already contain a valid digest in their name
- Remove remaining support for Ruby < 2.4.[#672](https://github.com/rails/sprockets/pull/672)
- Fix `CachedEnvironment` caching nil values [#723](https://github.com/rails/sprockets/pull/723)

## 4.0.2

Expand Down
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[path] ||= super(path)
@entries.fetch(path){ @entries[path] = super(path) }
end

# Internal: Cache Environment#stat
def stat(path)
@stats[path] ||= super(path)
@stats.fetch(path){ @stats[path] = super(path) }
end

# Internal: Cache Environment#load
def load(uri)
@uris[uri] ||= super(uri)
@uris.fetch(uri){ @uris[uri] = super(uri) }
end

# Internal: Cache Environment#processor_cache_key
def processor_cache_key(str)
@processor_cache_keys[str] ||= super(str)
@processor_cache_keys.fetch(str){ @processor_cache_keys[str] = super(str) }
end

# Internal: Cache Environment#resolve_dependency
def resolve_dependency(str)
@resolved_dependencies[str] ||= super(str)
@resolved_dependencies.fetch(str){ @resolved_dependencies[str] = super(str) }
end

private
Expand Down
18 changes: 18 additions & 0 deletions test/test_performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'sprockets_test'

$file_stat_calls = nil
$file_exist_calls = nil
class << File
alias_method :original_stat, :stat
def stat(filename)
Expand All @@ -11,6 +12,15 @@ def stat(filename)
end
original_stat(filename)
end

alias_method :original_exist?, :exist?
def exist?(filename)
if $file_exist_calls
$file_exist_calls[filename.to_s] ||= []
$file_exist_calls[filename.to_s] << caller
end
original_exist?(filename)
end
end

$dir_entires_calls = nil
Expand Down Expand Up @@ -56,6 +66,7 @@ def teardown
$bundle_processor_calls = nil
$cache_get_calls = nil
$cache_set_calls = nil
$file_exist_calls = nil
end

test "simple file" do
Expand Down Expand Up @@ -451,6 +462,7 @@ def new_environment(path = fixture_path('default'))

def reset_stats!
$file_stat_calls = {}
$file_exist_calls = {}
$dir_entires_calls = {}
$processor_calls = {}
$bundle_processor_calls = {}
Expand All @@ -473,9 +485,15 @@ def assert_no_redundant_stat_calls
assert_equal 1, callers.size, "File.stat(#{path.inspect}) called #{callers.size} times\n\n#{format_callers(callers)}"
end

$file_exist_calls.each do |path, callers|
assert_equal 1, callers.size, "File.exist?(#{path.inspect}) called #{callers.size} times\n\n#{format_callers(callers)}"
end

$dir_entires_calls.each do |path, callers|
assert_equal 1, callers.size, "Dir.entries(#{path.inspect}) called #{callers.size} times\n\n#{format_callers(callers)}"
end


end

def assert_no_processor_calls
Expand Down

0 comments on commit 06e1a8d

Please sign in to comment.