aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorlvl0nax <lvl0nax@gmail.com>2016-04-25 09:46:49 +0300
committerlvl0nax <lvl0nax@gmail.com>2016-04-26 09:21:56 +0300
commitffb1df52c1f8a8e1805e310448eb66665804b5fc (patch)
tree4e50b7550e85d21b2eb352048ae78aab8b2ddc37 /activesupport
parente007afd3cd7255c752fe8ab7fa24b59526ab95cb (diff)
downloadrails-ffb1df52c1f8a8e1805e310448eb66665804b5fc.tar.gz
rails-ffb1df52c1f8a8e1805e310448eb66665804b5fc.tar.bz2
rails-ffb1df52c1f8a8e1805e310448eb66665804b5fc.zip
Little perfomance fix for Array#split.
Calculating ------------------------------------- before 40.770k i/100ms after 58.464k i/100ms ------------------------------------------------- before 629.568k (± 5.0%) i/s - 3.180M after 1.159M (± 4.5%) i/s - 5.788M
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/array/grouping.rb16
-rw-r--r--activesupport/test/core_ext/array/grouping_test.rb8
2 files changed, 14 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb
index 87ae052eb0..34af83d1ab 100644
--- a/activesupport/lib/active_support/core_ext/array/grouping.rb
+++ b/activesupport/lib/active_support/core_ext/array/grouping.rb
@@ -100,17 +100,13 @@ class Array
results
end
else
- results, arr = [[]], self.dup
- until arr.empty?
- if (idx = arr.index(value))
- results.last.concat(arr.shift(idx))
- arr.shift
- results << []
- else
- results.last.concat(arr.shift(arr.size))
- end
+ arr = self.dup
+ result = []
+ while (idx = arr.index(value))
+ result << arr.shift(idx)
+ arr.shift
end
- results
+ result << arr
end
end
end
diff --git a/activesupport/test/core_ext/array/grouping_test.rb b/activesupport/test/core_ext/array/grouping_test.rb
index 2eb0f05141..fb7367b0bf 100644
--- a/activesupport/test/core_ext/array/grouping_test.rb
+++ b/activesupport/test/core_ext/array/grouping_test.rb
@@ -123,4 +123,12 @@ class SplitTest < ActiveSupport::TestCase
assert_equal [[], [2, 3, 4], []], a.split { |i| i == 1 || i == 5 }
assert_equal [1, 2, 3, 4, 5], a
end
+
+ def test_split_with_repeated_values
+ a = [1, 2, 3, 5, 5, 3, 4, 6, 2, 1, 3]
+ assert_equal [[1, 2], [5, 5], [4, 6, 2, 1], []], a.split(3)
+ assert_equal [[1, 2, 3], [], [3, 4, 6, 2, 1, 3]], a.split(5)
+ assert_equal [[1, 2], [], [], [], [4, 6, 2, 1], []], a.split { |i| i == 3 || i == 5 }
+ assert_equal [1, 2, 3, 5, 5, 3, 4, 6, 2, 1, 3], a
+ end
end