Skip to content

Commit 3f2138d

Browse files
committed
some moer general cleanup
1 parent de43b7c commit 3f2138d

File tree

3 files changed

+39
-60
lines changed

3 files changed

+39
-60
lines changed

base/array.jl

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -214,46 +214,15 @@ false
214214
isbitsunion(u::Union) = allocatedinline(u)
215215
isbitsunion(x) = false
216216

217-
function _unsetindex!(A::Array{T}, i::Int) where {T}
217+
function _unsetindex!(A::Array, i::Int)
218218
@inline
219219
@boundscheck checkbounds(A, i)
220-
t = @_gc_preserve_begin A
221-
p = Ptr{Ptr{Cvoid}}(pointer(A, i))
222-
if !allocatedinline(T)
223-
Intrinsics.atomic_pointerset(p, C_NULL, :monotonic)
224-
elseif T isa DataType
225-
if !datatype_pointerfree(T)
226-
for j = 1:Core.sizeof(Ptr{Cvoid}):Core.sizeof(T)
227-
Intrinsics.atomic_pointerset(p + j - 1, C_NULL, :monotonic)
228-
end
229-
end
230-
end
231-
@_gc_preserve_end t
220+
@inbounds _unsetindex!(MemoryRef(A.ref, i))
232221
return A
233222
end
234223

235224

236225
# TODO: deprecate this (aligned_sizeof and/or elsize and/or sizeof(Some{T}) are more correct)
237-
"""
238-
Base.bitsunionsize(U::Union) -> Int
239-
240-
For a `Union` of [`isbitstype`](@ref) types, return the size of the largest type; assumes `Base.isbitsunion(U) == true`.
241-
242-
# Examples
243-
```jldoctest
244-
julia> Base.bitsunionsize(Union{Float64, UInt8})
245-
8
246-
247-
julia> Base.bitsunionsize(Union{Float64, UInt8, Int128})
248-
16
249-
```
250-
"""
251-
function bitsunionsize(u::Union)
252-
isinline, sz, _ = uniontype_layout(u)
253-
@assert isinline
254-
return sz
255-
end
256-
257226
elsize(::Type{A}) where {T,A<:Array{T}} = aligned_sizeof(T)
258227
function elsize(::Type{Ptr{T}}) where T
259228
# this only must return something valid for values which satisfy is_valid_intrinsic_elptr(T),
@@ -1144,12 +1113,12 @@ function _growat!(a::Vector, i::Integer, delta::Integer)
11441113
unsafe_copyto!(newref, ref, i)
11451114
a.ref = newref
11461115
for j in i:i+delta-1
1147-
_unsetindex!(a, j)
1116+
@inbounds _unsetindex!(a, j)
11481117
end
11491118
elseif !prefer_start && memlen >= newmemlen
11501119
unsafe_copyto!(mem, offset+delta+i, mem, offset+i, len-i+1)
11511120
for j in i:i+delta-1
1152-
_unsetindex!(a, j)
1121+
@inbounds _unsetindex!(a, j)
11531122
end
11541123
else
11551124
# since we will allocate the array in the middle of the memory we need at least 2*delta extra space
@@ -1170,7 +1139,7 @@ function _deletebeg!(a::Vector, delta::Integer)
11701139
len = length(a)
11711140
0 <= delta <= len || throw(ArgumentError("_deleteat! requires delta in 0:length(a)"))
11721141
for i in 1:delta
1173-
_unsetindex!(a, i)
1142+
@inbounds _unsetindex!(a, i)
11741143
end
11751144
newlen = len - delta
11761145
if newlen != 0 # if newlen==0 we could accidentally index past the memory
@@ -1185,7 +1154,7 @@ function _deleteend!(a::Vector, delta::Integer)
11851154
0 <= delta <= len || throw(ArgumentError("_deleteat! requires delta in 0:length(a)"))
11861155
newlen = len - delta
11871156
for i in newlen+1:len
1188-
_unsetindex!(a, i)
1157+
@inbounds _unsetindex!(a, i)
11891158
end
11901159
a.size = (newlen,)
11911160
return
@@ -1782,17 +1751,19 @@ struct Nowhere; end
17821751
push!(::Nowhere, _) = nothing
17831752
_growend!(::Nowhere, _) = nothing
17841753

1785-
@inline function _push_deleted!(dltd, a::Vector, ind)
1786-
if @inbounds isassigned(a, ind)
1787-
push!(dltd, @inbounds a[ind])
1754+
function _push_deleted!(dltd, a::Vector, ind)
1755+
@_propagate_inbounds_meta
1756+
if isassigned(a, ind)
1757+
push!(dltd, a[ind])
17881758
else
17891759
_growend!(dltd, 1)
17901760
end
17911761
end
17921762

