4 ms·
Assuming your generic functions take _pointers_ to Components as input, full monomorphization does not occur and you're suffering a performance hit similar in m
by siftrics 5y ago
Assuming your generic functions take _pointers_ to Components as input, full monomorphization does not occur and you're suffering a performance hit similar in magnitude, if not strictly greater empirically, to interface "dereferences".
On this basis, I don't believe your generic implementation is as faster than an interface implementation as you claim.
- pphysch 5y agoYou're right, here's what one of my hot loops look like: func (cc *ComponentContainer[T]) ForEach(f func(*Component[T])) { for _, page := range cc.pool.pages { for i := range page { if page[i].IsActive() { f(&page[i]) } } } } Still, the interface approach is a total nightmare from a readability + runtime error perspective so I won't be going back & will just hope for some performance freebies in 1.19 or later :^)
- Groxx 5y agoJust to clarify things here: a pointer receiver on a generic type isn't what was covered in the article. That was covering if your `T` is a pointer type. If T here is a value-type, you're probably getting monomorphization (at the cost of copying the value all over, and possible benefit of inlining to reduce that copying).
- pphysch 5y agoThanks, that makes more sense! My components are all small structs (coordinates, states, health values, etc) so I am happy to copy them around Component[T] is something like { Metadata_fields... Data T }
- deleted 5y ago[deleted]