From 74b212fb6d66f80e59aaca3db0ef232b101ff8fe Mon Sep 17 00:00:00 2001 From: Daniel Wennberg Date: Tue, 1 Oct 2024 00:32:40 -0700 Subject: [PATCH] Add fast path for complete mutable structs --- src/make_zero.jl | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/make_zero.jl b/src/make_zero.jl index 90daf6aa08..0b0d8ffe82 100644 --- a/src/make_zero.jl +++ b/src/make_zero.jl @@ -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)