dengkeli
dengkeli
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
the second is significantly faster. and has almost the same functionality (for my purposes)
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
| Method | Mean | Error | StdDev |
|------------------- |----------:|---------:|---------:|
| ComponentsList | 165.50 us | 3.177 us | 2.972 us |
| GetComponentsArray | 45.25 us | 0.311 us | 0.291 us |
| Method | Mean | Error | StdDev |
|------------------- |----------:|---------:|---------:|
| ComponentsList | 165.50 us | 3.177 us | 2.972 us |
| GetComponentsArray | 45.25 us | 0.311 us | 0.291 us |
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
Hate to necro this but I did figure out this much:
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
public List<T> GetComponents<T>() where T : ECSComponent
{
Type componentType = typeof(T);
var componentList = components[componentType];
List<T> newList = new();
foreach (ECSComponent c in componentList)
{
newList.Add((T)c);
}
return newList;
}

public T[] GetComponentsArray<T>() where T : ECSComponent
{
Type componentType = typeof(T);
var componentList = components[componentType];
T[] newList = new T[componentList.Count];
for (int i=0; i<componentList.Count; i++)
{
newList[i] = (T)componentList[i];
}
return newList;
}
public List<T> GetComponents<T>() where T : ECSComponent
{
Type componentType = typeof(T);
var componentList = components[componentType];
List<T> newList = new();
foreach (ECSComponent c in componentList)
{
newList.Add((T)c);
}
return newList;
}

public T[] GetComponentsArray<T>() where T : ECSComponent
{
Type componentType = typeof(T);
var componentList = components[componentType];
T[] newList = new T[componentList.Count];
for (int i=0; i<componentList.Count; i++)
{
newList[i] = (T)componentList[i];
}
return newList;
}
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
The stackoverflow answers are interesting. I think ill noodle around with that a bit
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
I don't know if I'm making sense I'm pretty much a newb
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
I'll take a look at the above. Honestly how it's stored is pretty flexible as long as the interface remains
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
As in under 60fps, at least with what I'm current doing in my update
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
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
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
It's reasonable for what I need I guess. I am just really curious how to solve the problem mainly. c# is very interesting
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
Here is the thing. It's not slow
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
they are type checked at runtime and put into a Dict<Type,List<ECSComponent>>
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
Well, the way its implemented, I really don't see how it could ever get that assumption wrong
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
I might need to dig into other code. I know there are a couple of popular ECSs for c# so there might be something
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
I don't need the random access really, but you said .Cast<T> is just doing the loop.....
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
List<Position> positions = GetComponent<Position>()
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
I'm going the other direction though?
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
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
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
I kinda figured it would be a common problem with List<T> and someone might have the answer in their pocket honestly
28 replies
CC#
Created by dengkeli on 10/4/2023 in #help
❔ Need guidance on generics, List, IEnumerable
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.
28 replies