Unity-Reorderable-List icon indicating copy to clipboard operation
Unity-Reorderable-List copied to clipboard

Expose the List<T>.Enumerator struct or similar

Open johanolofsson opened this issue 3 years ago • 5 comments

C# uses whatever public GetEnumerator() is available in foreach. That's why List<T> and Array have versions which returns a struct instead of IEnumerator<T>.

Only having IEnumerator<T> GetEnumerator() causes vcalls and boxing, or worst case allocation if you have a class that implements the interface. Easiest and most performant solution would be to just implement the existing one explicitly and expose the List<T>.Enumerator in the public. Another solution would be to write your own but then that would need to wrap the list enumerator causing unnecessary copying or use the indexing operator of the internal list and that is not desirable.

johanolofsson avatar Mar 25 '21 10:03 johanolofsson

One problem with exposing List.Enumerator is that you are going to break ABI compatibility if you switched the underlying collection.

Therzok avatar Mar 25 '21 12:03 Therzok

That is true, but is also quite unlikely to happen? And even if the impl. would change it is even more unlikely that anyone will ever declare a variable of this explicit type. i.e. List < XYZ >.Enumerator e = stuff.GetEnumerator(); It is most likely to nearly always be used in foreach loops or if explicitly stored in a variable then by using var e = ...; so for the sake of performance which is paramount in game dev it is imho a risk worth considering to take.

johanolofsson avatar Mar 25 '21 12:03 johanolofsson

Oh, yeah, no, I completely agree. I was just putting out the possible ABI break so a custom enumerator struct is used that is specific to this type.

That way ABI compat issues can be avoided alltogether by having ReorderableList.Enumerator wrap the underlying list enumerator.

So you can modify implementation details in the future without breaking ABI.

Therzok avatar Mar 26 '21 01:03 Therzok

Yes, that would be a sufficient solution, albeit with a small overhead in non optimized scenarios, but still much better than using the IEnumerator. Can we get a comment from the author on this? Should be a 10 min fix.

johanolofsson avatar Mar 26 '21 08:03 johanolofsson

Hey, I'm super busy with another project right now. If you could add a pull request with the change, I'll approve it

cfoulston avatar Apr 06 '21 19:04 cfoulston