From e935bd8e8a983027cfb29fa9f4dd5be88df9ad7d Mon Sep 17 00:00:00 2001 From: Lachlan Sylvester Date: Tue, 26 Oct 2021 20:24:51 +1100 Subject: [PATCH 1/4] cache nil values in the CachedEnvironment --- 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 7d44386d0..72c6c2f84 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[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 From 5453f4d840c6b123460bf991a234156315f86e2f Mon Sep 17 00:00:00 2001 From: Lachlan Sylvester Date: Wed, 27 Oct 2021 20:58:00 +1100 Subject: [PATCH 2/4] add Changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c05d48fe9..24f6d1fef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,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 ## 4.0.2 From 9efe5ffcd8e40992d566e6716c7c68c2dbb9219f Mon Sep 17 00:00:00 2001 From: Lachlan Sylvester Date: Wed, 27 Oct 2021 20:58:59 +1100 Subject: [PATCH 3/4] update assert_no_redundant_stat_calls to ensure there are no redundent calls to File.exist? --- test/test_performance.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_performance.rb b/test/test_performance.rb index f2ffde606..7950462da 100644 --- a/test/test_performance.rb +++ b/test/test_performance.rb @@ -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) @@ -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 @@ -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 @@ -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 = {} @@ -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 From e29f64a151ab25630263b5ac10921b0663d97c59 Mon Sep 17 00:00:00 2001 From: Lachlan Sylvester Date: Wed, 27 Oct 2021 21:01:50 +1100 Subject: [PATCH 4/4] add PR to Changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24f6d1fef..0fdc212ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,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 +- Fix `CachedEnvironment` caching nil values [#723](https://github.com/rails/sprockets/pull/723) ## 4.0.2