From 385260590f9851b9f48c678b3d4313fce712c244 Mon Sep 17 00:00:00 2001 From: Dillon Welch Date: Mon, 19 Mar 2018 21:30:38 -0700 Subject: Only create an array with default options if we have default options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the options passed in don't have a default key, there's no point in creating an array from those empty results when we can just go straight to creating an empty array. Benchmarks: ```ruby master_version with false {:FREE=>-2497, :T_STRING=>52, :T_ARRAY=>2000, :T_HASH=>1000, :T_IMEMO=>1} master_version with true {:FREE=>-3001, :T_ARRAY=>2000, :T_HASH=>1000} fast_version with false {:FREE=>-1001, :T_ARRAY=>1000} fast_version with true {:FREE=>-3001, :T_ARRAY=>2000, :T_HASH=>1000} Warming up -------------------------------------- master_version with false 104.985k i/100ms master_version with true 118.737k i/100ms fast_version with false 206.013k i/100ms fast_version with true 107.005k i/100ms Calculating ------------------------------------- master_version with false 1.970M (±24.6%) i/s - 8.924M in 5.010302s master_version with true 2.152M (±12.4%) i/s - 10.686M in 5.051588s fast_version with false 5.613M (±19.6%) i/s - 26.782M in 5.003740s fast_version with true 2.027M (±15.8%) i/s - 9.951M in 5.065670s Comparison: fast_version with false: 5613159.2 i/s master_version with true: 2152354.4 i/s - 2.61x slower fast_version with true: 2027296.0 i/s - 2.77x slower master_version with false: 1969824.9 i/s - 2.85x slower ``` Benchmark code: ```ruby begin require "bundler/inline" rescue LoadError => e $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" raise e end gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" gem "rails" end def allocate_count GC.disable before = ObjectSpace.count_objects yield after = ObjectSpace.count_objects after.each { |k,v| after[k] = v - before[k] } after[:T_HASH] -= 1 # probe effect - we created the before hash. GC.enable result = after.reject { |k,v| v == 0 } GC.start result end def master_version(key) Array({}.delete(:default)).compact end def fast_version(key) if key Array({}.delete(:default)).compact else [] end end def test puts "master_version with false" puts allocate_count { 1000.times { master_version(false) } } puts "master_version with true" puts allocate_count { 1000.times { master_version(true) } } puts "fast_version with false" puts allocate_count { 1000.times { fast_version(false) } } puts "fast_version with true" puts allocate_count { 1000.times { fast_version(true) } } Benchmark.ips do |x| x.report("master_version with false") { master_version(false) } x.report("master_version with true") { master_version(true) } x.report("fast_version with false") { fast_version(false) } x.report("fast_version with true") { fast_version(true) } x.compare! end end test ``` --- actionview/lib/action_view/helpers/translation_helper.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'actionview/lib') diff --git a/actionview/lib/action_view/helpers/translation_helper.rb b/actionview/lib/action_view/helpers/translation_helper.rb index 1860bc4732..3ef9cb0fc3 100644 --- a/actionview/lib/action_view/helpers/translation_helper.rb +++ b/actionview/lib/action_view/helpers/translation_helper.rb @@ -60,7 +60,11 @@ module ActionView def translate(key, options = {}) options = options.dup has_default = options.has_key?(:default) - remaining_defaults = Array(options.delete(:default)).compact + if has_default + remaining_defaults = Array(options.delete(:default)).compact + else + remaining_defaults = [] + end if has_default && !remaining_defaults.first.kind_of?(Symbol) options[:default] = remaining_defaults -- cgit v1.2.3 From 47013a7126a92e1f2890b68e0fd2e7ba1b77c97c Mon Sep 17 00:00:00 2001 From: Yaroslav Markin Date: Tue, 17 Apr 2018 18:05:12 +0300 Subject: Add the `nonce: true` option for `javascript_include_tag` helper. --- actionview/lib/action_view/helpers/asset_tag_helper.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'actionview/lib') diff --git a/actionview/lib/action_view/helpers/asset_tag_helper.rb b/actionview/lib/action_view/helpers/asset_tag_helper.rb index 06fa1875fc..257080d902 100644 --- a/actionview/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionview/lib/action_view/helpers/asset_tag_helper.rb @@ -55,6 +55,8 @@ module ActionView # that path. # * :skip_pipeline - This option is used to bypass the asset pipeline # when it is set to true. + # * :nonce - When set to true, adds an automatic nonce value if + # you have Content Security Policy enabled. # # ==== Examples # @@ -79,6 +81,9 @@ module ActionView # # javascript_include_tag "http://www.example.com/xmlhr.js" # # => + # + # javascript_include_tag "http://www.example.com/xmlhr.js", nonce: true + # # => def javascript_include_tag(*sources) options = sources.extract_options!.stringify_keys path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys @@ -90,6 +95,9 @@ module ActionView tag_options = { "src" => href }.merge!(options) + if tag_options["nonce"] == true + tag_options["nonce"] = content_security_policy_nonce + end content_tag("script".freeze, "", tag_options) }.join("\n").html_safe -- cgit v1.2.3 From 8201646327796b18187b9ca3a165a18a468cc2f6 Mon Sep 17 00:00:00 2001 From: utilum Date: Tue, 17 Apr 2018 21:58:44 +0200 Subject: Ruby 2.6 warning: passing splat keyword arguments as a single Hash Before: ``` $ ruby -v ruby 2.6.0dev (2018-04-04 trunk 63085) [x86_64-linux] $ bundle exec rake test:template ... /rails/actionview/lib/action_view/digestor.rb:76: warning: passing splat keyword arguments as a single Hash to `find_all' ``` --- actionview/lib/action_view/digestor.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'actionview/lib') diff --git a/actionview/lib/action_view/digestor.rb b/actionview/lib/action_view/digestor.rb index 45cf48b3e0..3832293251 100644 --- a/actionview/lib/action_view/digestor.rb +++ b/actionview/lib/action_view/digestor.rb @@ -71,11 +71,16 @@ module ActionView private def find_template(finder, *args) + name = args.first + prefixes = args[1] || [] + partial = args[2] || false + keys = args[3] || [] + options = args[4] || {} finder.disable_cache do if format = finder.rendered_format - finder.find_all(*args, formats: [format]).first || finder.find_all(*args).first + finder.find_all(name, prefixes, partial, keys, options.merge(formats: [format])).first || finder.find_all(name, prefixes, partial, keys, options).first else - finder.find_all(*args).first + finder.find_all(name, prefixes, partial, keys, options).first end end end -- cgit v1.2.3