Skip to content

Commit

Permalink
make _unsetindex fast for isbits eltype (#56364)
Browse files Browse the repository at this point in the history
fixes
#56359 (comment)
```
using Plots

function f(n)
    a = Vector{Int}(undef, n)
    s = time_ns()
    resize!(a, 8)
    time_ns() - s
end

x = 8:10:1000000
y = f.(x)

plot(x, y)
```

![image](https://github.com/user-attachments/assets/5a1fb963-7d44-4cac-bedd-6f0733d4cf56)

(cherry picked from commit e802eff)
  • Loading branch information
oscardssmith authored and KristofferC committed Oct 29, 2024
1 parent 92405f2 commit 05182dc
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions base/genericmemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,16 @@ function _unsetindex!(A::MemoryRef{T}) where T
MemT = typeof(mem)
arrayelem = datatype_arrayelem(MemT)
elsz = datatype_layoutsize(MemT)
isboxed = 1; isunion = 2
isbits = 0; isboxed = 1; isunion = 2
arrayelem == isbits && datatype_pointerfree(T::DataType) && return A
t = @_gc_preserve_begin mem
p = Ptr{Ptr{Cvoid}}(@inbounds pointer(A))
if arrayelem == isboxed
Intrinsics.atomic_pointerset(p, C_NULL, :monotonic)
elseif arrayelem != isunion
if !datatype_pointerfree(T::DataType)
for j = 1:Core.sizeof(Ptr{Cvoid}):elsz
# XXX: this violates memory ordering, since it writes more than one C_NULL to each
Intrinsics.atomic_pointerset(p + j - 1, C_NULL, :monotonic)
end
for j = 1:Core.sizeof(Ptr{Cvoid}):elsz
# XXX: this violates memory ordering, since it writes more than one C_NULL to each
Intrinsics.atomic_pointerset(p + j - 1, C_NULL, :monotonic)
end
end
@_gc_preserve_end t
Expand Down

0 comments on commit 05182dc

Please sign in to comment.