SDL icon indicating copy to clipboard operation
SDL copied to clipboard

Long SDL_Init() time when Haptics enabled

Open spiderkeys opened this issue 1 year ago • 5 comments

I was testing performance improvements made by PR #9450 , and noticed that there is also a significant initialization block when you initialize SDL_INIT_HAPTIC.

I performed tests using:

auto ret = SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC);

and

auto ret = SDL_Init(SDL_INIT_GAMECONTROLLER);

Tests were performed on the 2.28.5 release (pre-libudev PR) and my patched version of the SDL2 branch (#10411).

Results:

2.28.5 (joystick+haptic) : 2.215s
2.28.5 (joystick): 1.118s
2.30.6 (joystick+haptic): 1.409s
2.30.6 (joystick): 0.407s

This showed the expected speedup when it came to Joystick initialization/device enumeration, but I was surprised to see the Haptic initialization adding another second to the init time. This may be expected, or may be a case for another performance improvement, similar to the joystick init. Just wanted to share this observation either way, as it is a significant amount of time that can delay your app in non-trivial ways.

spiderkeys avatar Jul 29 '24 06:07 spiderkeys

That doesn't look right. I tested the time SDL_INIT_HAPTIC takes and it was under 64ms. Try SDL_InitSubSystem to separate the init process.

Which platform are you running this on?

AntTheAlchemist avatar Jul 29 '24 10:07 AntTheAlchemist

Also, SDL_INIT_GAMEPAD took 57ms to complete on my low-end Windows system .

AntTheAlchemist avatar Jul 29 '24 10:07 AntTheAlchemist

This is on Ubuntu 22.04.

SDL_INIT_GAMEPAD only exists in main, not the SDL2 branch. Also, I think the libudev issues/changes would have only ever affected Linux.

I re-ran the tests using SDL_InitSubSystem and got (roughly) the same results:

  spdlog::info("a" );
  // Initialize SDL
  auto ret = SDL_Init(0);
  if (ret < 0) {
    throw std::runtime_error(std::string("Failed to initialize SDL: ") + std::string(SDL_GetError()));
  }
  spdlog::info("b" );
  ret = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
  if (ret < 0) {
    throw std::runtime_error(std::string("Failed to initialize SDL game controller: ") + std::string(SDL_GetError()));
  }
  spdlog::info("c" );
  ret = SDL_InitSubSystem(SDL_INIT_HAPTIC);
  if (ret < 0) {
    throw std::runtime_error(std::string("Failed to initialize SDL haptic: ") + std::string(SDL_GetError()));
  }
  spdlog::info("d" );
[04:35:57.102][info]: a
[04:35:57.102][info]: b
[04:35:57.503][info]: c
[04:35:58.642][info]: d

401ms for GAME_CONTROLLER, 1.139s for HAPTIC.

spiderkeys avatar Jul 29 '24 11:07 spiderkeys

Looking at the haptic code it looks like it would benefit from the same speedup approach that we used with joysticks. I'll throw this on the 3.2 list. Feel free to submit a PR if you get to it before we do!

slouken avatar Jul 29 '24 14:07 slouken

We are scoping work for the SDL 3.2.0 release, so please let us know if this is a showstopper for you.

slouken avatar Oct 06 '24 19:10 slouken