Skip to content

Commit 4d9b107

Browse files
authored
Merge pull request #23586 from JuliaLang/jq/bitsunionsperf
Speed up isbitsunion and bitsunionsize functions
2 parents 7e7300e + e05bd6b commit 4d9b107

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

base/array.jl

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,19 @@ asize_from(a::Array, n) = n > ndims(a) ? () : (arraysize(a,n), asize_from(a, n+1
114114
115115
Return whether a type is an "is-bits" Union type, meaning each type included in a Union is `isbits`.
116116
"""
117-
function isbitsunion end
118-
119-
function isbitsunion(U::Union)
120-
for u in Base.uniontypes(U)
121-
isbits(u) || return false
122-
end
123-
return true
124-
end
125-
isbitsunion(T) = false
117+
isbitsunion(u::Union) = ccall(:jl_array_store_unboxed, Cint, (Any,), u) == Cint(1)
118+
isbitsunion(x) = false
126119

127120
"""
128121
Base.bitsunionsize(U::Union)
129122
130-
For a Union of `isbits` types, return the size of the largest type.
123+
For a Union of `isbits` types, return the size of the largest type; assumes `Base.isbitsunion(U) == true`
131124
"""
132-
function bitsunionsize(U::Union)
133-
sz = 0
134-
for u in Base.uniontypes(U)
135-
sz = max(sz, sizeof(u))
136-
end
137-
return sz
125+
function bitsunionsize(u::Union)
126+
sz = Ref{Csize_t}(0)
127+
algn = Ref{Csize_t}(0)
128+
@assert ccall(:jl_islayout_inline, Cint, (Any, Ptr{Csize_t}, Ptr{Csize_t}), u, sz, algn) == Cint(1)
129+
return sz[]
138130
end
139131

140132
length(a::Array) = arraylen(a)

0 commit comments

Comments
 (0)