Skip to content

Commit

Permalink
Add fast path for when all struct fields are set
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwe committed Sep 25, 2024
1 parent 4f5b202 commit 545bf9b
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1970,19 +1970,31 @@ end
return y
end

flds = Vector{Any}(undef, nf)
for i in 1:nf
if isdefined(prevs, i)

y = if all(p -> isdefined(p, nf), prevs)
# fast path when all fields are set
splatnew(RT, ntuple(Val(nf)) do i
Base.@_inline_meta
xis = ntuple(j -> getfield(prevs[j], i), N)
T = Core.Typeof(first(xis))
yi = recursive_map(T, f, seen, xis, basetype)
flds[i] = yi
else
nf = i - 1 # rest of tail must be undefined values
break
recursive_map(T, f, seen, xis, basetype)
end)
else
flds = Vector{Any}(undef, nf)
nset = nf
for i in 1:nf
if all(p -> isdefined(p, i), prevs)
xis = ntuple(j -> getfield(prevs[j], i), N)
T = Core.Typeof(first(xis))
yi = recursive_map(T, f, seen, xis, basetype)
flds[i] = yi
else
nset = i - 1 # rest of tail must be undefined values
break
end
end
ccall(:jl_new_structv, Any, (Any, Ptr{Any}, UInt32), RT, flds, nset)
end
y = ccall(:jl_new_structv, Any, (Any, Ptr{Any}, UInt32), RT, flds, nf)
seen[prevs] = y
return y
end
Expand Down

0 comments on commit 545bf9b

Please sign in to comment.