Skip to content

Commit e8d0a68

Browse files
check through Profiler.BeginSample markers for early returns (uum-49019) (#1774)
* check through Profiler.BeginSample markers for early returns (uum-49019) * added changelog comment * try to use a finally block to make sure this profiler marker ends * attempt to fix compile error * actually tested it this time (sorry Paulius!)
1 parent bdd9937 commit e8d0a68

File tree

4 files changed

+75
-52
lines changed

4 files changed

+75
-52
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ however, it has to be formatted properly to pass verification tests.
3333
- Fixed case [ISXB-580](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-580) (UI Submit / Cancel not working with Switch Pro controller) by adding "Submit" & "Cancel" usages to the Switch Pro controller input controls.
3434
- Fixed an issue where undoing deletion of Action Maps did not restore Actions correctly.
3535
- Fixed case [ISXB-628](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-628) (OnIMECompositionChange does not return an empty string on accept when using Microsoft IME) by clarifying expectations and intended usage for the IME composition change event.
36+
- Fixed case [ISX-1668] (The Profiler shows incorrect data and spams the console with "Missing Profiler.EndSample" errors when there is an Input System Component in Scene).
3637

3738
## [1.8.0-pre.1] - 2023-09-04
3839

Packages/com.unity.inputsystem/InputSystem/Events/InputEventTrace.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,10 @@ private void OnInputEvent(InputEventPtr inputEvent, InputDevice device)
836836
newBufferSize = m_MaxEventBufferSize;
837837

838838
if (newBufferSize < bytesNeeded)
839+
{
840+
Profiler.EndSample();
839841
return;
842+
}
840843

841844
Resize(newBufferSize);
842845

Packages/com.unity.inputsystem/InputSystem/InputManager.cs

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -845,69 +845,75 @@ public InputControlLayout TryLoadControlLayout(InternedString name)
845845
////FIXME: allowing the description to be modified as part of this is surprising; find a better way
846846
public InternedString TryFindMatchingControlLayout(ref InputDeviceDescription deviceDescription, int deviceId = InputDevice.InvalidDeviceId)
847847
{
848-
Profiler.BeginSample("InputSystem.TryFindMatchingControlLayout");
849-
////TODO: this will want to take overrides into account
850-
851-
// See if we can match by description.
852-
var layoutName = m_Layouts.TryFindMatchingLayout(deviceDescription);
853-
if (layoutName.IsEmpty())
848+
InternedString layoutName = new InternedString(string.Empty);
849+
try
854850
{
855-
// No, so try to match by device class. If we have a "Gamepad" layout,
856-
// for example, a device that classifies itself as a "Gamepad" will match
857-
// that layout.
858-
//
859-
// NOTE: Have to make sure here that we get a device layout and not a
860-
// control layout.
861-
if (!string.IsNullOrEmpty(deviceDescription.deviceClass))
851+
Profiler.BeginSample("InputSystem.TryFindMatchingControlLayout");
852+
////TODO: this will want to take overrides into account
853+
854+
// See if we can match by description.
855+
layoutName = m_Layouts.TryFindMatchingLayout(deviceDescription);
856+
if (layoutName.IsEmpty())
862857
{
863-
var deviceClassLowerCase = new InternedString(deviceDescription.deviceClass);
864-
var type = m_Layouts.GetControlTypeForLayout(deviceClassLowerCase);
865-
if (type != null && typeof(InputDevice).IsAssignableFrom(type))
866-
layoutName = new InternedString(deviceDescription.deviceClass);
858+
// No, so try to match by device class. If we have a "Gamepad" layout,
859+
// for example, a device that classifies itself as a "Gamepad" will match
860+
// that layout.
861+
//
862+
// NOTE: Have to make sure here that we get a device layout and not a
863+
// control layout.
864+
if (!string.IsNullOrEmpty(deviceDescription.deviceClass))
865+
{
866+
var deviceClassLowerCase = new InternedString(deviceDescription.deviceClass);
867+
var type = m_Layouts.GetControlTypeForLayout(deviceClassLowerCase);
868+
if (type != null && typeof(InputDevice).IsAssignableFrom(type))
869+
layoutName = new InternedString(deviceDescription.deviceClass);
870+
}
867871
}
868-
}
869872

870-
////REVIEW: listeners registering new layouts from in here may potentially lead to the creation of devices; should we disallow that?
871-
////REVIEW: if a callback picks a layout, should we re-run through the list of callbacks? or should we just remove haveOverridenLayoutName?
872-
// Give listeners a shot to select/create a layout.
873-
if (m_DeviceFindLayoutCallbacks.length > 0)
874-
{
875-
// First time we get here, put our delegate for executing device commands
876-
// in place. We wrap the call to IInputRuntime.DeviceCommand so that we don't
877-
// need to expose the runtime to the onFindLayoutForDevice callbacks.
878-
if (m_DeviceFindExecuteCommandDelegate == null)
879-
m_DeviceFindExecuteCommandDelegate =
880-
(ref InputDeviceCommand commandRef) =>
881-
{
882-
if (m_DeviceFindExecuteCommandDeviceId == InputDevice.InvalidDeviceId)
883-
return InputDeviceCommand.GenericFailure;
884-
return m_Runtime.DeviceCommand(m_DeviceFindExecuteCommandDeviceId, ref commandRef);
885-
};
886-
m_DeviceFindExecuteCommandDeviceId = deviceId;
887-
888-
var haveOverriddenLayoutName = false;
889-
m_DeviceFindLayoutCallbacks.LockForChanges();
890-
for (var i = 0; i < m_DeviceFindLayoutCallbacks.length; ++i)
873+
////REVIEW: listeners registering new layouts from in here may potentially lead to the creation of devices; should we disallow that?
874+
////REVIEW: if a callback picks a layout, should we re-run through the list of callbacks? or should we just remove haveOverridenLayoutName?
875+
// Give listeners a shot to select/create a layout.
876+
if (m_DeviceFindLayoutCallbacks.length > 0)
891877
{
892-
try
878+
// First time we get here, put our delegate for executing device commands
879+
// in place. We wrap the call to IInputRuntime.DeviceCommand so that we don't
880+
// need to expose the runtime to the onFindLayoutForDevice callbacks.
881+
if (m_DeviceFindExecuteCommandDelegate == null)
882+
m_DeviceFindExecuteCommandDelegate =
883+
(ref InputDeviceCommand commandRef) =>
884+
{
885+
if (m_DeviceFindExecuteCommandDeviceId == InputDevice.InvalidDeviceId)
886+
return InputDeviceCommand.GenericFailure;
887+
return m_Runtime.DeviceCommand(m_DeviceFindExecuteCommandDeviceId, ref commandRef);
888+
};
889+
m_DeviceFindExecuteCommandDeviceId = deviceId;
890+
891+
var haveOverriddenLayoutName = false;
892+
m_DeviceFindLayoutCallbacks.LockForChanges();
893+
for (var i = 0; i < m_DeviceFindLayoutCallbacks.length; ++i)
893894
{
894-
var newLayout = m_DeviceFindLayoutCallbacks[i](ref deviceDescription, layoutName, m_DeviceFindExecuteCommandDelegate);
895-
if (!string.IsNullOrEmpty(newLayout) && !haveOverriddenLayoutName)
895+
try
896896
{
897-
layoutName = new InternedString(newLayout);
898-
haveOverriddenLayoutName = true;
897+
var newLayout = m_DeviceFindLayoutCallbacks[i](ref deviceDescription, layoutName, m_DeviceFindExecuteCommandDelegate);
898+
if (!string.IsNullOrEmpty(newLayout) && !haveOverriddenLayoutName)
899+
{
900+
layoutName = new InternedString(newLayout);
901+
haveOverriddenLayoutName = true;
902+
}
903+
}
904+
catch (Exception exception)
905+
{
906+
Debug.LogError($"{exception.GetType().Name} while executing 'InputSystem.onFindLayoutForDevice' callbacks");
907+
Debug.LogException(exception);
899908
}
900909
}
901-
catch (Exception exception)
902-
{
903-
Debug.LogError($"{exception.GetType().Name} while executing 'InputSystem.onFindLayoutForDevice' callbacks");
904-
Debug.LogException(exception);
905-
}
910+
m_DeviceFindLayoutCallbacks.UnlockForChanges();
906911
}
907-
m_DeviceFindLayoutCallbacks.UnlockForChanges();
908912
}
909-
910-
Profiler.EndSample();
913+
finally
914+
{
915+
Profiler.EndSample();
916+
}
911917
return layoutName;
912918
}
913919

@@ -1259,7 +1265,10 @@ public InputDevice AddDevice(InputDeviceDescription description, bool throwIfNoL
12591265
if (layout.IsEmpty())
12601266
{
12611267
if (throwIfNoLayoutFound)
1268+
{
1269+
Profiler.EndSample();
12621270
throw new ArgumentException($"Cannot find layout matching device description '{description}'", nameof(description));
1271+
}
12631272

12641273
// If it's a device coming from the runtime, disable it.
12651274
if (deviceId != InputDevice.InvalidDeviceId)
@@ -2844,7 +2853,10 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
28442853
Profiler.BeginSample("InputUpdate");
28452854

28462855
if (m_InputEventStream.isOpen)
2856+
{
2857+
Profiler.EndSample();
28472858
throw new InvalidOperationException("Already have an event buffer set! Was OnUpdate() called recursively?");
2859+
}
28482860

28492861
// Restore devices before checking update mask. See InputSystem.RunInitialUpdate().
28502862
RestoreDevicesAfterDomainReloadIfNecessary();
@@ -3205,7 +3217,10 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
32053217
var shouldProcess = ((IEventPreProcessor)device).PreProcessEvent(currentEventReadPtr);
32063218
#if UNITY_EDITOR
32073219
if (currentEventReadPtr->sizeInBytes > eventSizeBeforePreProcessor)
3220+
{
3221+
Profiler.EndSample();
32083222
throw new AccessViolationException($"'{device}'.PreProcessEvent tries to grow an event from {eventSizeBeforePreProcessor} bytes to {currentEventReadPtr->sizeInBytes} bytes, this will potentially corrupt events after the current event and/or cause out-of-bounds memory access.");
3223+
}
32093224
#endif
32103225
if (!shouldProcess)
32113226
{
@@ -3386,6 +3401,7 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
33863401
{
33873402
// We need to restore m_InputEventStream to a sound state
33883403
// to avoid failing recursive OnUpdate check next frame.
3404+
Profiler.EndSample();
33893405
m_InputEventStream.CleanUpAfterException();
33903406
throw;
33913407
}

Packages/com.unity.inputsystem/InputSystem/Plugins/Users/InputUser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,10 @@ private static void OnEvent(InputEventPtr eventPtr, InputDevice device)
16581658
// we early out and ignore the event entirely.
16591659
if (!DelegateHelpers.InvokeCallbacksSafe_AnyCallbackReturnsTrue(
16601660
ref s_GlobalState.onPreFilterUnpairedDeviceUsed, device, eventPtr, "InputUser.onPreFilterUnpairedDeviceActivity"))
1661+
{
1662+
Profiler.EndSample();
16611663
return;
1664+
}
16621665

16631666
// Go through the changed controls in the event and look for ones actuated
16641667
// above a magnitude of a little above zero.

0 commit comments

Comments
 (0)