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

backport 1.11: fix _growbeg! unncessary resizing (#56029) #56090

Merged
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
9 changes: 7 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1062,10 +1062,11 @@ function _growbeg!(a::Vector, delta::Integer)
setfield!(a, :ref, @inbounds memoryref(ref, 1 - delta))
else
@noinline (function()
@_terminates_locally_meta
memlen = length(mem)
# since we will allocate the array in the middle of the memory we need at least 2*delta extra space
# the +1 is because I didn't want to have an off by 1 error.
newmemlen = max(overallocation(memlen), len + 2 * delta + 1)
newmemlen = max(overallocation(len), len + 2 * delta + 1)
newoffset = div(newmemlen - newlen, 2) + 1
# If there is extra data after the end of the array we can use that space so long as there is enough
# space at the end that there won't be quadratic behavior with a mix of growth from both ends.
Expand All @@ -1074,10 +1075,14 @@ function _growbeg!(a::Vector, delta::Integer)
if newoffset + newlen < memlen
newoffset = div(memlen - newlen, 2) + 1
newmem = mem
unsafe_copyto!(newmem, newoffset + delta, mem, offset, len)
for j in offset:newoffset+delta-1
@inbounds _unsetindex!(mem, j)
end
else
newmem = array_new_memory(mem, newmemlen)
unsafe_copyto!(newmem, newoffset + delta, mem, offset, len)
end
unsafe_copyto!(newmem, newoffset + delta, mem, offset, len)
setfield!(a, :ref, @inbounds memoryref(newmem, newoffset))
end)()
end
Expand Down
5 changes: 5 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ end
@test vc == [v[1:(i-1)]; 5; v[i:end]]
end
@test_throws BoundsError insert!(v, 5, 5)

# test that data is copied when there is plenty of room to do so
v = empty!(collect(1:100))
pushfirst!(v, 1)
@test length(v.ref.mem) == 100
end

@testset "popat!(::Vector, i, [default])" begin
Expand Down