Input: event routing and text input allocate per event, and listener order is undefined
Author: sasvdwCreated Sep 27, 2026Updated Sep 27, 2026
Release Type: GitHub
Version: master @ 65045e2ae
Platform(s): All. Text input: SDL and WinForms backends.
Describe the bug The input pipeline allocates on paths that run once per input event, every frame:
InputManager.InputEventRouter<T>.RouteEvent(sources/engine/Stride.Input/InputManager.cs) callsListeners.ToArray()for every event it routes. One array per event per frame, so the garbage grows with how much input arrives (mouse moves, held keys).- Listeners are kept in a
HashSet, so the order in which listeners see an event is not defined. Removing a listener and adding it back puts it back in its old slot: register A, B, C, remove B, add B, and events still reach A, B, C instead of A, C, B.InputManagerregisters itself as a listener andUISystemregisters another, so this order matters. - Text input allocates a scratch buffer on top of the string for each event.
KeyboardSDLcopies SDL's 32-byte text buffer into a newbyte[]before decoding it, andKeyboardWinformsdoes the same for every IME composition update. A single typed ASCII character also allocates a new one-character string each time.
To Reproduce
- Create an
InputManagerwithGameContextHeadlessand anInputSourceSimulatedwith a keyboard and a mouse. - Add one listener for
KeyEventandPointerEvent. - Each frame, simulate 16 key presses and releases and 8 mouse moves, then call
Update. - Measure
GC.GetAllocatedBytesForCurrentThread()across frames after a warm-up: 1,600 bytes per frame. That is one 40-byte array per routed event.
For the text path, decoding an SDL text event for "日本" allocates 88 bytes: a 56-byte scratch array plus the 32-byte string.
Expected behavior
- Routing events to listeners allocates nothing in steady state.
- Listeners receive events in the order they were registered.
- Decoding text input allocates at most the resulting string, and nothing for a single ASCII character.
Screenshots N/A
Log and callstacks N/A
Additional context
These can be fixed without changing the public API. TextInputEvent.Text is a string, so non-ASCII text still costs one string per event. Removing that last allocation would need an API change, which I'd rather discuss separately.
Source: stride3d/stride