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 _growbeg! unnecessary resizing #56029

Merged
merged 7 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ function _growbeg!(a::Vector, delta::Integer)
end
# 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 Down
5 changes: 5 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,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