Skip to content

Commit

Permalink
Improve input-sdl startup time.
Browse files Browse the repository at this point in the history
SDL_InitSubSystem() takes approximately 0.6s for me, presumably because
it does lots of device enumeration.

When all Input-sdl-ControlX configuration sections are present and
valid, then input-sdl calls SDL_InitSubSystem(SDL_INIT_JOYSTICK) three
times. When configuration is not present, this happens once more for
each connected joysick.

Moving the SDL_InitSubSystem(SDL_INIT_JOYSTICK) calls to a higher level
allows for a reduction to two total calls (whether configuration is
present or not). The first remaining call happens when loading the
plugin and the second call happens when attaching the plugin to the
core. Keeping these calls separate allows SDL to refresh itself in case
the user has plugged/unplugged a joystick in between (not likely with
ui-console, but perhaps with a GUI).

On a system with three joystick devices, loading a game and then
exiting (--testshots 0), this reduces time by:
with configuration:    11.5%
without configuration: 32.1%

Full benchmarks:

[ ----------- original behavior -------------]
[ with configuraton ][ without configuration ]
real    0m5.255s        real    0m7.470s
user    0m1.499s        user    0m1.534s
sys     0m0.110s        sys     0m0.135s

real    0m5.313s        real    0m7.471s
user    0m1.471s        user    0m1.514s
sys     0m0.119s        sys     0m0.120s

real    0m5.360s        real    0m7.414s
user    0m1.496s        user    0m1.569s
sys     0m0.106s        sys     0m0.127s

[ ------------ patched behavior -------------]
[ with configuraton ][ without configuration ]
real    0m4.741s        real    0m5.088s
user    0m1.473s        user    0m1.468s
sys     0m0.122s        sys     0m0.101s

real    0m4.724s        real    0m5.066s
user    0m1.511s        user    0m1.484s
sys     0m0.082s        sys     0m0.090s

real    0m4.628s        real    0m5.024s
user    0m1.494s        user    0m1.504s
sys     0m0.094s        sys     0m0.081s
  • Loading branch information
bugfood committed Jan 29, 2019
1 parent 2320005 commit 097fc83
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
38 changes: 1 addition & 37 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@ static const char * get_sdl_joystick_name(int iCtrlIdx)
{
static char JoyName[256];
const char *joySDLName;
int joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);

/* initialize the joystick subsystem if necessary */
if (!joyWasInit)
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
{
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return NULL;
}

/* get the name of the corresponding joystick */
joySDLName = SDL_JoystickName(iCtrlIdx);
Expand All @@ -141,40 +132,13 @@ static const char * get_sdl_joystick_name(int iCtrlIdx)
JoyName[255] = 0;
}

/* quit the joystick subsystem if necessary */
if (!joyWasInit)
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);

/* if the SDL function had an error, then return NULL, otherwise return local copy of joystick name */
if (joySDLName == NULL)
return NULL;
else
return JoyName;
}

static int get_sdl_num_joysticks(void)
{
int numJoysticks = 0;
int joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);

/* initialize the joystick subsystem if necessary */
if (!joyWasInit)
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
{
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return 0;
}

/* get thenumber of joysticks */
numJoysticks = SDL_NumJoysticks();

/* quit the joystick subsystem if necessary */
if (!joyWasInit)
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);

return numJoysticks;
}

/////////////////////////////////////
// load_controller_config()
// return value: 1 = OK
Expand Down Expand Up @@ -540,7 +504,7 @@ void load_configuration(int bPreConfig)
int ControlDevice[4];
char DeviceName[4][256];
int ActiveControllers = 0;
int sdlNumJoysticks = get_sdl_num_joysticks();
int sdlNumJoysticks = SDL_NumJoysticks();
float fVersion = 0.0f;
const char *sdl_name;
int ControllersFound = 0;
Expand Down
29 changes: 22 additions & 7 deletions src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ EXPORT m64p_error CALL PluginStartup(m64p_dynlib_handle CoreLibHandle, void *Con
ptr_CoreGetAPIVersions CoreAPIVersionFunc;

int i, ConfigAPIVersion, DebugAPIVersion, VidextAPIVersion;
int joyWasInit;

if (l_PluginInit)
return M64ERR_ALREADY_INIT;
Expand Down Expand Up @@ -206,9 +207,22 @@ EXPORT m64p_error CALL PluginStartup(m64p_dynlib_handle CoreLibHandle, void *Con
for (i = 0; i < 4; i++)
controller[i].control = temp_core_controlinfo + i;

/* initialize the joystick subsystem if necessary */
joyWasInit = SDL_WasInit(SDL_INIT_JOYSTICK);
if (!joyWasInit)
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
{
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return M64ERR_SYSTEM_FAIL;
}

/* read plugin config from core config database, auto-config if necessary and update core database */
load_configuration(1);

/* quit the joystick subsystem if necessary */
if (!joyWasInit)
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);

l_PluginInit = 1;
return M64ERR_SUCCESS;
}
Expand Down Expand Up @@ -715,13 +729,6 @@ EXPORT void CALL GetKeys( int Control, BUTTONS *Keys )

static void InitiateJoysticks(int cntrl)
{
// init SDL joystick subsystem
if (!SDL_WasInit(SDL_INIT_JOYSTICK))
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) {
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return;
}

if (controller[cntrl].device >= 0) {
controller[cntrl].joystick = SDL_JoystickOpen(controller[cntrl].device);
if (!controller[cntrl].joystick)
Expand Down Expand Up @@ -910,6 +917,14 @@ EXPORT void CALL InitiateControllers(CONTROL_INFO ControlInfo)
for (i = 0; i < 4; i++)
controller[i].control = ControlInfo.Controls + i;

/* initialize the joystick subsystem if necessary (and leave it initialized) */
if (! SDL_WasInit(SDL_INIT_JOYSTICK))
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
{
DebugMessage(M64MSG_ERROR, "Couldn't init SDL joystick subsystem: %s", SDL_GetError() );
return;
}

// read configuration
load_configuration(0);

Expand Down

0 comments on commit 097fc83

Please sign in to comment.