Replies: 2 comments 1 reply
-
The I also wouldn't touch I know that some KTX modules might be still guilty of this with the overloaded operators, but I'd prefer if we didn't add any utilities that violate the Kotlin guidelines. |
Beta Was this translation helpful? Give feedback.
-
I forgot about that, that's totally true
I can understand that. In a project of mine, I was in need of a tool to add multiple components at once, just for prototyping, so I thought of that and operator overloading is tempting ... But I know now not the way to go, a vararg function can do the same thing. Thanks for the return. |
Beta Was this translation helpful? Give feedback.
-
We already have one operator overloaded, it's += for adding a Component to an Entity.
Let me introduce another :
No we can write :
Maybe we would also want to write :
However, since
+=
operator have the lowest precedence, we have to add Components to a list-like structure before assigning them to the entity, and that can cause performance issues. We need to write these three functions for it to works :I was curious about performances vs adding components one by one. I made three implementations of theses three functions.
One with MutableList :
Another with a self-made implementation of LinkedList :
And another with ArrayList, with an initial capacity
Then i ran some benchmarks :
I really like the
LinkedList
implementation, it's memory efficient since we aren't dealing with a lot of components, and it's not a lot slower than the+=
. The problem is that the order is reversed : the first component before the += will be added lastWhat do you guys think of that ? Is it worth adding or not ?
Beta Was this translation helpful? Give feedback.
All reactions