Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array: optimize shift and unshift #10081

Merged
merged 3 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion etc/lldb/crystal_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def update(self):
self.valobj = self.valobj.Dereference()
self.size = int(self.valobj.child[0].value)
self.type = self.valobj.type
self.buffer = self.valobj.child[2]
self.buffer = self.valobj.child[3]

def num_children(self):
size = 0 if self.size is None else self.size
Expand Down
149 changes: 149 additions & 0 deletions spec/std/array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,15 @@ describe "Array" do
a.should eq([6, 7, 4, 5])
end

it "optimizes when index is 0" do
a = [1, 2, 3, 4, 5, 6, 7, 8]
buffer = a.@buffer
a[0..2] = 10
a.should eq([10, 4, 5, 6, 7, 8])
a.@offset_to_buffer.should eq(2)
a.@buffer.should eq(buffer + 2)
end

it "replaces entire range with a value for empty array (#8341)" do
a = [] of Int32
a[..] = 6
Expand Down Expand Up @@ -535,6 +544,15 @@ describe "Array" do
a.should eq([1, 3, 4])
end

it "deletes at beginning is same as shift" do
a = [1, 2, 3, 4]
buffer = a.@buffer
a.delete_at(0)
a.should eq([2, 3, 4])
a.@offset_to_buffer.should eq(1)
a.@buffer.should eq(buffer + 1)
end

it "deletes use range" do
a = [1, 2, 3]
a.delete_at(1).should eq(2)
Expand Down Expand Up @@ -1173,6 +1191,101 @@ describe "Array" do
a.shift(-1)
end
end

it "shifts one and resizes" do
a = [1, 2, 3, 4]
old_capacity = a.@capacity
a.shift.should eq(1)
a.@offset_to_buffer.should eq(1)
a << 5
a.@capacity.should eq(old_capacity * 2)
a.@offset_to_buffer.should eq(1)
a.size.should eq(4)
a.should eq([2, 3, 4, 5])
end

it "shifts almost all and then avoid resize" do
a = [1, 2, 3, 4]
old_capacity = a.@capacity
(1..3).each do |i|
a.shift.should eq(i)
end
a.@offset_to_buffer.should eq(3)
a << 5
a.@capacity.should eq(old_capacity)
a.@offset_to_buffer.should eq(0)
a.size.should eq(2)
a.should eq([4, 5])
end

it "shifts and then concats Array" do
size = 10_000
a = (1..size).to_a
(size - 1).times do
a.shift
end
a.size.should eq(1)
a.concat((1..size).to_a)
a.size.should eq(size + 1)
a.should eq([size] + (1..size).to_a)
end

it "shifts and then concats Enumerable" do
size = 10_000
a = (1..size).to_a
(size - 1).times do
a.shift
end
a.size.should eq(1)
a.concat((1..size))
a.size.should eq(size + 1)
a.should eq([size] + (1..size).to_a)
end

it "shifts all" do
a = [1, 2, 3, 4]
buffer = a.@buffer
4.times do
a.shift
end
a.size.should eq(0)
a.@offset_to_buffer.should eq(0)
a.@buffer.should eq(buffer)
end

it "shifts all after pop" do
a = [1, 2, 3, 4]
buffer = a.@buffer
a.pop
3.times do
a.shift
end
a.size.should eq(0)
a.@offset_to_buffer.should eq(0)
a.@buffer.should eq(buffer)
end

it "pops after shift" do
a = [1, 2, 3, 4]
buffer = a.@buffer
3.times do
a.shift
end
a.pop
a.size.should eq(0)
a.@offset_to_buffer.should eq(0)
a.@buffer.should eq(buffer)
end

it "shifts all with shift(n)" do
a = [1, 2, 3, 4]
buffer = a.@buffer
a.shift
a.shift(3)
a.size.should eq(0)
a.@offset_to_buffer.should eq(0)
a.@buffer.should eq(buffer)
end
end

describe "shuffle" do
Expand Down Expand Up @@ -1441,6 +1554,22 @@ describe "Array" do
a.should eq [3, 1, 2]
end

it "unshifts one elements three times" do
a = [] of Int32
3.times do |i|
a.unshift(i)
end
a.should eq([2, 1, 0])
end

it "unshifts one element multiple times" do
a = [1, 2]
(3..100).each do |i|
a.unshift(i)
end
a.should eq((3..100).to_a.reverse + [1, 2])
end

it "unshifts multiple elements" do
a = [1, 2]
a.unshift(3, 4).should be(a)
Expand All @@ -1452,6 +1581,26 @@ describe "Array" do
a.unshift(1, 2, 3).should be(a)
a.should eq([1, 2, 3])
end

it "unshifts after shift" do
a = [1, 2, 3, 4]
buffer = a.@buffer
a.shift
a.unshift(10)
a.should eq([10, 2, 3, 4])
a.@offset_to_buffer.should eq(0)
a.@buffer.should eq(buffer)
end

it "unshifts many after many shifts" do
a = [1, 2, 3, 4, 5, 6, 7, 8]
buffer = a.@buffer
3.times { a.shift }
a.unshift(10, 20, 30)
a.should eq([10, 20, 30, 4, 5, 6, 7, 8])
a.@offset_to_buffer.should eq(0)
a.@buffer.should eq(buffer)
end
end

it "does update" do
Expand Down
Loading