❔ Need guidance on generics, List, IEnumerable
So I have an implemented ECS in a game I'm working on. Admittedly I'm not the most seasoned c# programmer, but I believe I have gotten it to a state where it's usable and reasonably fast to do "queries" on. My question has to do with Returning generic lists. So with the following code has really been bugging me for a while:
This loop obviously sucks, but as I understand it and for good reason a cast here would be casting the list not the items and that's not possible. Cool. How about:
This seems better at a glace, but it seemed lest performant with large sets than just a foreach loop in the profiler. So I'm thinking "I don't need any of the List functionality of List on a return of this function I just need to be able to iterate and access the data. So why not return Just the IEnumerable.
Seems even better right?
11 Replies
My question is: Which is more performant? I'm not competent enough to understand everything happening here, but I do know adding the constraint and doing an explicit cast (T) in the first example seemed orders of magnitude faster. I'm just not fully aware of the consequences of Cast<T> and ToList, and I don't really have a goof enough understanding to profile them myself. Thanks much.
$benchmark
Try BenchmarkDotNet if you want a sophisticated library for benchmarking https://github.com/dotnet/BenchmarkDotNet
Benchmarking is really the only real answer here
I kinda figured it would be a common problem with List<T> and someone might have the answer in their pocket honestly
I was unable to find anything satisfying with google-fu. I guess I need to learn how to instrument it myself and figure it out
Well, you can see what
.Cast<T>()
does: https://source.dot.net/#System.Linq/System/Linq/Cast.cs,27bb217a6d5457ecZeth
REPL Result: Success
Compile: 445.611ms | Execution: 32.178ms | React with ❌ to remove this embed.
I'm going the other direction though?
List<Position> positions = GetComponent<Position>()
I don't need the random access really, but you said .Cast<T> is just doing the loop.....
I might need to dig into other code. I know there are a couple of popular ECSs for c# so there might be something
Well, the way its implemented, I really don't see how it could ever get that assumption wrong
they are type checked at runtime and put into a Dict<Type,List<ECSComponent>>
Here is the thing. It's not slow
It's reasonable for what I need I guess. I am just really curious how to solve the problem mainly. c# is very interesting
Well honestly the normal design for perf favors the storage by like type. Honestly I just like the pattern, not so much about perf. But it's kinda limited to about 5 k items before it cant be used in frame time
As in under 60fps, at least with what I'm current doing in my update
I'll take a look at the above. Honestly how it's stored is pretty flexible as long as the interface remains
I don't know if I'm making sense I'm pretty much a newb
The stackoverflow answers are interesting. I think ill noodle around with that a bit
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.
Hate to necro this but I did figure out this much:
the second is significantly faster. and has almost the same functionality (for my purposes)
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.