From 2d0917493d1b31161d81e45c7bfd19372dd7835c Mon Sep 17 00:00:00 2001 From: Mike Angell Date: Tue, 27 Aug 2019 22:07:44 +1000 Subject: [PATCH] Add performance rubocop and fixes --- .rubocop.yml | 5 +++++ .travis.yml | 4 ---- Gemfile | 1 + lib/liquid/block_body.rb | 4 ++-- lib/liquid/errors.rb | 4 ++-- lib/liquid/parser.rb | 2 +- lib/liquid/tags/ifchanged.rb | 2 +- lib/liquid/tags/raw.rb | 2 +- lib/liquid/template.rb | 2 +- test/integration/drop_test.rb | 4 ++-- test/unit/block_unit_test.rb | 4 ++-- test/unit/tag_unit_test.rb | 4 ++-- 12 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 9373c6cf..5bb1cc77 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,10 @@ inherit_from: - .rubocop.shopify.yml +require: rubocop-performance + +Performance: + Enabled: true + Metrics/LineLength: Enabled: false \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 593c9551..c9928fc8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,10 +21,6 @@ matrix: - rvm: ruby-head - rvm: jruby-head -before_install: - - gem update --system - - gem install bundler - install: - bundle install diff --git a/Gemfile b/Gemfile index 4dede32c..1166eab6 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ end group :test do gem 'rubocop', '~> 0.74.0', require: false + gem 'rubocop-performance', require: false platform :mri, :truffleruby do gem 'liquid-c', github: 'Shopify/liquid-c', ref: '7ba926791ef8411984d0f3e41c6353fd716041c6' diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index 504b653e..7a011f66 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -65,7 +65,7 @@ module Liquid end def render(context) - render_to_output_buffer(context, ''.dup) + render_to_output_buffer(context, +'') end def render_to_output_buffer(context, output) @@ -81,7 +81,7 @@ module Liquid when Variable render_node(context, output, node) when Block - render_node(context, node.blank? ? ''.dup : output, node) + render_node(context, node.blank? ? +'' : output, node) break if context.interrupt? # might have happened in a for-block when Continue, Break # If we get an Interrupt that means the block must stop processing. An diff --git a/lib/liquid/errors.rb b/lib/liquid/errors.rb index e61db4be..652ac0d4 100644 --- a/lib/liquid/errors.rb +++ b/lib/liquid/errors.rb @@ -7,7 +7,7 @@ module Liquid attr_accessor :markup_context def to_s(with_prefix = true) - str = ''.dup + str = +'' str << message_prefix if with_prefix str << super() @@ -22,7 +22,7 @@ module Liquid private def message_prefix - str = ''.dup + str = +'' str << if is_a?(SyntaxError) 'Liquid syntax error' else diff --git a/lib/liquid/parser.rb b/lib/liquid/parser.rb index 82920ac2..fbf3df06 100644 --- a/lib/liquid/parser.rb +++ b/lib/liquid/parser.rb @@ -70,7 +70,7 @@ module Liquid end def argument - str = ''.dup + str = +'' # might be a keyword argument (identifier: expression) str << consume << consume << ' ' if look(:id) && look(:colon, 1) diff --git a/lib/liquid/tags/ifchanged.rb b/lib/liquid/tags/ifchanged.rb index 92a2ac2d..48a69f3b 100644 --- a/lib/liquid/tags/ifchanged.rb +++ b/lib/liquid/tags/ifchanged.rb @@ -4,7 +4,7 @@ module Liquid class Ifchanged < Block def render_to_output_buffer(context, output) context.stack do - block_output = ''.dup + block_output = +'' super(context, block_output) if block_output != context.registers[:ifchanged] diff --git a/lib/liquid/tags/raw.rb b/lib/liquid/tags/raw.rb index 95f5bb4d..9825f48a 100644 --- a/lib/liquid/tags/raw.rb +++ b/lib/liquid/tags/raw.rb @@ -12,7 +12,7 @@ module Liquid end def parse(tokens) - @body = ''.dup + @body = +'' while (token = tokens.shift) if token =~ FULL_TOKEN_POSSIBLY_INVALID @body << Regexp.last_match(1) if Regexp.last_match(1) != '' diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 0c600698..86a5cbab 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -204,7 +204,7 @@ module Liquid # render the nodelist. # for performance reasons we get an array back here. join will make a string out of it. with_profiling(context) do - @root.render_to_output_buffer(context, output || ''.dup) + @root.render_to_output_buffer(context, output || +'') end rescue Liquid::MemoryError => e context.handle_error(e) diff --git a/test/integration/drop_test.rb b/test/integration/drop_test.rb index 0e106c2b..5b20c8c1 100644 --- a/test/integration/drop_test.rb +++ b/test/integration/drop_test.rb @@ -33,7 +33,7 @@ class ProductDrop < Liquid::Drop class CatchallDrop < Liquid::Drop def liquid_method_missing(method) - 'catchall_method: '.dup << method.to_s + +'catchall_method: ' << method.to_s end end @@ -50,7 +50,7 @@ class ProductDrop < Liquid::Drop end def user_input - 'foo'.dup.taint + (+'foo').taint end protected diff --git a/test/unit/block_unit_test.rb b/test/unit/block_unit_test.rb index 815ee77c..bf887e2f 100644 --- a/test/unit/block_unit_test.rb +++ b/test/unit/block_unit_test.rb @@ -63,7 +63,7 @@ class BlockUnitTest < Minitest::Test assert_equal 'hello', template.render - buf = ''.dup + buf = +'' output = template.render({}, output: buf) assert_equal 'hello', output assert_equal 'hello', buf @@ -81,7 +81,7 @@ class BlockUnitTest < Minitest::Test assert_equal 'foohellobar', template.render - buf = ''.dup + buf = +'' output = template.render({}, output: buf) assert_equal 'foohellobar', output assert_equal 'foohellobar', buf diff --git a/test/unit/tag_unit_test.rb b/test/unit/tag_unit_test.rb index 4191118f..88d08dbb 100644 --- a/test/unit/tag_unit_test.rb +++ b/test/unit/tag_unit_test.rb @@ -33,7 +33,7 @@ class TagUnitTest < Minitest::Test assert_equal 'hello', template.render - buf = ''.dup + buf = +'' output = template.render({}, output: buf) assert_equal 'hello', output assert_equal 'hello', buf @@ -51,7 +51,7 @@ class TagUnitTest < Minitest::Test assert_equal 'foohellobar', template.render - buf = ''.dup + buf = +'' output = template.render({}, output: buf) assert_equal 'foohellobar', output assert_equal 'foohellobar', buf