Skip to content

Commit

Permalink
Optimize Array#* (crystal-lang#8087)
Browse files Browse the repository at this point in the history
We use the same trick of multiple memcpy like we do in `String#*`
and also consider a few edge cases like 0 and 1.
  • Loading branch information
asterite authored and dnamsons committed Jan 10, 2020
1 parent 168a52f commit 2625d2b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions spec/std/array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ describe "Array" do
end

it "does *" do
(([] of Int32) * 10).empty?.should be_true
([1, 2, 3] * 0).empty?.should be_true
([1] * 3).should eq([1, 1, 1])
([1, 2, 3] * 3).should eq([1, 2, 3, 1, 2, 3, 1, 2, 3])
([1, 2] * 10).should eq([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2])
end

describe "[]" do
Expand Down
28 changes: 24 additions & 4 deletions src/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,31 @@ class Array(T)
# ["a", "b", "c"] * 2 # => [ "a", "b", "c", "a", "b", "c" ]
# ```
def *(times : Int)
ary = Array(T).new(size * times)
times.times do
ary.concat(self)
if times == 0 || empty?
return Array(T).new
end

if times == 1
return dup
end

if size == 1
return Array(T).new(times, first)
end

new_size = size * times
Array(T).build(new_size) do |buffer|
buffer.copy_from(to_unsafe, size)
n = size

while n <= new_size // 2
(buffer + n).copy_from(buffer, n)
n *= 2
end

(buffer + n).copy_from(buffer, new_size - n)
new_size
end
ary
end

# Append. Alias for `push`.
Expand Down

0 comments on commit 2625d2b

Please sign in to comment.