Skip to content

Commit a7b48f3

Browse files
committed
php#53: Optimize network_async_set_socket_blocking to return bool
Changed network_async_set_socket_blocking from void to bool return type to eliminate redundant EG(exception) checks. Function now returns false on error instead of requiring separate exception checking. Updated 3 call sites: - php_sockop_write() in xp_socket.c - sock_async_poll() in xp_socket.c - network_async_accept_incoming() in network_async.c This reduces code duplication and improves performance by combining function call and error check into single conditional.
1 parent b5f9c63 commit a7b48f3

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

main/network_async.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ static zend_always_inline zend_ulong poll2_events_to_async(const short events);
6363
* @param blocking
6464
* @param sock_data
6565
*/
66-
void network_async_set_socket_blocking(php_socket_t socket, bool blocking, php_netstream_data_t *sock_data)
66+
bool network_async_set_socket_blocking(php_socket_t socket, bool blocking, php_netstream_data_t *sock_data)
6767
{
6868
// Optimization: avoid redundant system calls if the socket is already in the desired mode
6969
if (sock_data != NULL) {
7070
if (!blocking && sock_data->nonblocking_applied) {
7171
// Already in non-blocking mode, skip system call
72-
return;
72+
return true;
7373
}
7474
if (blocking && !sock_data->nonblocking_applied) {
7575
// Already in blocking mode, skip system call
76-
return;
76+
return true;
7777
}
7878
}
7979

@@ -86,7 +86,7 @@ void network_async_set_socket_blocking(php_socket_t socket, bool blocking, php_n
8686
ZEND_ASYNC_EXCEPTION_DEFAULT,
8787
"ioctlsocket(FIONBIO) failed (WSA error %d)", err
8888
);
89-
return;
89+
return false;
9090
}
9191
#else
9292
int flags = fcntl(socket, F_GETFL, 0);
@@ -97,7 +97,7 @@ void network_async_set_socket_blocking(php_socket_t socket, bool blocking, php_n
9797
"fcntl(F_GETFL) failed: %s", strerror(errno)
9898
);
9999

100-
return;
100+
return false;
101101
}
102102

103103
int new_flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
@@ -107,14 +107,16 @@ void network_async_set_socket_blocking(php_socket_t socket, bool blocking, php_n
107107
ZEND_ASYNC_EXCEPTION_DEFAULT,
108108
"fcntl(F_SETFL) failed: %s", strerror(errno)
109109
);
110-
return;
110+
return false;
111111
}
112112
#endif
113113

114114
// Update the flag to reflect the actual socket state
115115
if (sock_data != NULL) {
116116
sock_data->nonblocking_applied = !blocking;
117117
}
118+
119+
return true;
118120
}
119121

120122
bool network_async_ensure_socket_nonblocking(php_socket_t socket)
@@ -1142,8 +1144,7 @@ ZEND_API php_socket_t network_async_accept_incoming(php_netstream_data_t *netdat
11421144

11431145
// Ensure socket is in non-blocking mode for async operations
11441146
if (netdata->is_blocked && !netdata->nonblocking_applied) {
1145-
network_async_set_socket_blocking(netdata->socket, false, netdata);
1146-
if (UNEXPECTED(EG(exception) != NULL)) {
1147+
if (UNEXPECTED(!network_async_set_socket_blocking(netdata->socket, false, netdata))) {
11471148
goto return_error;
11481149
}
11491150
}

main/network_async.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
BEGIN_EXTERN_C()
2222

23-
ZEND_API void network_async_set_socket_blocking(php_socket_t socket, bool blocking, php_netstream_data_t *sock_data);
23+
ZEND_API bool network_async_set_socket_blocking(php_socket_t socket, bool blocking, php_netstream_data_t *sock_data);
2424
ZEND_API bool network_async_ensure_socket_nonblocking(php_socket_t socket);
2525
ZEND_API void network_async_wait_socket(php_socket_t socket, const zend_ulong events, const zend_ulong timeout);
2626
ZEND_API int network_async_await_stream_socket(php_netstream_data_t *netdata, async_poll_event events, struct timeval *timeout);

main/streams/xp_socket.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ static ssize_t php_sockop_write(php_stream *stream, const char *buf, size_t coun
7272
ptimeout = &sock->timeout;
7373

7474
if (ZEND_ASYNC_IS_ACTIVE && sock->is_blocked && !sock->nonblocking_applied) {
75-
network_async_set_socket_blocking(sock->socket, false, sock);
76-
if (UNEXPECTED(EG(exception) != NULL)) {
75+
if (UNEXPECTED(!network_async_set_socket_blocking(sock->socket, false, sock))) {
7776
/* If we are in async context and an exception was thrown, we should not continue. */
7877
return -1;
7978
}
@@ -351,8 +350,7 @@ static int php_sockop_stat(php_stream *stream, php_stream_statbuf *ssb)
351350
static inline int sock_async_poll(php_netstream_data_t *sock, async_poll_event poll_events)
352351
{
353352
if (UNEXPECTED(sock->is_blocked && !sock->nonblocking_applied && ZEND_ASYNC_IS_ACTIVE)) {
354-
network_async_set_socket_blocking(sock->socket, false, sock);
355-
if (UNEXPECTED(EG(exception) != NULL)) {
353+
if (UNEXPECTED(!network_async_set_socket_blocking(sock->socket, false, sock))) {
356354
return -1;
357355
}
358356
}

0 commit comments

Comments
 (0)