Removes the memory alloc of FocusProvider::GetPointers#8118
Conversation
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
| if (typePointer != null) | ||
| { | ||
| typePointers.Add(typePointer); | ||
| yield return typePointer; |
There was a problem hiding this comment.
TIL c# has this kind of lazy evaluation
so cool
There was a problem hiding this comment.
@RogPodge from C# 7 you can even do this:
/// <inheritdoc />
public IEnumerable<T> GetPointers<T>() where T : class, IMixedRealityPointer
{
foreach (var pointer in pointers.Values)
{
if (pointer.Pointer as T typePointer)
{
yield return typePointer;
}
}
}
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
|
This is an older article (2016) and I've been meaning to retest their method, but it seems like this change might trade garbage for a perf cost: https://jacksondunstan.com/articles/3598 |
Garbage will eventually result in a performance cost, but not as predictable. This would be good to test in an app with a typical |
|
Unfortunately I haven't had time to do the profiling for this (given @keveleigh 's previous comment - I'll close this out until then) |
Overview
GetPointers currently returns an IEnumerable backed by a List, which allocs memory for each call to it. While we could have 'saved' on this by caching a result for a particular type T on a per-Update loop basis, this would also end up making things more complex than necessary (i.e. saving cached state, clearing it, making it possible for us to hit bugs there).
Since the function is already an IEnumerable, we can just use yield return typePointer to avoid allocing a return structure (thanks @Alexees for the solution here)
We use this in a bunch of non-test and test locations, so this is verified just with existing tests that pass.
Changes