Kwargs... and allocations

Why not use singleton structs?

8 Likes

I am not. Thank you! I will check that out.

Why not indeed! Because I had forgotten all about them and unfortunately the first thing I thought of was an abstract type. I like it. I will move over to that. It looks like it may be better. Is there a disadvantage?

Nope, not to my knowledge. This is the pattern that Julia itself uses in many many places (see, e.g., IndexLinear() and Base.HasEltype() and Val(_) instead of Val{_} and more) so you’re on the happy path.

1 Like

Love the Julia community! Thank you all of teaching me 3 or 4 new tricks today!

5 Likes

I do seem to still get allocations, even after switching to singleton structs when I am just using named variables at all in the functions after the semicolon and I don’t use the splats and slerps. Using the settings structure seems to be the only solution that is giving me no allocations. The @check_allocs still gives me 1 dynamic dispatch even on the settings method, which gives no allocations.

1 Like

A singleton struct shouldn’t be worse than a settings struct (think of it as a settings struct with 0 fields). Are you using SingletonStruct (the type)? Be sure you’re using SingletonStruct() (an instance) as your argument, as the type still comes with all the caveats of the types discussed above.