#3495·stride

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:

  1. InputManager.InputEventRouter<T>.RouteEvent (sources/engine/Stride.Input/InputManager.cs) calls Listeners.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).
  2. 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. InputManager registers itself as a listener and UISystem registers another, so this order matters.
  3. Text input allocates a scratch buffer on top of the string for each event. KeyboardSDL copies SDL's 32-byte text buffer into a new byte[] before decoding it, and KeyboardWinforms does the same for every IME composition update. A single typed ASCII character also allocates a new one-character string each time.

To Reproduce

  1. Create an InputManager with GameContextHeadless and an InputSourceSimulated with a keyboard and a mouse.
  2. Add one listener for KeyEvent and PointerEvent.
  3. Each frame, simulate 16 key presses and releases and 8 mouse moves, then call Update.
  4. 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.