Skip to content

Commit 6f289c5

Browse files
committed
MFH: Iterator methods overwriting optimisations
1 parent a30add1 commit 6f289c5

File tree

1 file changed

+57
-82
lines changed

1 file changed

+57
-82
lines changed

ext/spl/spl_fixedarray.c

Lines changed: 57 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,9 @@ typedef struct _spl_fixedarray_object { /* {{{ */
5656
zend_function *fptr_offset_set;
5757
zend_function *fptr_offset_has;
5858
zend_function *fptr_offset_del;
59-
zend_function *fptr_it_next;
60-
zend_function *fptr_it_rewind;
61-
zend_function *fptr_it_current;
62-
zend_function *fptr_it_key;
63-
zend_function *fptr_it_valid;
6459
zend_function *fptr_count;
6560
int current;
61+
int flags;
6662
zend_class_entry *ce_get_iterator;
6763
} spl_fixedarray_object;
6864
/* }}} */
@@ -73,6 +69,12 @@ typedef struct _spl_fixedarray_it { /* {{{ */
7369
} spl_fixedarray_it;
7470
/* }}} */
7571

72+
#define SPL_FIXEDARRAY_OVERLOADED_REWIND 0x0001
73+
#define SPL_FIXEDARRAY_OVERLOADED_VALID 0x0002
74+
#define SPL_FIXEDARRAY_OVERLOADED_KEY 0x0004
75+
#define SPL_FIXEDARRAY_OVERLOADED_CURRENT 0x0008
76+
#define SPL_FIXEDARRAY_OVERLOADED_NEXT 0x0010
77+
7678
static void spl_fixedarray_init(spl_fixedarray *array, long size TSRMLS_DC) /* {{{ */
7779
{
7880
if (size > 0) {
@@ -209,6 +211,7 @@ static zend_object_value spl_fixedarray_object_new_ex(zend_class_entry *class_ty
209211
zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
210212

211213
intern->current = 0;
214+
intern->flags = 0;
212215

213216
if (orig && clone_orig) {
214217
spl_fixedarray_object *other = (spl_fixedarray_object*)zend_object_store_get_object(orig TSRMLS_CC);
@@ -222,6 +225,7 @@ static zend_object_value spl_fixedarray_object_new_ex(zend_class_entry *class_ty
222225
while (parent) {
223226
if (parent == spl_ce_SplFixedArray) {
224227
retval.handlers = &spl_handler_SplFixedArray;
228+
class_type->get_iterator = spl_fixedarray_get_iterator;
225229
break;
226230
}
227231

@@ -234,7 +238,30 @@ static zend_object_value spl_fixedarray_object_new_ex(zend_class_entry *class_ty
234238
if (!parent) { /* this must never happen */
235239
php_error_docref(NULL TSRMLS_CC, E_COMPILE_ERROR, "Internal compiler error, Class is not child of SplFixedArray");
236240
}
241+
if (!class_type->iterator_funcs.zf_current) {
242+
zend_hash_find(&class_type->function_table, "rewind", sizeof("rewind"), (void **) &class_type->iterator_funcs.zf_rewind);
243+
zend_hash_find(&class_type->function_table, "valid", sizeof("valid"), (void **) &class_type->iterator_funcs.zf_valid);
244+
zend_hash_find(&class_type->function_table, "key", sizeof("key"), (void **) &class_type->iterator_funcs.zf_key);
245+
zend_hash_find(&class_type->function_table, "current", sizeof("current"), (void **) &class_type->iterator_funcs.zf_current);
246+
zend_hash_find(&class_type->function_table, "next", sizeof("next"), (void **) &class_type->iterator_funcs.zf_next);
247+
}
237248
if (inherited) {
249+
if (class_type->iterator_funcs.zf_rewind->common.scope != parent) {
250+
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_REWIND;
251+
}
252+
if (class_type->iterator_funcs.zf_valid->common.scope != parent) {
253+
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_VALID;
254+
}
255+
if (class_type->iterator_funcs.zf_key->common.scope != parent) {
256+
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_KEY;
257+
}
258+
if (class_type->iterator_funcs.zf_current->common.scope != parent) {
259+
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_CURRENT;
260+
}
261+
if (class_type->iterator_funcs.zf_next->common.scope != parent) {
262+
intern->flags |= SPL_FIXEDARRAY_OVERLOADED_NEXT;
263+
}
264+
238265
zend_hash_find(&class_type->function_table, "offsetget", sizeof("offsetget"), (void **) &intern->fptr_offset_get);
239266
if (intern->fptr_offset_get->common.scope == parent) {
240267
intern->fptr_offset_get = NULL;
@@ -251,26 +278,6 @@ static zend_object_value spl_fixedarray_object_new_ex(zend_class_entry *class_ty
251278
if (intern->fptr_offset_del->common.scope == parent) {
252279
intern->fptr_offset_del = NULL;
253280
}
254-
zend_hash_find(&class_type->function_table, "next", sizeof("next"), (void **) &intern->fptr_it_next);
255-
if (intern->fptr_it_next->common.scope == parent) {
256-
intern->fptr_it_next = NULL;
257-
}
258-
zend_hash_find(&class_type->function_table, "rewind", sizeof("rewind"), (void **) &intern->fptr_it_rewind);
259-
if (intern->fptr_it_rewind->common.scope == parent) {
260-
intern->fptr_it_rewind = NULL;
261-
}
262-
zend_hash_find(&class_type->function_table, "current", sizeof("current"), (void **) &intern->fptr_it_current);
263-
if (intern->fptr_it_current->common.scope == parent) {
264-
intern->fptr_it_current = NULL;
265-
}
266-
zend_hash_find(&class_type->function_table, "key", sizeof("key"), (void **) &intern->fptr_it_key);
267-
if (intern->fptr_it_key->common.scope == parent) {
268-
intern->fptr_it_key = NULL;
269-
}
270-
zend_hash_find(&class_type->function_table, "valid", sizeof("valid"), (void **) &intern->fptr_it_valid);
271-
if (intern->fptr_it_valid->common.scope == parent) {
272-
intern->fptr_it_valid = NULL;
273-
}
274281
zend_hash_find(&class_type->function_table, "count", sizeof("count"), (void **) &intern->fptr_count);
275282
if (intern->fptr_count->common.scope == parent) {
276283
intern->fptr_count = NULL;
@@ -822,31 +829,22 @@ static void spl_fixedarray_it_rewind(zend_object_iterator *iter TSRMLS_DC) /* {{
822829
{
823830
spl_fixedarray_it *iterator = (spl_fixedarray_it *)iter;
824831
spl_fixedarray_object *intern = iterator->object;
825-
zval *object = (zval *)&iterator->intern.it.data;
826832

827-
if (intern->fptr_it_rewind) {
828-
zend_call_method_with_0_params(&object, intern->std.ce, &intern->fptr_it_rewind, "rewind", NULL);
833+
if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_REWIND) {
834+
zend_user_it_rewind(iter TSRMLS_CC);
835+
} else {
836+
iterator->object->current = 0;
829837
}
830-
iterator->object->current = 0;
831838
}
832839
/* }}} */
833840

834841
static int spl_fixedarray_it_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
835842
{
836843
spl_fixedarray_it *iterator = (spl_fixedarray_it *)iter;
837844
spl_fixedarray_object *intern = iterator->object;
838-
zval *object = (zval *)&iterator->intern.it.data;
839845

840-
if (intern->fptr_it_valid) {
841-
zval *rv;
842-
zend_call_method_with_0_params(&object, intern->std.ce, &intern->fptr_it_valid, "valid", &rv);
843-
if (rv) {
844-
zval_ptr_dtor(&intern->retval);
845-
MAKE_STD_ZVAL(intern->retval);
846-
ZVAL_ZVAL(intern->retval, rv, 1, 1);
847-
return zend_is_true(intern->retval) ? SUCCESS : FAILURE;
848-
}
849-
return FAILURE;
846+
if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_VALID) {
847+
return zend_user_it_valid(iter TSRMLS_CC);
850848
}
851849

852850
if (iterator->object->current >= 0 && iterator->object->array && iterator->object->current < iterator->object->array->size) {
@@ -859,76 +857,53 @@ static int spl_fixedarray_it_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{
859857

860858
static void spl_fixedarray_it_get_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC) /* {{{ */
861859
{
862-
zval *zindex;
860+
zval *zindex;
863861
spl_fixedarray_it *iterator = (spl_fixedarray_it *)iter;
864862
spl_fixedarray_object *intern = iterator->object;
865-
zval *object = (zval *)&iterator->intern.it.data;
866863

867-
if (intern->fptr_it_current) {
868-
zval *rv;
869-
zend_call_method_with_0_params(&object, intern->std.ce, &intern->fptr_it_current, "current", &rv);
870-
if (rv) {
871-
zval_ptr_dtor(&intern->retval);
872-
MAKE_STD_ZVAL(intern->retval);
873-
ZVAL_ZVAL(intern->retval, rv, 1, 1);
874-
*data = &intern->retval;
875-
return;
876-
}
877-
*data = NULL;
878-
return;
879-
}
864+
if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_CURRENT) {
865+
zend_user_it_get_current_data(iter, data TSRMLS_CC);
866+
} else {
867+
ALLOC_INIT_ZVAL(zindex);
868+
ZVAL_LONG(zindex, iterator->object->current);
880869

881-
ALLOC_INIT_ZVAL(zindex);
882-
ZVAL_LONG(zindex, iterator->object->current);
870+
*data = spl_fixedarray_object_read_dimension_helper(intern, zindex TSRMLS_CC);
883871

884-
*data = spl_fixedarray_object_read_dimension_helper(intern, zindex TSRMLS_CC);
872+
if (*data == NULL) {
873+
*data = &EG(uninitialized_zval_ptr);
874+
}
885875

886-
if (*data == NULL) {
887-
*data = &EG(uninitialized_zval_ptr);
876+
zval_ptr_dtor(&zindex);
888877
}
889-
890-
zval_ptr_dtor(&zindex);
891878
}
892879
/* }}} */
893880

894881
static int spl_fixedarray_it_get_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC) /* {{{ */
895882
{
896883
spl_fixedarray_it *iterator = (spl_fixedarray_it *)iter;
897884
spl_fixedarray_object *intern = iterator->object;
898-
zval *object = (zval *)&iterator->intern.it.data;
899885

900-
if (intern->fptr_it_key) {
901-
zval *rv;
902-
zend_call_method_with_0_params(&object, intern->std.ce, &intern->fptr_it_key, "key", &rv);
903-
if (rv) {
904-
zval_ptr_dtor(&intern->retval);
905-
MAKE_STD_ZVAL(intern->retval);
906-
ZVAL_ZVAL(intern->retval, rv, 1, 1);
907-
convert_to_long(intern->retval);
908-
*int_key = (ulong) Z_LVAL_P(intern->retval);
909-
}
910-
*int_key = (ulong) 0;
886+
if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_KEY) {
887+
return zend_user_it_get_current_key(iter, str_key, str_key_len, int_key TSRMLS_CC);
911888
} else {
912889
*int_key = (ulong) iterator->object->current;
890+
return HASH_KEY_IS_LONG;
913891
}
914892

915-
return HASH_KEY_IS_LONG;
916893
}
917894
/* }}} */
918895

919896
static void spl_fixedarray_it_move_forward(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
920897
{
921898
spl_fixedarray_it *iterator = (spl_fixedarray_it *)iter;
922899
spl_fixedarray_object *intern = iterator->object;
923-
zval *object = (zval *)&iterator->intern.it.data;
924900

925-
if (intern->fptr_it_next) {
926-
zend_call_method_with_0_params(&object, intern->std.ce, &intern->fptr_it_next, "next", NULL);
901+
if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_NEXT) {
902+
zend_user_it_move_forward(iter TSRMLS_CC);
903+
} else {
904+
zend_user_it_invalidate_current(iter TSRMLS_CC);
905+
iterator->object->current++;
927906
}
928-
929-
zend_user_it_invalidate_current(iter TSRMLS_CC);
930-
931-
iterator->object->current++;
932907
}
933908
/* }}} */
934909

0 commit comments

Comments
 (0)