[Xcb] [PATCH v2 libX11] Avoid reading events from connection when it's not necessary.

Rami Ylimäki rami.ylimaki at vincit.fi
Tue May 10 07:42:59 PDT 2011

This commit changes poll_for_event to check for queued events
only. This optimizes unnecessary system calls away in certain
situations. For example, calling XInternAtoms would read from the
connection a bit too much without the fix.

_XReply uses xcb_wait_for_reply to read a reply. It only cares about
handling events that preceded the reply. Those events must have been
already queued when xcb_wait_for_reply returns.

_XReadEvents uses xcb_wait_for_event to read an event. Only the
replies that preceded the event should be handled. Therefore the
replies that we care about must have been already queued when
xcb_wait_for_event returns.

_XEventsQueued should check the connection status if events haven't
been queued. This is accomplished by extracting the xcb_poll_for_event
call from poll_for_event. _XEventsQueued is the only function that
actually needed the connection to be checked in poll_for_event.

Signed-off-by: Rami Ylimäki <rami.ylimaki at vincit.fi>

v2: Define HAVE_XCB_POLL_FOR_QUEUED_EVENT to see whether new XCB
interface can be used.

 configure.ac |    8 ++++++++
 src/xcb_io.c |    8 ++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/configure.ac b/configure.ac
index a39ab8d..953fc14 100644
--- a/configure.ac
+++ b/configure.ac
@@ -84,6 +84,14 @@ PKG_PROG_PKG_CONFIG()
+# Check whether XCB supports polling of events without checking the
+# connection for new events.
+AC_SEARCH_LIBS([xcb_poll_for_queued_event], [xcb],
+        [Minimize X connection checks when reading events]),
+        [Check X connection always when reading events]))
 # Issue an error if xtrans.m4 was not found and XTRANS_CONNECTION_FLAGS macro
 # was not expanded, since libX11 with no transport types is rather useless.
diff --git a/src/xcb_io.c b/src/xcb_io.c
index 8930736..2f52e46 100644
--- a/src/xcb_io.c
+++ b/src/xcb_io.c
@@ -207,7 +207,11 @@ static xcb_generic_reply_t *poll_for_event(Display *dpy)
 	assert(dpy->xcb->event_owner == XlibOwnsEventQueue && !dpy->xcb->event_waiter);
+		dpy->xcb->next_event = xcb_poll_for_queued_event(dpy->xcb->connection);
 		dpy->xcb->next_event = xcb_poll_for_event(dpy->xcb->connection);
@@ -301,6 +305,10 @@ int _XEventsQueued(Display *dpy, int mode)
 	 * can reasonably claim there are no new events right now. */
+		if(!dpy->xcb->next_event)
+			dpy->xcb->next_event = xcb_poll_for_event(dpy->xcb->connection);
 		while((response = poll_for_response(dpy)))
 			handle_response(dpy, response, False);

More information about the Xcb mailing list