Skip to content

Commit

Permalink
fix _growbeg! unncessary resizing (JuliaLang#56029)
Browse files Browse the repository at this point in the history
This was very explicitly designed such that if there was a bunch of
extra space at the end of the array, we would copy rather than
allocating, but by making `newmemlen` be at least
`overallocation(memlen)` rather than `overallocation(len)`, this branch
was never hit. found by JuliaLang#56026
  • Loading branch information
oscardssmith authored and Dani Pinyol committed Oct 10, 2024
1 parent 0de7b6c commit d00fbe2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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

0 comments on commit d00fbe2

Please sign in to comment.