Skip to content

Commit 2b471d9

Browse files
SimonDarksideJmicrosoft-github-policy-service[bot]keveleigh
authored
Update caches systems references to be actual caches and not an additional 0(n) lookup (#10606)
* Microsoft mandatory file (#10599) Co-authored-by: microsoft-github-policy-service[bot] <77245923+microsoft-github-policy-service[bot]@users.noreply.github.com> * Update caches systems references to be actual caches and not an additional 0n lookup * Updated Reset cache implementations to also clear the individual cache references, as per request Co-authored-by: microsoft-github-policy-service[bot] <77245923+microsoft-github-policy-service[bot]@users.noreply.github.com> Co-authored-by: Kurtis <[email protected]>
1 parent c2098fd commit 2b471d9

File tree

1 file changed

+83
-12
lines changed

1 file changed

+83
-12
lines changed

Assets/MRTK/Core/Utilities/CoreServices.cs

+83-12
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,94 @@ namespace Microsoft.MixedReality.Toolkit
2020
/// </summary>
2121
public static class CoreServices
2222
{
23+
private static IMixedRealityBoundarySystem boundarySystem;
24+
2325
/// <summary>
2426
/// Cached reference to the active instance of the boundary system.
2527
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
2628
/// </summary>
27-
public static IMixedRealityBoundarySystem BoundarySystem => GetService<IMixedRealityBoundarySystem>();
29+
public static IMixedRealityBoundarySystem BoundarySystem => boundarySystem ?? (boundarySystem = GetService<IMixedRealityBoundarySystem>());
30+
31+
private static IMixedRealityCameraSystem cameraSystem;
2832

2933
/// <summary>
3034
/// Cached reference to the active instance of the camera system.
3135
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
3236
/// </summary>
33-
public static IMixedRealityCameraSystem CameraSystem => GetService<IMixedRealityCameraSystem>();
37+
public static IMixedRealityCameraSystem CameraSystem => cameraSystem ?? (cameraSystem = GetService<IMixedRealityCameraSystem>());
38+
39+
private static IMixedRealityDiagnosticsSystem diagnosticsSystem;
3440

3541
/// <summary>
3642
/// Cached reference to the active instance of the diagnostics system.
3743
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
3844
/// </summary>
39-
public static IMixedRealityDiagnosticsSystem DiagnosticsSystem => GetService<IMixedRealityDiagnosticsSystem>();
45+
public static IMixedRealityDiagnosticsSystem DiagnosticsSystem => diagnosticsSystem ?? (diagnosticsSystem = GetService<IMixedRealityDiagnosticsSystem>());
46+
47+
private static IMixedRealityFocusProvider focusProvider;
4048

4149
/// <summary>
4250
/// Cached reference to the active instance of the focus provider.
4351
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
4452
/// </summary>
45-
public static IMixedRealityFocusProvider FocusProvider => GetService<IMixedRealityFocusProvider>();
53+
public static IMixedRealityFocusProvider FocusProvider => focusProvider ?? (focusProvider = GetService<IMixedRealityFocusProvider>());
54+
55+
private static IMixedRealityInputSystem inputSystem;
4656

4757
/// <summary>
4858
/// Cached reference to the active instance of the input system.
4959
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
5060
/// </summary>
51-
public static IMixedRealityInputSystem InputSystem => GetService<IMixedRealityInputSystem>();
61+
public static IMixedRealityInputSystem InputSystem => inputSystem ?? (inputSystem = GetService<IMixedRealityInputSystem>());
62+
63+
private static IMixedRealityRaycastProvider raycastProvider;
5264

5365
/// <summary>
5466
/// Cached reference to the active instance of the raycast provider.
5567
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
5668
/// </summary>
57-
public static IMixedRealityRaycastProvider RaycastProvider => GetService<IMixedRealityRaycastProvider>();
69+
public static IMixedRealityRaycastProvider RaycastProvider => raycastProvider ?? (raycastProvider = GetService<IMixedRealityRaycastProvider>());
70+
71+
private static IMixedRealitySceneSystem sceneSystem;
5872

5973
/// <summary>
6074
/// Cached reference to the active instance of the scene system.
6175
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
6276
/// </summary>
63-
public static IMixedRealitySceneSystem SceneSystem => GetService<IMixedRealitySceneSystem>();
77+
public static IMixedRealitySceneSystem SceneSystem => sceneSystem ?? (sceneSystem = GetService<IMixedRealitySceneSystem>());
78+
79+
private static IMixedRealitySpatialAwarenessSystem spatialAwarenessSystem;
6480

6581
/// <summary>
6682
/// Cached reference to the active instance of the spatial awareness system.
6783
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
6884
/// </summary>
69-
public static IMixedRealitySpatialAwarenessSystem SpatialAwarenessSystem => GetService<IMixedRealitySpatialAwarenessSystem>();
85+
public static IMixedRealitySpatialAwarenessSystem SpatialAwarenessSystem => spatialAwarenessSystem ?? (spatialAwarenessSystem = GetService<IMixedRealitySpatialAwarenessSystem>());
86+
87+
private static IMixedRealityTeleportSystem teleportSystem;
7088

7189
/// <summary>
7290
/// Cached reference to the active instance of the teleport system.
7391
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
7492
/// </summary>
75-
public static IMixedRealityTeleportSystem TeleportSystem => GetService<IMixedRealityTeleportSystem>();
93+
public static IMixedRealityTeleportSystem TeleportSystem => teleportSystem ?? (teleportSystem = GetService<IMixedRealityTeleportSystem>());
7694

7795
/// <summary>
7896
/// Resets all cached system references to null
7997
/// </summary>
80-
public static void ResetCacheReferences() => serviceCache.Clear();
98+
public static void ResetCacheReferences()
99+
{
100+
serviceCache.Clear();
101+
boundarySystem = null;
102+
cameraSystem = null;
103+
diagnosticsSystem = null;
104+
focusProvider = null;
105+
inputSystem = null;
106+
raycastProvider = null;
107+
sceneSystem = null;
108+
spatialAwarenessSystem = null;
109+
teleportSystem = null;
110+
}
81111

82112
/// <summary>
83113
/// Clears the cache of the reference with key of given type if present and applicable
@@ -91,6 +121,7 @@ public static bool ResetCacheReference(Type serviceType)
91121
if (serviceCache.ContainsKey(serviceType))
92122
{
93123
serviceCache.Remove(serviceType);
124+
ResetCacheReferenceFromType(serviceType);
94125
return true;
95126
}
96127
}
@@ -102,6 +133,46 @@ public static bool ResetCacheReference(Type serviceType)
102133
return false;
103134
}
104135

136+
private static void ResetCacheReferenceFromType(Type serviceType)
137+
{
138+
if (typeof(IMixedRealityBoundarySystem).IsAssignableFrom(serviceType))
139+
{
140+
boundarySystem = null;
141+
}
142+
if (typeof(IMixedRealityCameraSystem).IsAssignableFrom(serviceType))
143+
{
144+
cameraSystem = null;
145+
}
146+
if (typeof(IMixedRealityDiagnosticsSystem).IsAssignableFrom(serviceType))
147+
{
148+
diagnosticsSystem = null;
149+
}
150+
if (typeof(IMixedRealityFocusProvider).IsAssignableFrom(serviceType))
151+
{
152+
focusProvider = null;
153+
}
154+
if (typeof(IMixedRealityInputSystem).IsAssignableFrom(serviceType))
155+
{
156+
inputSystem = null;
157+
}
158+
if (typeof(IMixedRealityRaycastProvider).IsAssignableFrom(serviceType))
159+
{
160+
raycastProvider = null;
161+
}
162+
if (typeof(IMixedRealitySceneSystem).IsAssignableFrom(serviceType))
163+
{
164+
sceneSystem = null;
165+
}
166+
if (typeof(IMixedRealitySpatialAwarenessSystem).IsAssignableFrom(serviceType))
167+
{
168+
sceneSystem = null;
169+
}
170+
if (typeof(IMixedRealityTeleportSystem).IsAssignableFrom(serviceType))
171+
{
172+
teleportSystem = null;
173+
}
174+
}
175+
105176
/// <summary>
106177
/// Gets first matching <see cref="Microsoft.MixedReality.Toolkit.Input.IMixedRealityInputDeviceManager"/> or extension thereof for CoreServices.InputSystem
107178
/// </summary>
@@ -147,7 +218,7 @@ public static T GetDataProvider<T>(IMixedRealityService service) where T : IMixe
147218
// We do not want to keep a service around so use WeakReference
148219
private static readonly Dictionary<Type, WeakReference<IMixedRealityService>> serviceCache = new Dictionary<Type, WeakReference<IMixedRealityService>>();
149220

150-
private static T GetService<T>() where T : IMixedRealityService
221+
private static T GetService<T>() where T : IMixedRealityService
151222
{
152223
Type serviceType = typeof(T);
153224

@@ -176,4 +247,4 @@ private static T GetService<T>() where T : IMixedRealityService
176247
return service;
177248
}
178249
}
179-
}
250+
}

0 commit comments

Comments
 (0)