Add no-allocation controller retrieval + Controller Support#812
Open
wallstop wants to merge 2 commits intoFacepunch:masterfrom
Open
Add no-allocation controller retrieval + Controller Support#812wallstop wants to merge 2 commits intoFacepunch:masterfrom
wallstop wants to merge 2 commits intoFacepunch:masterfrom
Conversation
Add allocation tests PR feedback Fix compilation bug
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.
The Problem
Currently, controller retrieval through
SteamInput.Controllersallocates memory every time it is called due to having to create the enumerator object to statefully walk throughyield return. Most games will be running this code inside anUpdateloop, to retrieve current controller state and fire input events.The general advice to pretty much all game developers is: "Don't allocate in Update". Doing so will generate huge amounts of garbage, causing the Garbage Collector to kick in over and over, which tanks FPS.
Unity has learned this lesson over the years and has started shipping allocation-free versions of its methods that used to return arrays. See:
The Solution
I've implemented a simple alternative to the
SteamInput.Controllersenumeration, which I've calledSteamInput.GetControllersNoAlloc. The implementation is the same, except that it does not allocate an enumerator, leading to less garbage being generated, which results in an allocation-free method that can safely be called duringUpdateTests
I have simple tests around equivalence, as well as tests for allocation (existing) v no allocation (this approach) in a simplistic manner.
Extra
In order to try to test this the best I could, I needed to add a fix for bug #512, which are public
InitandShutdownmethods. This should also solve #758, which is also solved in #805.