1793-
@inline function _copy_item!(a::Vector, p, q)
1794-
if @inbounds isassigned(a, q)
1795-
@inbounds a[p] = a[q]
1763+
function _copy_item!(a::Vector, p, q)
1764+
@_propagate_inbounds_meta
1765+
if isassigned(a, q)
1766+
a[p] = a[q]
17961767
else
17971768
_unsetindex!(a, p)
17981769
end
@@ -1804,7 +1775,7 @@ function _deleteat!(a::Vector, inds, dltd=Nowhere())
18041775
y === nothing && return a
18051776
(p, s) = y
18061777
checkbounds(a, p)
1807-
_push_deleted!(dltd, a, p)
1778+
@inbounds _push_deleted!(dltd, a, p)
18081779
q = p+1
18091780
while true
18101781
y = iterate(inds, s)
@@ -1818,14 +1789,14 @@ function _deleteat!(a::Vector, inds, dltd=Nowhere())
18181789
end
18191790
end
18201791
while q < i
1821-
_copy_item!(a, p, q)
1792+
@inbounds _copy_item!(a, p, q)
18221793
p += 1; q += 1
18231794
end
1824-
_push_deleted!(dltd, a, i)
1795+
@inbounds _push_deleted!(dltd, a, i)
18251796
q = i+1
18261797
end
18271798
while q <= n
1828-
_copy_item!(a, p, q)
1799+
@inbounds _copy_item!(a, p, q)
18291800
p += 1; q += 1
18301801
end
18311802
_deleteend!(a, n-p+1)
@@ -1838,7 +1809,7 @@ function deleteat!(a::Vector, inds::AbstractVector{Bool})
18381809
length(inds) == n || throw(BoundsError(a, inds))
18391810
p = 1
18401811
for (q, i) in enumerate(inds)
1841-
_copy_item!(a, p, q)
1812+
@inbounds _copy_item!(a, p, q)
18421813
p += !i
18431814
end
18441815
_deleteend!(a, n-p+1)

base/compiler/tfuncs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ end
12951295
# - is_undefref_fieldtype(Int) === false
12961296
# - is_undefref_fieldtype(Union{Int32,Int64}) === false
12971297
function is_undefref_fieldtype(@nospecialize ftyp)
1298-
return !has_free_typevars(ftyp) && !allocatedinline(ftyp)
1298+
return isbitstype(ftyp) || isbitsunion(ftyp)
12991299
end
13001300

13011301
@nospecs function setfield!_tfunc(𝕃::AbstractLattice, o, f, v, order)
@@ -2070,7 +2070,7 @@ function array_type_undefable(@nospecialize(arytype))
20702070
else
20712071
elmtype = memoryref_elemtype(arytype::DataType)
20722072
# TODO: use arraytype layout instead to derive this
2073-
return !(isbitstype(elmtype) || isbitsunion(elmtype))
2073+
return !is_undefref_fieldtype(elmtype)
20742074
end
20752075
end
20762076

base/genericmemory.jl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,27 @@ size(a::GenericMemory, d::Int) =
2626
size(a::GenericMemory, d::Integer) = size(a, convert(d, Int))
2727
size(a::GenericMemory) = (length(a),)
2828

29-
pointer(mem::GenericMemory, i::Int) = unsafe_convert(Ptr{Cvoid}, MemoryRef(mem, i))
30-
function _unsetindex!(A::Memory{T}, i::Int) where {T}
29+
pointer(mem::GenericMemory, i::Int) = (@_propagate_inbounds_meta; unsafe_convert(Ptr{Cvoid}, MemoryRef(mem, i))) # boundschecked, even for i==1
30+
pointer(mem::MemoryRef) = unsafe_convert(Ptr{Cvoid}, mem) # no bounds check, even for empty array
31+
32+
_unsetindex!(A::Memory, i::Int) = (@_propagate_inbounds_meta; _unsetindex!(MemoryRef(A, i)); A)
33+
function _unsetindex!(A::MemoryRef{:not_atomic,T}) where T
3134
@_terminates_locally_meta
35+
@_propagate_inbounds_meta
3236
@inline
33-
@boundscheck checkbounds(A, i)
34-
t = @_gc_preserve_begin A
35-
p = Ptr{Ptr{Cvoid}}(pointer(A, i))
36-
# TODO(jwn): access datatype_layout from A instead
37-
if !allocatedinline(T)
37+
@boundscheck MemoryRef(A, 1)
38+
mem = A.mem
39+
MemT = typeof(mem)
40+
arrayelem = datatype_arrayelem(MemT)
41+
elsz = datatype_layoutsize(MemT)
42+
isboxed = 1; isunion = 2
43+
t = @_gc_preserve_begin mem
44+
p = Ptr{Ptr{Cvoid}}(@inbounds pointer(A))
45+
if arrayelem == isboxed
3846
Intrinsics.atomic_pointerset(p, C_NULL, :monotonic)
39-
elseif T isa DataType
40-
if !datatype_pointerfree(T)
41-
for j = 1:Core.sizeof(Ptr{Cvoid}):Core.sizeof(T)
47+
elseif arrayelem != isunion
48+
if !datatype_pointerfree(T::DataType)
49+
for j = 1:Core.sizeof(Ptr{Cvoid}):elsz
4250
Intrinsics.atomic_pointerset(p + j - 1, C_NULL, :monotonic)
4351
end
4452
end

0 commit comments

Comments
 (0)