Skip to content

Commit

Permalink
Add fast path for complete mutable structs
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwe committed Oct 1, 2024
1 parent 5c770e8 commit 74b212f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/make_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,23 @@ end
nf = fieldcount(RT)
x1 = first(xs)
if ismutabletype(RT)
y = ccall(:jl_new_struct_uninit, Any, (Any,), RT)
for i in 1:nf
if isdefined(x1, i)
yi = newyi(i)
if Base.isconst(RT, i)
ccall(:jl_set_nth_field, Cvoid, (Any, Csize_t, Any), y, i - 1, yi)
else
setfield!(y, i, yi)
if all(i -> isdefined(x1, i), 1:nf)
# fast path when all fields are set
return splatnew(RT, ntuple(newyi, Val(nf)))
else
y = ccall(:jl_new_struct_uninit, Any, (Any,), RT)
for i in 1:nf
if isdefined(x1, i)
yi = newyi(i)
if Base.isconst(RT, i)
ccall(:jl_set_nth_field, Cvoid, (Any, Csize_t, Any), y, i - 1, yi)
else
setfield!(y, i, yi)
end
end
end
return y
end
return y
elseif nf == 0
return f(xs...)::RT
elseif isdefined(x1, nf)
Expand Down

0 comments on commit 74b212f

Please sign in to comment.