[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()
 
 AC_SUBST(X11_EXTRA_DEPS)
 
+# Check whether XCB supports polling of events without checking the
+# connection for new events.
+AC_SEARCH_LIBS([xcb_poll_for_queued_event], [xcb],
+        AC_DEFINE(HAVE_XCB_POLL_FOR_QUEUED_EVENT,1,
+        [Minimize X connection checks when reading events]),
+        AC_DEFINE(HAVE_XCB_POLL_FOR_QUEUED_EVENT,0,
+        [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);
 
 	if(!dpy->xcb->next_event)
+#if HAVE_XCB_POLL_FOR_QUEUED_EVENT
+		dpy->xcb->next_event = xcb_poll_for_queued_event(dpy->xcb->connection);
+#else
 		dpy->xcb->next_event = xcb_poll_for_event(dpy->xcb->connection);
+#endif
 
 	if(dpy->xcb->next_event)
 	{
@@ -301,6 +305,10 @@ int _XEventsQueued(Display *dpy, int mode)
 	 * can reasonably claim there are no new events right now. */
 	if(!dpy->xcb->event_waiter)
 	{
+#if HAVE_XCB_POLL_FOR_QUEUED_EVENT
+		if(!dpy->xcb->next_event)
+			dpy->xcb->next_event = xcb_poll_for_event(dpy->xcb->connection);
+#endif
 		while((response = poll_for_response(dpy)))
 			handle_response(dpy, response, False);
 		if(xcb_connection_has_error(dpy->xcb->connection))
-- 
1.7.1



More information about the Xcb mailing list