Skip to content

Commit a0ff76a

Browse files
committed
core_run: skip game_ai_think when no override or debug is active
The HAVE_GAME_AI block in core_run called game_ai_think on every frame regardless of whether any GameAI feature was actually requested. The function itself always runs game_ai_lib_set_show_debug(ga, show_debug) when an AI instance has been created (line 172 of ai/game_ai.c), which is an indirect dispatch into the loaded GameAI library and runs even when override_p1, override_p2, and show_debug are all false. When the user has loaded a GameAI library but is not currently using any of its overrides, this per-frame dispatch is wasted work. Gate the call in core_run on at least one of the three settings being active. When all are false: - The function call is skipped (function-call overhead saved) - The per-frame game_ai_lib_set_show_debug dispatch is skipped (the meaningful win when an AI lib is loaded but unused) - The internal g_frameCount counter doesn't tick, so when overrides eventually flip on, the first think() may fire on the very next frame rather than after the SKIPFRAMES delay. Harmless - the user just enabled the feature; immediate AI input is the desired UX. - The lazy 'ga' creation path inside game_ai_think (gated on !ga && g_ram_ptr) is deferred until overrides actually turn on, which is correct: no point creating an AI instance for a frame that won't use it. For builds without HAVE_GAME_AI, the entire block is removed by the preprocessor and this change has no effect. For builds with HAVE_GAME_AI but no GameAI library ever loaded, the work skipped by the new gate is sub-microsecond per frame. For builds with a GameAI library loaded but overrides currently off, the skip avoids one per-frame indirect function-pointer dispatch into the loaded shared library, which is a real per-frame win measured in hundreds of cycles depending on the AI lib's implementation.
1 parent 129a5ba commit a0ff76a

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

runloop.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8267,16 +8267,27 @@ void core_run(void)
82678267
#ifdef HAVE_GAME_AI
82688268
{
82698269
settings_t *settings = config_get_ptr();
8270-
video_driver_state_t *video_st = video_state_get_ptr();
8271-
game_ai_think(
8272-
settings->bools.game_ai_override_p1,
8273-
settings->bools.game_ai_override_p2,
8274-
settings->bools.game_ai_show_debug,
8275-
video_st->frame_cache_data,
8276-
video_st->frame_cache_width,
8277-
video_st->frame_cache_height,
8278-
video_st->frame_cache_pitch,
8279-
video_st->pix_fmt);
8270+
bool override_p1 = settings->bools.game_ai_override_p1;
8271+
bool override_p2 = settings->bools.game_ai_override_p2;
8272+
bool show_debug = settings->bools.game_ai_show_debug;
8273+
8274+
/* Skip the call entirely when no GameAI feature is active for this
8275+
* frame. Avoids per-frame indirect dispatch into the loaded GameAI
8276+
* library (game_ai_lib_set_show_debug) that game_ai_think runs
8277+
* unconditionally when an AI has been instantiated. */
8278+
if (override_p1 || override_p2 || show_debug)
8279+
{
8280+
video_driver_state_t *video_st = video_state_get_ptr();
8281+
game_ai_think(
8282+
override_p1,
8283+
override_p2,
8284+
show_debug,
8285+
video_st->frame_cache_data,
8286+
video_st->frame_cache_width,
8287+
video_st->frame_cache_height,
8288+
video_st->frame_cache_pitch,
8289+
video_st->pix_fmt);
8290+
}
82808291
}
82818292
#endif
82828293

0 commit comments

Comments
 (0)