Skip to content

Commit 3575a2b

Browse files
committed
Micro optimizations
1 parent 9226695 commit 3575a2b

File tree

4 files changed

+66
-61
lines changed

4 files changed

+66
-61
lines changed

Zend/zend_interfaces.c

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ static int zend_implement_aggregate(zend_class_entry *interface, zend_class_entr
312312
{
313313
uint32_t i;
314314
int t = -1;
315+
zend_class_iterator_funcs *funcs_ptr;
315316

316317
if (class_type->get_iterator) {
317318
if (class_type->type == ZEND_INTERNAL_CLASS) {
@@ -340,16 +341,21 @@ static int zend_implement_aggregate(zend_class_entry *interface, zend_class_entr
340341
}
341342
}
342343
class_type->get_iterator = zend_user_it_get_new_iterator;
343-
if (class_type->iterator_funcs_ptr != NULL) {
344-
class_type->iterator_funcs_ptr->zf_new_iterator = NULL;
345-
} else if (class_type->type == ZEND_INTERNAL_CLASS) {
346-
class_type->iterator_funcs_ptr = calloc(1, sizeof(zend_class_iterator_funcs));
347-
} else {
348-
class_type->iterator_funcs_ptr = zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
349-
memset(class_type->iterator_funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
350-
}
344+
funcs_ptr = class_type->iterator_funcs_ptr;
351345
if (class_type->type == ZEND_INTERNAL_CLASS) {
352-
class_type->iterator_funcs_ptr->zf_new_iterator = zend_hash_str_find_ptr(&class_type->function_table, "getiterator", sizeof("getiterator") - 1);
346+
if (!funcs_ptr) {
347+
funcs_ptr = calloc(1, sizeof(zend_class_iterator_funcs));
348+
class_type->iterator_funcs_ptr = funcs_ptr;
349+
}
350+
funcs_ptr->zf_new_iterator = zend_hash_str_find_ptr(&class_type->function_table, "getiterator", sizeof("getiterator") - 1);
351+
} else {
352+
if (!funcs_ptr) {
353+
funcs_ptr = zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
354+
class_type->iterator_funcs_ptr = funcs_ptr;
355+
memset(funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
356+
} else {
357+
funcs_ptr->zf_new_iterator = NULL;
358+
}
353359
}
354360
return SUCCESS;
355361
}
@@ -358,6 +364,8 @@ static int zend_implement_aggregate(zend_class_entry *interface, zend_class_entr
358364
/* {{{ zend_implement_iterator */
359365
static int zend_implement_iterator(zend_class_entry *interface, zend_class_entry *class_type)
360366
{
367+
zend_class_iterator_funcs *funcs_ptr;
368+
361369
if (class_type->get_iterator && class_type->get_iterator != zend_user_it_get_iterator) {
362370
if (class_type->type == ZEND_INTERNAL_CLASS) {
363371
/* inheritance ensures the class has the necessary userland methods */
@@ -374,24 +382,30 @@ static int zend_implement_iterator(zend_class_entry *interface, zend_class_entry
374382
}
375383
}
376384
class_type->get_iterator = zend_user_it_get_iterator;
377-
if (class_type->iterator_funcs_ptr != NULL) {
378-
class_type->iterator_funcs_ptr->zf_valid = NULL;
379-
class_type->iterator_funcs_ptr->zf_current = NULL;
380-
class_type->iterator_funcs_ptr->zf_key = NULL;
381-
class_type->iterator_funcs_ptr->zf_next = NULL;
382-
class_type->iterator_funcs_ptr->zf_rewind = NULL;
383-
} else if (class_type->type == ZEND_INTERNAL_CLASS) {
384-
class_type->iterator_funcs_ptr = calloc(1, sizeof(zend_class_iterator_funcs));
385-
} else {
386-
class_type->iterator_funcs_ptr = zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
387-
memset(class_type->iterator_funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
388-
}
385+
funcs_ptr = class_type->iterator_funcs_ptr;
389386
if (class_type->type == ZEND_INTERNAL_CLASS) {
390-
class_type->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
391-
class_type->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
392-
class_type->iterator_funcs_ptr->zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
393-
class_type->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
394-
class_type->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
387+
if (!funcs_ptr) {
388+
funcs_ptr = calloc(1, sizeof(zend_class_iterator_funcs));
389+
class_type->iterator_funcs_ptr = funcs_ptr;
390+
} else {
391+
funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
392+
funcs_ptr->zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
393+
funcs_ptr->zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
394+
funcs_ptr->zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
395+
funcs_ptr->zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
396+
}
397+
} else {
398+
if (!funcs_ptr) {
399+
funcs_ptr = zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
400+
class_type->iterator_funcs_ptr = funcs_ptr;
401+
memset(funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
402+
} else {
403+
funcs_ptr->zf_valid = NULL;
404+
funcs_ptr->zf_current = NULL;
405+
funcs_ptr->zf_key = NULL;
406+
funcs_ptr->zf_next = NULL;
407+
funcs_ptr->zf_rewind = NULL;
408+
}
395409
}
396410
return SUCCESS;
397411
}

ext/spl/spl_array.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,21 @@ static zend_object *spl_array_object_new_ex(zend_class_entry *class_type, zval *
243243
/* Cache iterator functions if ArrayIterator or derived. Check current's */
244244
/* cache since only current is always required */
245245
if (intern->std.handlers == &spl_handler_ArrayIterator) {
246-
if (!class_type->iterator_funcs_ptr->zf_current) {
247-
class_type->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
248-
class_type->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
249-
class_type->iterator_funcs_ptr->zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
250-
class_type->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
251-
class_type->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
246+
zend_class_iterator_funcs *funcs_ptr = class_type->iterator_funcs_ptr;
247+
248+
if (!funcs_ptr->zf_current) {
249+
funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
250+
funcs_ptr->zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
251+
funcs_ptr->zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
252+
funcs_ptr->zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
253+
funcs_ptr->zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
252254
}
253255
if (inherited) {
254-
if (class_type->iterator_funcs_ptr->zf_rewind->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_REWIND;
255-
if (class_type->iterator_funcs_ptr->zf_valid->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_VALID;
256-
if (class_type->iterator_funcs_ptr->zf_key->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_KEY;
257-
if (class_type->iterator_funcs_ptr->zf_current->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_CURRENT;
258-
if (class_type->iterator_funcs_ptr->zf_next->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_NEXT;
256+
if (funcs_ptr->zf_rewind->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_REWIND;
257+
if (funcs_ptr->zf_valid->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_VALID;
258+
if (funcs_ptr->zf_key->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_KEY;
259+
if (funcs_ptr->zf_current->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_CURRENT;
260+
if (funcs_ptr->zf_next->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_NEXT;
259261
}
260262
}
261263

ext/spl/spl_fixedarray.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ static zend_object *spl_fixedarray_object_new_ex(zend_class_entry *class_type, z
207207
spl_fixedarray_object *intern;
208208
zend_class_entry *parent = class_type;
209209
int inherited = 0;
210+
zend_class_iterator_funcs *funcs_ptr;
210211

211212
intern = zend_object_alloc(sizeof(spl_fixedarray_object), parent);
212213

@@ -238,27 +239,28 @@ static zend_object *spl_fixedarray_object_new_ex(zend_class_entry *class_type, z
238239
php_error_docref(NULL, E_COMPILE_ERROR, "Internal compiler error, Class is not child of SplFixedArray");
239240
}
240241

241-
if (!class_type->iterator_funcs_ptr->zf_current) {
242-
class_type->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
243-
class_type->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
244-
class_type->iterator_funcs_ptr->zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
245-
class_type->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
246-
class_type->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
242+
funcs_ptr = class_type->iterator_funcs_ptr;
243+
if (!funcs_ptr->zf_current) {
244+
funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
245+
funcs_ptr->zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
246+
funcs_ptr->zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
247+
funcs_ptr->zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
248+
funcs_ptr->zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
247249
}
248250
if (inherited) {
249-
if (class_type->iterator_funcs_ptr->zf_rewind->common.scope != parent) {
251+
if (funcs_ptr->zf_rewind->common.scope != parent) {
250252
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_REWIND;
251253
}
252-
if (class_type->iterator_funcs_ptr->zf_valid->common.scope != parent) {
254+
if (funcs_ptr->zf_valid->common.scope != parent) {
253255
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_VALID;
254256
}
255-
if (class_type->iterator_funcs_ptr->zf_key->common.scope != parent) {
257+
if (funcs_ptr->zf_key->common.scope != parent) {
256258
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_KEY;
257259
}
258-
if (class_type->iterator_funcs_ptr->zf_current->common.scope != parent) {
260+
if (funcs_ptr->zf_current->common.scope != parent) {
259261
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_CURRENT;
260262
}
261-
if (class_type->iterator_funcs_ptr->zf_next->common.scope != parent) {
263+
if (funcs_ptr->zf_next->common.scope != parent) {
262264
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_NEXT;
263265
}
264266

ext/spl/spl_iterators.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,19 +1346,6 @@ static const zend_function_entry spl_funcs_RecursiveTreeIterator[] = {
13461346
PHP_FE_END
13471347
};
13481348

1349-
#if MBO_0
1350-
static int spl_dual_it_gets_implemented(zend_class_entry *interface, zend_class_entry *class_type)
1351-
{
1352-
class_type->iterator_funcs_ptr->zf_valid = NULL;
1353-
class_type->iterator_funcs_ptr->zf_current = NULL;
1354-
class_type->iterator_funcs_ptr->zf_key = NULL;
1355-
class_type->iterator_funcs_ptr->zf_next = NULL;
1356-
class_type->iterator_funcs_ptr->zf_rewind = NULL;
1357-
1358-
return SUCCESS;
1359-
}
1360-
#endif
1361-
13621349
static zend_function *spl_dual_it_get_method(zend_object **object, zend_string *method, const zval *key)
13631350
{
13641351
zend_function *function_handler;

0 commit comments

Comments
 (0)