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

Fix protect Array#map against concurrent mutation #15162

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
148 changes: 114 additions & 34 deletions spec/std/array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -979,16 +979,122 @@ describe "Array" do
end
end

it "does map" do
a = [1, 2, 3]
a.map { |x| x * 2 }.should eq([2, 4, 6])
a.should eq([1, 2, 3])
describe "#map_with_index" do
it "does map_with_index" do
ary = [1, 1, 2, 2]
ary2 = ary.map_with_index { |e, i| e + i }
ary2.should eq([1, 2, 4, 5])
end

it "does map_with_index, with offset" do
ary = [1, 1, 2, 2]
ary2 = ary.map_with_index(10) { |e, i| e + i }
ary2.should eq([11, 12, 14, 15])
end

it "doesn't break on concurrent mutation (pop)" do
ary = [1, 2, 3]

ary.map_with_index do |e, i|
ary.pop if e == 1
{e, i}
end.should eq [{1, 0}, {2, 1}]
end

it "doesn't break on concurrent mutation (push)" do
ary = [1, 2, 3]

mapped = ary.map_with_index do |e, i|
ary.push 4 if e == 1
{e, i}
end
mapped.should eq [{1, 0}, {2, 1}, {3, 2}, {4, 3}]
end
end

it "does map!" do
a = [1, 2, 3]
a.map! { |x| x * 2 }
a.should eq([2, 4, 6])
describe "#map" do
it "does map" do
a = [1, 2, 3]
a.map { |x| x * 2 }.should eq([2, 4, 6])
a.should eq([1, 2, 3])
end

it "doesn't break on concurrent mutation (pop)" do
ary = [1, 2, 3]

ary.map do |e|
ary.pop if e == 1
e
end.should eq [1, 2]
end

it "doesn't break on concurrent mutation (push)" do
ary = [1, 2, 3]
ary.map do |e|
ary.push 4 if e == 1
e
end.should eq [1, 2, 3, 4]
end
end

describe "#map_with_index!" do
it "does map_with_index!" do
ary = [0, 1, 2]
ary2 = ary.map_with_index! { |e, i| i * 2 }
ary.should eq([0, 2, 4])
ary2.should be(ary)
end

it "does map_with_index!, with offset" do
ary = [0, 1, 2]
ary2 = ary.map_with_index!(10) { |e, i| i * 2 }
ary.should eq([20, 22, 24])
ary2.should be(ary)
end

it "doesn't break on concurrent mutation (pop)" do
ary = [1, 2, 3]

ary.map_with_index! do |e, i|
ary.pop if e == 1
e
end.should eq [1, 2]
end

it "doesn't break on concurrent mutation (push)" do
ary = [1, 2, 3]

ary.map_with_index! do |e, i|
ary.push 4 if e == 1
e
end.should eq [1, 2, 3, 4]
end
end

describe "#map!" do
it "does map!" do
a = [1, 2, 3]
a.map! { |x| x * 2 }
a.should eq([2, 4, 6])
end

it "doesn't break on concurrent mutation (pop)" do
ary = [1, 2, 3]

ary.map! do |e|
ary.pop if e == 1
e
end.should eq [1,2 ]
end

it "doesn't break on concurrent mutation (push)" do
ary = [1, 2, 3]

ary.map! do |e|
ary.push 4 if e == 1
e
end.should eq [1, 2, 3, 4]
end
end

describe "pop" do
Expand Down Expand Up @@ -1919,32 +2025,6 @@ describe "Array" do
ary2.should be(ary1)
end

it "does map_with_index" do
ary = [1, 1, 2, 2]
ary2 = ary.map_with_index { |e, i| e + i }
ary2.should eq([1, 2, 4, 5])
end

it "does map_with_index, with offset" do
ary = [1, 1, 2, 2]
ary2 = ary.map_with_index(10) { |e, i| e + i }
ary2.should eq([11, 12, 14, 15])
end

it "does map_with_index!" do
ary = [0, 1, 2]
ary2 = ary.map_with_index! { |e, i| i * 2 }
ary.should eq([0, 2, 4])
ary2.should be(ary)
end

it "does map_with_index!, with offset" do
ary = [0, 1, 2]
ary2 = ary.map_with_index!(10) { |e, i| i * 2 }
ary.should eq([20, 22, 24])
ary2.should be(ary)
end

it "does + with different types (#568)" do
a = [1, 2, 3]
a += ["hello"]
Expand Down
10 changes: 7 additions & 3 deletions src/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,9 @@ class Array(T)

# Optimized version of `Enumerable#map`.
def map(& : T -> U) : Array(U) forall U
Array(U).new(size) { |i| yield @buffer[i] }
map_with_index do |item, _|
yield item
end
end

# Modifies `self`, keeping only the elements in the collection for which the
Expand Down Expand Up @@ -1215,8 +1217,10 @@ class Array(T)
# results = gems.map_with_index { |gem, i| "#{i}: #{gem}" }
# results # => ["0: crystal", "1: pearl", "2: diamond"]
# ```
def map_with_index(offset = 0, & : T, Int32 -> _)
Array.new(size) { |i| yield @buffer[i], offset + i }
def map_with_index(offset = 0, & : T, Int32 -> U) forall U
ary = Array(U).new(size)
each_with_index(offset) { |e, i| ary << yield e, i }
ary
end

# Returns an `Array` with the first *count* elements removed
Expand Down
Loading