julia icon indicating copy to clipboard operation
julia copied to clipboard

prepend! allocates when X is empty even when there's capacity again

Open dpinol opened this issue 6 months ago • 2 comments

https://github.com/JuliaLang/julia/issues/56029 fixed allocations when inserting at the beginning of a vector even when there was capacity. However, the fix only works with "large" vectors. With small ones (5 or less in my tests), it allocates even if there's capacity (and the capacity is not affected either) Reproducible on julia 1.11.5 & 1.12beta3 & master

const x=Vector{Int}(undef, 5);
empty!(x) 
@time insert!(x, 1, 10)

0.000001 seconds (1 allocation: 96 bytes)

dpinol avatar Jun 04 '25 21:06 dpinol

This isn't fully unintended, but I do see the argument specifically for empty arrays where moving the 0 elements will take pretty little time.

oscardssmith avatar Jun 04 '25 22:06 oscardssmith

This does not reproduce on 1.10.

julia> versioninfo()
Julia Version 1.10.9
Commit 5595d20a287 (2025-03-10 12:51 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (aarch64-linux-gnu)
  CPU: 8 × unknown
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, generic)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  JULIA_EDITOR = code

julia> function f()
           x = Vector{Int}(undef, 5)
           empty!(x)
           insert!(x, 1, 10)
       end
f (generic function with 1 method)

julia> using Chairmarks

julia> @b f
20.904 ns (1 allocs: 96 bytes)
Julia Version 1.11.4
Commit 8561cc3d68d (2025-03-10 11:36 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (aarch64-linux-gnu)
  CPU: 8 × unknown
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, apple-m2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  JULIA_EDITOR = code

julia> function f()
           x = Vector{Int}(undef, 5)
           empty!(x)
           insert!(x, 1, 10)
       end
f (generic function with 1 method)

julia> using Chairmarks

julia> @b f
23.841 ns (3 allocs: 192 bytes)

LilithHafner avatar Jun 09 '25 22:06 LilithHafner

Hi! I'm really interested in contributing to this repository and becoming a part of the Julia project. I'm new to the codebase, but I'm eager to learn and get involved. I'd like to work on this issue—could you please assign it to me if it's still available? It would also be very helpful if you could elaborate a bit more on the problem and guide me on where to begin in the codebase.

Priansh-master avatar Jun 26 '25 05:06 Priansh-master

Hi @Priansh-master ,

Glad to hear you're excited! we do not assign issues; you are free to work on anything you like and submitted PRs in good condition will be reviewed by the community.

A good place to start is to read the guide to contributing --- fyi, please allow me to highlight in that guide that the usage of AI code assistants should be disclosed explicitly --- and then search through the github open issues with label good first issue (which includes this issue) for one that interests you.

usage questions should generally be directed to the discourse or the Slack or Zulip communities, and Github is reserved for technical discussion of specific PRs

adienes avatar Jun 26 '25 12:06 adienes