From b992b915e520fed9e8558c9e6d8ca66370c04e64 Mon Sep 17 00:00:00 2001
From: Cameron Gutman <[EMAIL REDACTED]>
Date: Sat, 5 Jun 2021 11:57:30 -0500
Subject: [PATCH] Optimize SDL_WaitEventTimeout() for the SDL_PollEvent() case
There's no sense in doing all the setup for waiting if we're just polling.
---
src/events/SDL_events.c | 21 +++++++++------------
1 file changed, 9 insertions(+), 12 deletions(-)
diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index 69990cf1d..cefe885a7 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -866,26 +866,23 @@ int
SDL_WaitEventTimeout(SDL_Event * event, int timeout)
{
SDL_VideoDevice *_this = SDL_GetVideoDevice();
- SDL_bool need_polling = SDL_events_need_polling();
- SDL_Window *wakeup_window = NULL;
+ SDL_Window *wakeup_window;
Uint32 expiration = 0;
if (timeout > 0)
expiration = SDL_GetTicks() + timeout;
- if (!need_polling && _this) {
+ if (timeout != 0 && _this && _this->WaitEventTimeout && _this->SendWakeupEvent && !SDL_events_need_polling()) {
/* Look if a shown window is available to send the wakeup event. */
wakeup_window = SDL_find_active_window(_this);
- need_polling = (wakeup_window == NULL);
- }
-
- if (!need_polling && _this && _this->WaitEventTimeout && _this->SendWakeupEvent) {
- int status = SDL_WaitEventTimeout_Device(_this, wakeup_window, event, timeout);
+ if (wakeup_window) {
+ int status = SDL_WaitEventTimeout_Device(_this, wakeup_window, event, timeout);
- /* There may be implementation-defined conditions where the backend cannot
- reliably wait for the next event. If that happens, fall back to polling */
- if (status >= 0) {
- return status;
+ /* There may be implementation-defined conditions where the backend cannot
+ reliably wait for the next event. If that happens, fall back to polling. */
+ if (status >= 0) {
+ return status;
+ }
}
}