x11: don't read the X connection needlessly before polling

An empty main loop iteration, of which Pale Moon does tens of thousands
a second, now costs about a third of what it did.
This commit is contained in:
Daemonratte 2026-10-17 15:33:14 +02:00
commit 77f8b18155
3 changed files with 97 additions and 3 deletions

View file

@ -925,5 +925,21 @@ _gdk_x11_roundtrip_async (GdkDisplay *display,
SyncHandle();
}
/* TRUE when Xlib has no events in its queue and no requests waiting
* to be sent: then only new data from the X server can bring events */
gboolean
_gdk_x11_display_is_idle (Display *dpy)
{
gboolean idle;
LockDisplay (dpy);
idle = (dpy->qlen == 0 &&
dpy->bufptr == dpy->buffer &&
!(dpy->flags & XlibDisplayIOError));
UnlockDisplay (dpy);
return idle;
}
#define __GDK_ASYNC_C__
#include "gdkaliasdef.c"

View file

@ -70,6 +70,8 @@ void _gdk_x11_roundtrip_async (GdkDisplay *display,
GdkRoundTripCallback callback,
gpointer data);
gboolean _gdk_x11_display_is_idle (Display *dpy);
G_END_DECLS
#endif /* __GDK_ASYNC_H__ */

View file

@ -83,6 +83,11 @@ struct _GdkDisplaySource
GdkDisplay *display;
GPollFD event_poll_fd;
/* how much xcb had read from the X server when an XPending() found
* no events (see gdk_event_prepare) */
guint64 quiet_total_read;
gboolean quiet;
};
struct _GdkEventTypeX11
@ -2413,6 +2418,73 @@ _gdk_events_queue (GdkDisplay *display)
}
}
/* GLib asks every event source before it waits whether it has
* something, and some programs turn the main loop very often: Pale
* Moon and Firefox tens of thousands of times a second while there is
* work. For the X connection, XPending() answers that with two reads
* from the socket, which find nothing nearly every time - and need not
* look, as the poll() that follows watches the socket. What poll()
* cannot see is what has already been read: events Xlib or xcb queued
* while waiting for a reply, also of other code using xcb directly
* (cairo, Mesa). So XPending() is skipped while Xlib has nothing queued
* or unsent and xcb has read nothing from the socket since the last
* XPending() that found no events; xcb_total_read() counts that (libxcb
* 1.14). Weak references: used where the program has libX11-xcb
* loaded (Pale Moon, Firefox, programs with OpenGL), with no new
* dependency for GTK+; elsewhere every turn asks XPending() as before. */
#if defined (__ELF__) && defined (__GNUC__)
#define HAVE_WEAK_XCB_TOTAL_READ 1
struct xcb_connection_t;
extern struct xcb_connection_t *XGetXCBConnection (Display *dpy) __attribute__ ((weak));
extern guint64 xcb_total_read (struct xcb_connection_t *c) __attribute__ ((weak));
#endif
static gboolean
gdk_display_source_total_read (GdkDisplaySource *source,
guint64 *total)
{
#ifdef HAVE_WEAK_XCB_TOTAL_READ
struct xcb_connection_t *c;
if (XGetXCBConnection == NULL || xcb_total_read == NULL)
return FALSE;
c = XGetXCBConnection (GDK_DISPLAY_XDISPLAY (source->display));
if (c == NULL)
return FALSE;
*total = xcb_total_read (c);
return TRUE;
#else
return FALSE;
#endif
}
static gboolean
gdk_display_source_is_quiet (GdkDisplaySource *source)
{
guint64 total;
return (source->quiet &&
_gdk_x11_display_is_idle (GDK_DISPLAY_XDISPLAY (source->display)) &&
gdk_display_source_total_read (source, &total) &&
total == source->quiet_total_read);
}
static gboolean
gdk_display_source_check_xpending (GdkDisplaySource *source)
{
guint64 total = 0;
gboolean known, pending;
/* counted before: whatever is read during or after the XPending(),
* here or in another thread, makes the next turn look again */
known = gdk_display_source_total_read (source, &total);
pending = gdk_check_xpending (source->display);
source->quiet = known && !pending;
source->quiet_total_read = total;
return pending;
}
static gboolean
gdk_event_prepare (GSource *source,
gint *timeout)
@ -2423,8 +2495,12 @@ gdk_event_prepare (GSource *source,
GDK_THREADS_ENTER ();
*timeout = -1;
retval = (_gdk_event_queue_find_first (display) != NULL ||
gdk_check_xpending (display));
if (_gdk_event_queue_find_first (display) != NULL)
retval = TRUE;
else if (gdk_display_source_is_quiet ((GdkDisplaySource *) source))
retval = FALSE;
else
retval = gdk_display_source_check_xpending ((GdkDisplaySource *) source);
GDK_THREADS_LEAVE ();
@ -2441,7 +2517,7 @@ gdk_event_check (GSource *source)
if (display_source->event_poll_fd.revents & G_IO_IN)
retval = (_gdk_event_queue_find_first (display_source->display) != NULL ||
gdk_check_xpending (display_source->display));
gdk_display_source_check_xpending (display_source));
else
retval = FALSE;