Spurious allocations with @threads, when setting variables using if statements

julia> @code_warntype g!(y, flag, dx, dy)
MethodInstance for g!(::Vector{ComplexF64}, ::Bool, ::Float64, ::Float64)
  from g!(f::AbstractVector, flag::Bool, dx::Float64, dy::Float64) in Main at /tmp/foo.jl:51
Arguments
  #self#::Core.Const(g!)
  f::Vector{ComplexF64}
  flag::Bool
  dx::Float64
  dy::Float64
Locals
  threadsfor_fun::var"#150#threadsfor_fun#19"{var"#150#threadsfor_fun#18#20"{Vector{ComplexF64}, Base.OneTo{Int64}}}
  Δ::Core.Box

Δ is boxed. Δ = if flag dy else dx end matches more closely your ternary operator and with this change performance is identical for me for g! and g_ternary!