From f3fb43c72979a68bbf3612e6c7a10ab4ba5f06e1 Mon Sep 17 00:00:00 2001 From: Christopher Aue Date: Sat, 26 Aug 2017 15:19:38 +0200 Subject: Reimplemented Array#flatten with #flatten! --- mrbgems/mruby-array-ext/mrblib/array.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 7146604d7..3b824ac0f 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -169,15 +169,9 @@ class Array # a.flatten(1) #=> [1, 2, 3, [4, 5]] # def flatten(depth=nil) - ar = [] - self.each do |e| - if e.is_a?(Array) && (depth.nil? || depth > 0) - ar += e.flatten(depth.nil? ? nil : depth - 1) - else - ar << e - end - end - ar + res = dup + res.flatten! depth + res end ## -- cgit v1.2.3 From 6c9013e22b56f8a6b9bbd8ba2defa9db6a768401 Mon Sep 17 00:00:00 2001 From: Christopher Aue Date: Sat, 26 Aug 2017 15:20:57 +0200 Subject: Removed unneeded block check in Array#uniq --- mrbgems/mruby-array-ext/mrblib/array.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 3b824ac0f..21ef6791b 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -81,11 +81,7 @@ class Array # def uniq(&block) ary = self.dup - if block - ary.uniq!(&block) - else - ary.uniq! - end + ary.uniq!(&block) ary end -- cgit v1.2.3 From c956e17c02d890ed96f640504f57eae0495f1f75 Mon Sep 17 00:00:00 2001 From: Christopher Aue Date: Sat, 26 Aug 2017 15:42:52 +0200 Subject: Replaced Array#each with while loop for performance reasons Example benchmark: $ time build/bench/bin/mruby -e "Array.new(2_000_000){ |i| i }.index{ |i| i == 1_999_999 }" Before: real 0m0.934s user 0m0.922s sys 0m0.003s After: real 0m0.590s user 0m0.583s sys 0m0.007s --- mrbgems/mruby-array-ext/mrblib/array.rb | 49 ++++++++++++++++++++++++++------- mrblib/array.rb | 7 ++++- 2 files changed, 45 insertions(+), 11 deletions(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 21ef6791b..e28e5238d 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -101,8 +101,19 @@ class Array hash = {} array = [] - elem.each { |x| hash[x] = true } - self.each { |x| array << x unless hash[x] } + idx = 0 + len = elem.size + while idx < len + hash[elem[idx]] = true + idx += 1 + end + idx = 0 + len = size + while idx < len + v = self[idx] + array << v unless hash[v] + idx += 1 + end array end @@ -137,12 +148,21 @@ class Array hash = {} array = [] - elem.each{|v| hash[v] = true } - self.each do |v| + idx = 0 + len = elem.size + while idx < len + hash[elem[idx]] = true + idx += 1 + end + idx = 0 + len = size + while idx < len + v = self[idx] if hash[v] array << v hash.delete v end + idx += 1 end array end @@ -190,13 +210,17 @@ class Array def flatten!(depth=nil) modified = false ar = [] - self.each do |e| + idx = 0 + len = size + while idx < len + e = self[idx] if e.is_a?(Array) && (depth.nil? || depth > 0) ar += e.flatten(depth.nil? ? nil : depth - 1) modified = true else ar << e end + idx += 1 end if modified self.replace(ar) @@ -718,10 +742,14 @@ class Array return to_enum :select! unless block result = [] - self.each do |x| - result << x if block.call(x) + idx = 0 + len = size + while idx < len + elem = self[idx] + result << elem if block.call(elem) + idx += 1 end - return nil if self.size == result.size + return nil if len == result.size self.replace(result) end @@ -743,8 +771,9 @@ class Array if block idx = 0 - self.each do |*e| - return idx if block.call(*e) + len = size + while idx < len + return idx if block.call self[idx] idx += 1 end else diff --git a/mrblib/array.rb b/mrblib/array.rb index 5b9ee47c0..a75ed6223 100644 --- a/mrblib/array.rb +++ b/mrblib/array.rb @@ -46,7 +46,12 @@ class Array def collect!(&block) return to_enum :collect! unless block - self.each_index { |idx| self[idx] = block.call(self[idx]) } + idx = 0 + len = size + while idx < len + self[idx] = block.call self[idx] + idx += 1 + end self end -- cgit v1.2.3