gsocket: avoid unnecessary select in _send_messages() and _receive_message()
authorTim-Philipp Müller <tim@centricular.com>
Wed, 3 Jun 2015 12:06:24 +0000 (13:06 +0100)
committerTim-Philipp Müller <tim@centricular.com>
Sun, 21 Jun 2015 09:28:14 +0000 (10:28 +0100)
For performance reasons we should always try to send or
receive our messages first and only wait for more space
or data to become available if we get an EAGAIN (and
are in blocking mode).

https://bugzilla.gnome.org/show_bug.cgi?id=751122

gio/gsocket.c

index 7278e80..7d6e91d 100644 (file)
@@ -4188,11 +4188,6 @@ g_socket_send_messages (GSocket        *socket,
       {
         gint ret;
 
-        if (socket->priv->blocking &&
-            !g_socket_condition_wait (socket,
-                                      G_IO_OUT, cancellable, error))
-          return -1;
-
         ret = sendmmsg (socket->priv->fd, msgvec + num_sent, num_messages - num_sent,
                         flags | G_SOCKET_DEFAULT_SEND_FLAGS);
 
@@ -4206,7 +4201,13 @@ g_socket_send_messages (GSocket        *socket,
             if (socket->priv->blocking &&
                 (errsv == EWOULDBLOCK ||
                  errsv == EAGAIN))
-              continue;
+              {
+                if (!g_socket_condition_wait (socket,
+                                              G_IO_OUT, cancellable, error))
+                  return -1;
+
+                continue;
+              }
 
             if (num_sent > 0 &&
                 (errsv == EWOULDBLOCK ||
@@ -4517,11 +4518,6 @@ g_socket_receive_message (GSocket                 *socket,
     /* do it */
     while (1)
       {
-       if (socket->priv->blocking &&
-           !g_socket_condition_wait (socket,
-                                     G_IO_IN, cancellable, error))
-         return -1;
-
        result = recvmsg (socket->priv->fd, &msg, msg.msg_flags);
 #ifdef MSG_CMSG_CLOEXEC        
        if (result < 0 && get_socket_errno () == EINVAL)
@@ -4542,7 +4538,13 @@ g_socket_receive_message (GSocket                 *socket,
            if (socket->priv->blocking &&
                (errsv == EWOULDBLOCK ||
                 errsv == EAGAIN))
-             continue;
+             {
+               if (!g_socket_condition_wait (socket,
+                                             G_IO_IN, cancellable, error))
+                 return -1;
+
+               continue;
+             }
 
            g_set_error (error, G_IO_ERROR,
                         socket_io_error_from_errno (errsv),