From 71e41a5b9495ba6ebe3cdf1e10afa27bcc0db208 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 28 Jul 2019 17:16:03 +0900 Subject: Performance improvement for `String#to` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ```ruby class String def to1(position) position = [position + length, -1].max if position < 0 self[0, position + 1] end def to2(position) position += size if position < 0 self[0, position + 1] || +"" end end Benchmark.ips do |x| x.report("'foo'.to(1)") { 'foo'.to(1) } x.report("'foo'.to1(1)") { 'foo'.to1(1) } x.report("'foo'.to2(1)") { 'foo'.to2(1) } x.report("'foo'.to(-1)") { 'foo'.to(-1) } x.report("'foo'.to1(-1)") { 'foo'.to1(-1) } x.report("'foo'.to2(-1)") { 'foo'.to2(-1) } x.report("'foo'.to(-10)") { 'foo'.to(-10) } x.report("'foo'.to1(-10)") { 'foo'.to1(-10) } x.report("'foo'.to2(-10)") { 'foo'.to2(-10) } end ``` Result: ``` Warming up -------------------------------------- 'foo'.to(1) 199.859k i/100ms 'foo'.to1(1) 220.293k i/100ms 'foo'.to2(1) 221.522k i/100ms 'foo'.to(-1) 205.032k i/100ms 'foo'.to1(-1) 195.837k i/100ms 'foo'.to2(-1) 214.975k i/100ms 'foo'.to(-10) 214.331k i/100ms 'foo'.to1(-10) 182.666k i/100ms 'foo'.to2(-10) 224.696k i/100ms Calculating ------------------------------------- 'foo'.to(1) 4.685M (± 4.2%) i/s - 23.583M in 5.042568s 'foo'.to1(1) 5.233M (± 5.8%) i/s - 26.215M in 5.026778s 'foo'.to2(1) 5.180M (± 5.7%) i/s - 25.918M in 5.020735s 'foo'.to(-1) 4.253M (± 7.0%) i/s - 21.323M in 5.043133s 'foo'.to1(-1) 4.438M (±11.2%) i/s - 21.934M in 5.025751s 'foo'.to2(-1) 4.716M (± 9.8%) i/s - 23.432M in 5.028088s 'foo'.to(-10) 4.678M (± 9.5%) i/s - 23.148M in 5.007379s 'foo'.to1(-10) 4.428M (± 5.1%) i/s - 22.103M in 5.005155s 'foo'.to2(-10) 5.243M (± 4.6%) i/s - 26.289M in 5.024695s ``` --- activesupport/lib/active_support/core_ext/string/access.rb | 2 +- activesupport/lib/active_support/dependencies.rb | 1 - activesupport/lib/active_support/secure_compare_rotator.rb | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb index eff31e5493..1a0b10b4f6 100644 --- a/activesupport/lib/active_support/core_ext/string/access.rb +++ b/activesupport/lib/active_support/core_ext/string/access.rb @@ -62,7 +62,7 @@ class String # str.from(1).to(-2) # => "ell" def to(position) position += size if position < 0 - self[0, position + 1].to_s + self[0, position + 1] || +"" end # Returns the first character. If a limit is supplied, returns a substring diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 923d6ad228..6d71a67a46 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -793,7 +793,6 @@ module ActiveSupport #:nodoc: end private - # Returns the original name of a class or module even if `name` has been # overridden. def real_mod_name(mod) diff --git a/activesupport/lib/active_support/secure_compare_rotator.rb b/activesupport/lib/active_support/secure_compare_rotator.rb index 97110d41f7..269703c34a 100644 --- a/activesupport/lib/active_support/secure_compare_rotator.rb +++ b/activesupport/lib/active_support/secure_compare_rotator.rb @@ -44,7 +44,6 @@ module ActiveSupport end private - def build_rotation(previous_value, _options) self.class.new(previous_value) end -- cgit v1.2.3