Skip to content

Commit 07ce3c5

Browse files
committed
poll: optimize poll add and remove for those using fd table
1 parent 4cb90cb commit 07ce3c5

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

main/poll/php_poll_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ php_poll_fd_table *php_poll_fd_table_init(int initial_capacity, bool persistent)
9999
void php_poll_fd_table_cleanup(php_poll_fd_table *table);
100100
php_poll_fd_entry *php_poll_fd_table_find(php_poll_fd_table *table, int fd);
101101
php_poll_fd_entry *php_poll_fd_table_get(php_poll_fd_table *table, int fd);
102-
void php_poll_fd_table_remove(php_poll_fd_table *table, int fd);
102+
php_poll_fd_entry *php_poll_fd_table_get_new(php_poll_fd_table *table, int fd);
103+
bool php_poll_fd_table_remove(php_poll_fd_table *table, int fd);
103104

104105
/* Accessor functions for table properties */
105106
static inline int php_poll_fd_table_count(php_poll_fd_table *table)

main/poll/poll_backend_eventport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static zend_result eventport_backend_add(
146146
return FAILURE;
147147
}
148148

149-
php_poll_fd_entry *entry = php_poll_fd_table_get(backend_data->fd_table, fd);
149+
php_poll_fd_entry *entry = php_poll_fd_table_get_new(backend_data->fd_table, fd);
150150
if (!entry) {
151151
php_poll_set_error(ctx, PHP_POLL_ERR_NOMEM);
152152
return FAILURE;

main/poll/poll_backend_poll.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static zend_result poll_backend_add(php_poll_ctx *ctx, int fd, uint32_t events,
116116
return FAILURE;
117117
}
118118

119-
php_poll_fd_entry *entry = php_poll_fd_table_get(backend_data->fd_table, fd);
119+
php_poll_fd_entry *entry = php_poll_fd_table_get_new(backend_data->fd_table, fd);
120120
if (!entry) {
121121
php_poll_set_error(ctx, PHP_POLL_ERR_NOMEM);
122122
return FAILURE;
@@ -153,12 +153,11 @@ static zend_result poll_backend_remove(php_poll_ctx *ctx, int fd)
153153
{
154154
poll_backend_data_t *backend_data = (poll_backend_data_t *) ctx->backend_data;
155155

156-
if (!php_poll_fd_table_find(backend_data->fd_table, fd)) {
156+
if (!php_poll_fd_table_remove(backend_data->fd_table, fd)) {
157157
php_poll_set_error(ctx, PHP_POLL_ERR_NOTFOUND);
158158
return FAILURE;
159159
}
160160

161-
php_poll_fd_table_remove(backend_data->fd_table, fd);
162161
return SUCCESS;
163162
}
164163

main/poll/poll_backend_wsapoll.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static zend_result wsapoll_backend_add(php_poll_ctx *ctx, int fd, uint32_t event
120120
return FAILURE;
121121
}
122122

123-
php_poll_fd_entry *entry = php_poll_fd_table_get(backend_data->fd_table, fd);
123+
php_poll_fd_entry *entry = php_poll_fd_table_get_new(backend_data->fd_table, fd);
124124
if (!entry) {
125125
php_poll_set_error(ctx, PHP_POLL_ERR_NOMEM);
126126
return FAILURE;
@@ -158,12 +158,11 @@ static zend_result wsapoll_backend_remove(php_poll_ctx *ctx, int fd)
158158
{
159159
wsapoll_backend_data_t *backend_data = (wsapoll_backend_data_t *) ctx->backend_data;
160160

161-
if (!php_poll_fd_table_find(backend_data->fd_table, fd)) {
161+
if (!php_poll_fd_table_remove(backend_data->fd_table, fd)) {
162162
php_poll_set_error(ctx, PHP_POLL_ERR_NOTFOUND);
163163
return FAILURE;
164164
}
165165

166-
php_poll_fd_table_remove(backend_data->fd_table, fd);
167166
return SUCCESS;
168167
}
169168

main/poll/poll_fd_table.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,9 @@ php_poll_fd_entry *php_poll_fd_table_find(php_poll_fd_table *table, int fd)
5454
return zv ? Z_PTR_P(zv) : NULL;
5555
}
5656

57-
php_poll_fd_entry *php_poll_fd_table_get(php_poll_fd_table *table, int fd)
57+
php_poll_fd_entry *php_poll_fd_table_get_new(php_poll_fd_table *table, int fd)
5858
{
59-
php_poll_fd_entry *entry = php_poll_fd_table_find(table, fd);
60-
if (entry) {
61-
return entry;
62-
}
63-
64-
entry = php_poll_calloc(1, sizeof(php_poll_fd_entry), table->persistent);
59+
php_poll_fd_entry *entry = php_poll_calloc(1, sizeof(php_poll_fd_entry), table->persistent);
6560
if (!entry) {
6661
return NULL;
6762
}
@@ -82,14 +77,25 @@ php_poll_fd_entry *php_poll_fd_table_get(php_poll_fd_table *table, int fd)
8277
return entry;
8378
}
8479

85-
void php_poll_fd_table_remove(php_poll_fd_table *table, int fd)
80+
php_poll_fd_entry *php_poll_fd_table_get(php_poll_fd_table *table, int fd)
81+
{
82+
php_poll_fd_entry *entry = php_poll_fd_table_find(table, fd);
83+
if (entry) {
84+
return entry;
85+
}
86+
87+
return php_poll_fd_table_get_new(table, fd);
88+
}
89+
90+
bool php_poll_fd_table_remove(php_poll_fd_table *table, int fd)
8691
{
8792
zval *zv = zend_hash_index_find(&table->entries_ht, (zend_ulong) fd);
88-
if (zv) {
89-
php_poll_fd_entry *entry = Z_PTR_P(zv);
90-
pefree(entry, table->persistent);
91-
zend_hash_index_del(&table->entries_ht, (zend_ulong) fd);
93+
if (zv == NULL) {
94+
return false;
9295
}
96+
php_poll_fd_entry *entry = Z_PTR_P(zv);
97+
pefree(entry, table->persistent);
98+
return zend_hash_index_del(&table->entries_ht, (zend_ulong) fd) == SUCCESS;
9399
}
94100

95101
/* Helper function for backends that need to iterate over all entries */

0 commit comments

Comments
 (0)