-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Removes the memory alloc of FocusProvider::GetPointers #8118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL c# has this kind of lazy evaluation
so cool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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