Skip to content

Commit 55183b9

Browse files
authored
poll: fetch method ptr directly (#22816)
Rather than going through a whole callability check via call_user_function() we can grab the known method pointer and call it directly. This removes some allocations
1 parent af43113 commit 55183b9

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

‎main/poll/poll_handle.c‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,22 @@
1919
static php_socket_t php_poll_handle_default_get_fd(php_poll_handle_object *handle)
2020
{
2121
zval retval;
22-
zval obj;
23-
zval func_name;
2422

25-
ZVAL_OBJ(&obj, &handle->std);
26-
27-
/* Prepare function name as zval */
28-
ZVAL_STRING(&func_name, "getFileDescriptor");
23+
/* Grab getFileDescriptor() method pointer which is stored in lowercase in the function table */
24+
zend_function *method = zend_hash_str_find_ptr_lc(&handle->std.ce->function_table, ZEND_STRL("getfiledescriptor"));
25+
ZEND_ASSERT(method && "no default method???");
2926

3027
/* Call getFileDescriptor() method */
31-
if (EXPECTED(call_user_function(NULL, &obj, &func_name, &retval, 0, NULL) == SUCCESS)) {
32-
if (Z_TYPE(retval) == IS_LONG) {
33-
php_socket_t fd = Z_LVAL(retval) < 0 ? SOCK_ERR : (php_socket_t) Z_LVAL(retval);
34-
zval_ptr_dtor(&retval);
35-
zval_ptr_dtor(&func_name); /* Clean up function name */
36-
return fd;
37-
}
38-
zval_ptr_dtor(&retval);
28+
zend_call_known_function(method, &handle->std, handle->std.ce, &retval, 0, NULL, NULL);
29+
30+
/* No need to deref the return value as the class is final and thus the method cannot be changed to return by-ref */
31+
if (EXPECTED(Z_TYPE(retval) == IS_LONG)) {
32+
php_socket_t fd = Z_LVAL(retval) < 0 ? SOCK_ERR : (php_socket_t) Z_LVAL(retval);
33+
/* No need to clean the retval as we know it is an integer, and thus it's just on the stack */
34+
return fd;
3935
}
4036

41-
zval_ptr_dtor(&func_name); /* Clean up function name */
37+
zval_ptr_dtor(&retval);
4238
return SOCK_ERR; /* Invalid socket */
4339
}
4440

0 commit comments

Comments
 (0)