Skip to content

Commit 6f5cefb

Browse files
vtjnashJeffBezanson
authored andcommitted
arrayunset: micro-optimization for performance (#32405)
1 parent b32c1b8 commit 6f5cefb

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

base/array.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ false
175175
isbitsunion(u::Union) = (@_pure_meta; ccall(:jl_array_store_unboxed, Cint, (Any,), u) != Cint(0))
176176
isbitsunion(x) = false
177177

178+
isptrelement(t::Type) = (@_pure_meta; ccall(:jl_array_store_unboxed, Cint, (Any,), t) == Cint(0))
179+
180+
function _unsetindex!(A::Array{T}, i::Int) where {T}
181+
@boundscheck checkbounds(A, i)
182+
if isptrelement(T)
183+
t = @_gc_preserve_begin A
184+
p = Ptr{Ptr{Cvoid}}(pointer(A))
185+
unsafe_store!(p, C_NULL, i)
186+
@_gc_preserve_end t
187+
end
188+
return A
189+
end
190+
191+
178192
"""
179193
Base.bitsunionsize(U::Union)
180194

base/dict.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,8 @@ end
620620

621621
function _delete!(h::Dict{K,V}, index) where {K,V}
622622
@inbounds h.slots[index] = 0x2
623-
isbitstype(K) || isbitsunion(K) || ccall(:jl_arrayunset, Cvoid, (Any, UInt), h.keys, index-1)
624-
isbitstype(V) || isbitsunion(V) || ccall(:jl_arrayunset, Cvoid, (Any, UInt), h.vals, index-1)
623+
@inbounds _unsetindex!(h.keys, index)
624+
@inbounds _unsetindex!(h.vals, index)
625625
h.ndel += 1
626626
h.count -= 1
627627
h.age += 1

src/array.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,9 @@ JL_DLLEXPORT void jl_arrayset(jl_array_t *a JL_ROOTING_ARGUMENT, jl_value_t *rhs
592592
JL_DLLEXPORT void jl_arrayunset(jl_array_t *a, size_t i)
593593
{
594594
if (i >= jl_array_len(a))
595-
jl_bounds_error_int((jl_value_t*)a, i+1);
596-
char *ptail = (char*)a->data + i*a->elsize;
595+
jl_bounds_error_int((jl_value_t*)a, i + 1);
597596
if (a->flags.ptrarray)
598-
memset(ptail, 0, a->elsize);
597+
((jl_value_t**)a->data)[i] = NULL;
599598
}
600599

601600
// at this size and bigger, allocate resized array data with malloc

0 commit comments

Comments
 (0)