# Unroll setfield!

**URL:** <https://discourse.julialang.org/t/unroll-setfield/122545>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [November 12, 2024, 1:49pm UTC](https://discourse.julialang.org/t/unroll-setfield/122545 "2024-11-12T13:49:19Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [November 12, 2024, 2:12pm UTC](https://discourse.julialang.org/t/unroll-setfield/122545/3 "2024-11-12T14:12:31Z")

</div>

So there’s a few things going on here. First off, your `setup_data_manual!` isn’t actually using `field_names` or `field_types`, and if it did, you’d find there’s also a performance problem with your `manual` version since julia is having problems constant propagating the field name symbols, and it’s also suffering from the awful problem that `Tuple`s erase type info when given types:

```julia-repl
julia> field_types=(Int32, Float32, String)
(Int32, Float32, String)

julia> typeof(field_types)
Tuple{DataType, DataType, DataType}

```

🤮

I think the best way to hand this is with tuple recursion, and passing some info to the type domain:

```julia
unwrap(::Val{T}) where {T} = T

function setup_data_recur!(data, field_names, field_values, field_types) 
    name, names... = field_names
    value, values... = field_values
    type, types... = field_types
    setfield!(data, unwrap(name), convert(unwrap(type), value)) # Note the use of unwrap here
    setup_data_recur!(data, names, values, types)
end

# The base-case
setup_data_recur!(data, ::Tuple{}, ::Tuple{}, ::Tuple{}) = data

field_names_val = (Val(:a), Val(:b), Val(:c))
field_types_val = (Val(Int32), Val(Float32), Val(String))

```

This gives me

```julia-repl
julia> @btime setup_data_recur!($(Data32()), $field_names_val, $field_values, $field_types_val)
  2.484 ns (0 allocations: 0 bytes)
Data32(1, 1.0f0, "X", 0, 0.0f0)

```

---

_[View the full topic](https://discourse.julialang.org/t/unroll-setfield/122545)._
