Skip to content

Commit 2963154

Browse files
authored
Merge pull request #3946 from Railboy/mrtk_gc_alloc_fixes
Removes GC Alloc from various services
2 parents 83e43d3 + 8047529 commit 2963154

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/WindowsMixedRealityDeviceManager.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public WindowsMixedRealityDeviceManager(
4242

4343
#if UNITY_WSA
4444

45+
/// <summary>
46+
/// The max expected sources is two - two controllers and/or two hands.
47+
/// We'll set it to 20 just to be certain we can't run out of sources.
48+
/// </summary>
49+
public const int MaxInteractionSourceStates = 20;
50+
4551
/// <summary>
4652
/// Dictionary to capture all active controllers detected
4753
/// </summary>
@@ -50,7 +56,12 @@ public WindowsMixedRealityDeviceManager(
5056
/// <summary>
5157
/// Cache of the states captured from the Unity InteractionManager for UWP
5258
/// </summary>
53-
InteractionSourceState[] interactionmanagerStates;
59+
InteractionSourceState[] interactionmanagerStates = new InteractionSourceState[MaxInteractionSourceStates];
60+
61+
/// <summary>
62+
/// The number of states captured most recently
63+
/// </summary>
64+
private int numInteractionManagerStates;
5465

5566
/// <summary>
5667
/// The current source state reading for the Unity InteractionManager for UWP
@@ -277,12 +288,12 @@ public override void Enable()
277288
InteractionManager.InteractionSourcePressed += InteractionManager_InteractionSourcePressed;
278289
InteractionManager.InteractionSourceReleased += InteractionManager_InteractionSourceReleased;
279290

280-
interactionmanagerStates = InteractionManager.GetCurrentReading();
291+
numInteractionManagerStates = InteractionManager.GetCurrentReading(interactionmanagerStates);
281292

282293
// Avoids a Unity Editor bug detecting a controller from the previous run during the first frame
283294
#if !UNITY_EDITOR
284295
// NOTE: We update the source state data, in case an app wants to query it on source detected.
285-
for (var i = 0; i < interactionmanagerStates?.Length; i++)
296+
for (var i = 0; i < numInteractionManagerStates; i++)
286297
{
287298
var controller = GetController(interactionmanagerStates[i].source);
288299

@@ -307,9 +318,9 @@ public override void Update()
307318
{
308319
base.Update();
309320

310-
interactionmanagerStates = InteractionManager.GetCurrentReading();
321+
numInteractionManagerStates = InteractionManager.GetCurrentReading(interactionmanagerStates);
311322

312-
for (var i = 0; i < interactionmanagerStates?.Length; i++)
323+
for (var i = 0; i < numInteractionManagerStates; i++)
313324
{
314325
// SourceDetected gets raised when a new controller is detected and, if previously present,
315326
// when OnEnable is called. Do not create a new controller here.

Assets/MixedRealityToolkit.SDK/Features/UX/Scripts/Pointers/DefaultPointerMediator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class DefaultPointerMediator : IMixedRealityPointerMediator
1111
private readonly HashSet<IMixedRealityPointer> farInteractPointers = new HashSet<IMixedRealityPointer>();
1212
private readonly HashSet<IMixedRealityNearPointer> nearInteractPointers = new HashSet<IMixedRealityNearPointer>();
1313
private readonly HashSet<IMixedRealityTeleportPointer> teleportPointers = new HashSet<IMixedRealityTeleportPointer>();
14+
private readonly HashSet<IMixedRealityPointer> unassignedPointers = new HashSet<IMixedRealityPointer>();
1415
private readonly Dictionary<IMixedRealityInputSource, HashSet<IMixedRealityPointer>> pointerByInputSourceParent = new Dictionary<IMixedRealityInputSource, HashSet<IMixedRealityPointer>>();
1516

1617
public void RegisterPointers(IMixedRealityPointer[] pointers)
@@ -91,7 +92,11 @@ public void UpdatePointers()
9192
}
9293

9394
// pointers whose active state has not yet been set this frame
94-
HashSet<IMixedRealityPointer> unassignedPointers = new HashSet<IMixedRealityPointer>(allPointers);
95+
unassignedPointers.Clear();
96+
foreach (IMixedRealityPointer unassignedPointer in allPointers)
97+
{
98+
unassignedPointers.Add(unassignedPointer);
99+
}
95100

96101
// If any pointers are locked, they have priority.
97102
// Deactivate all other pointers that are on that input source

Assets/MixedRealityToolkit.Services/InputSimulation/SimulatedHandDataProvider.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ public class SimulatedHandDataProvider
159159
// Last timestamp when hands were tracked
160160
private long lastSimulatedTimestampLeft = 0;
161161
private long lastSimulatedTimestampRight = 0;
162+
// Cached delegates for hand joint generation
163+
private SimulatedHandData.HandJointDataGenerator generatorLeft;
164+
private SimulatedHandData.HandJointDataGenerator generatorRight;
162165

163166
public SimulatedHandDataProvider(MixedRealityInputSimulationProfile _profile)
164167
{
@@ -185,8 +188,20 @@ public bool UpdateHandData(SimulatedHandData handDataLeft, SimulatedHandData han
185188
// TODO: DateTime.UtcNow can be quite imprecise, better use Stopwatch.GetTimestamp
186189
// https://stackoverflow.com/questions/2143140/c-sharp-datetime-now-precision
187190
long timestamp = DateTime.UtcNow.Ticks;
188-
handDataChanged |= handDataLeft.UpdateWithTimestamp(timestamp, HandStateLeft.IsTracked, HandStateLeft.IsPinching, HandStateLeft.FillCurrentFrame);
189-
handDataChanged |= handDataRight.UpdateWithTimestamp(timestamp, HandStateRight.IsTracked, HandStateRight.IsPinching, HandStateRight.FillCurrentFrame);
191+
192+
// Cache the generator delegates so we don't gc alloc every frame
193+
if (generatorLeft == null)
194+
{
195+
generatorLeft = HandStateLeft.FillCurrentFrame;
196+
}
197+
198+
if (generatorRight == null)
199+
{
200+
generatorRight = HandStateRight.FillCurrentFrame;
201+
}
202+
203+
handDataChanged |= handDataLeft.UpdateWithTimestamp(timestamp, HandStateLeft.IsTracked, HandStateLeft.IsPinching, generatorLeft);
204+
handDataChanged |= handDataRight.UpdateWithTimestamp(timestamp, HandStateRight.IsTracked, HandStateRight.IsPinching, generatorRight);
190205

191206
return handDataChanged;
192207
}

Assets/MixedRealityToolkit/Providers/UnityInput/UnityTouchDeviceManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public UnityTouchDeviceManager(
4141
/// <inheritdoc />
4242
public override void Update()
4343
{
44-
for (var i = 0; i < UInput.touches.Length; i++)
44+
int touchCount = UInput.touchCount;
45+
for (var i = 0; i < touchCount; i++)
4546
{
4647
Touch touch = UInput.touches[i];
4748

0 commit comments

Comments
 (0)