Removes the memory alloc of FocusProvider::GetPointers#8118
Closed
wiwei wants to merge 1 commit into
Closed
Conversation
Contributor
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
RogPodge
approved these changes
Jun 30, 2020
| if (typePointer != null) | ||
| { | ||
| typePointers.Add(typePointer); | ||
| yield return typePointer; |
Contributor
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;
}
}
}
Contributor
Author
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Contributor
|
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 |
Contributor
Author
|
Unfortunately I haven't had time to do the profiling for this (given @keveleigh 's previous comment - I'll close this out until then) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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