libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
10.23k stars 1.86k forks source link

Long SDL_Init() time when Haptics enabled #10412

Open spiderkeys opened 4 months ago

spiderkeys commented 4 months ago

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.

AntTheAlchemist commented 4 months ago

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 commented 4 months ago

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

spiderkeys commented 4 months ago

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.

slouken commented 4 months ago

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 commented 1 month ago

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