Skip to content

Performance testing: Bind static variables as non-references. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Zend/Optimizer/zend_inference.c
Original file line number Diff line number Diff line change
Expand Up @@ -3012,8 +3012,13 @@ static zend_always_inline zend_result _zend_update_type_info(
UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
break;
case ZEND_BIND_STATIC:
/**
* NOTE: External PECL functions such as uopz_set_static allow modifying static variables,
* so they really can have any type.
* This is used in some unit testing frameworks to restore state after a unit test runs.
*/
tmp = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF
| ((opline->extended_value & ZEND_BIND_REF) ? MAY_BE_REF : (MAY_BE_RC1 | MAY_BE_RCN));
| ((((opline->extended_value & (ZEND_BIND_REF|ZEND_BIND_ACTUALLY_NON_REF)) == ZEND_BIND_REF)) ? MAY_BE_REF : (MAY_BE_RC1 | MAY_BE_RCN));
if (opline->extended_value & ZEND_BIND_IMPLICIT) {
tmp |= MAY_BE_UNDEF;
}
Expand Down Expand Up @@ -4409,7 +4414,8 @@ static void zend_mark_cv_references(const zend_op_array *op_array, const zend_sc
}
break;
case ZEND_BIND_STATIC:
if (!(opline->extended_value & ZEND_BIND_REF)) {
if ((opline->extended_value & (ZEND_BIND_REF|ZEND_BIND_ACTUALLY_NON_REF)) != ZEND_BIND_REF) {
/** Anything other than `static $x [= $val];` or `use(&$x) will not create references */
continue;
}
break;
Expand Down Expand Up @@ -4795,11 +4801,11 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
return (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY));
case ZEND_BIND_STATIC:
if (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)) {
/* Destructor may throw. */
/* Destructor may throw when the new value replaces the old value. */
return 1;
} else {
zval *value = (zval*)((char*)op_array->static_variables->arData + (opline->extended_value & ~(ZEND_BIND_REF|ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT)));
/* May throw if initializer is CONSTANT_AST. */
zval *value = (zval*)((char*)op_array->static_variables->arData + (opline->extended_value & ~ZEND_BIND_BITFLAG_COMBINATION));
/* May throw if initializer is CONSTANT_AST (i.e. an AST instead of a value, because it wasn't precomputed at compile time). */
return Z_TYPE_P(value) == IS_CONSTANT_AST;
}
case ZEND_ASSIGN_DIM:
Expand Down
1 change: 1 addition & 0 deletions Zend/Optimizer/zend_ssa.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ static zend_always_inline zend_ssa_phi* zend_ssa_next_use_phi(const zend_ssa *ss
return NULL;
}

/* Returns true if this is not a use of a previous assignment to var in this operation in the SSA graph. */
static zend_always_inline bool zend_ssa_is_no_val_use(const zend_op *opline, const zend_ssa_op *ssa_op, int var)
{
if (opline->opcode == ZEND_ASSIGN
Expand Down
21 changes: 21 additions & 0 deletions Zend/tests/static_variable_initialize_non_ref.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Static variable initialize by non-reference (:=)
--FILE--
<?php
function test_append($value) {
static $singleton := new ArrayObject();
$singleton->append($value);
$values = $singleton->getArrayCopy();

$singleton = 'object overwritten by value (not by ref)';
return [$values, $singleton];
}
for ($i = 0; $i < 3; $i++) {
echo json_encode(test_append("x$i")), "\n";
}

?>
--EXPECT--
[["x0"],"object overwritten by value (not by ref)"]
[["x0","x1"],"object overwritten by value (not by ref)"]
[["x0","x1","x2"],"object overwritten by value (not by ref)"]
2 changes: 1 addition & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4746,7 +4746,7 @@ static void zend_compile_static_var(zend_ast *ast) /* {{{ */
ZVAL_NULL(&value_zv);
}

zend_compile_static_var_common(zend_ast_get_str(var_ast), &value_zv, ZEND_BIND_REF);
zend_compile_static_var_common(zend_ast_get_str(var_ast), &value_zv, ZEND_BIND_REF | ast->attr);
}
/* }}} */

Expand Down
3 changes: 3 additions & 0 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -1043,10 +1043,13 @@ static zend_always_inline bool zend_check_arg_send_type(const zend_function *zf,
#define ZEND_RETURN_VAL 0
#define ZEND_RETURN_REF 1

/* These bit flags currently must be less than the zval size of 16 bytes, unless ZEND_VM_HANDLER for ZEND_BIND_STATIC is adjusted. */
#define ZEND_BIND_VAL 0
#define ZEND_BIND_REF 1
#define ZEND_BIND_IMPLICIT 2
#define ZEND_BIND_EXPLICIT 4
#define ZEND_BIND_ACTUALLY_NON_REF 8
#define ZEND_BIND_BITFLAG_COMBINATION (ZEND_BIND_REF|ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT|ZEND_BIND_ACTUALLY_NON_REF)

#define ZEND_RETURNS_FUNCTION (1<<0)
#define ZEND_RETURNS_VALUE (1<<1)
Expand Down
5 changes: 3 additions & 2 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,9 @@ static_var_list:
;

static_var:
T_VARIABLE { $$ = zend_ast_create(ZEND_AST_STATIC, $1, NULL); }
| T_VARIABLE '=' expr { $$ = zend_ast_create(ZEND_AST_STATIC, $1, $3); }
T_VARIABLE { $$ = zend_ast_create(ZEND_AST_STATIC, $1, NULL); }
| T_VARIABLE '=' expr { $$ = zend_ast_create(ZEND_AST_STATIC, $1, $3); }
| T_VARIABLE ':' '=' expr { $$ = zend_ast_create(ZEND_AST_STATIC, $1, $4); $$->attr = ZEND_BIND_ACTUALLY_NON_REF; }
;

class_statement_list:
Expand Down
11 changes: 7 additions & 4 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -8715,18 +8715,21 @@ ZEND_VM_HANDLER(183, ZEND_BIND_STATIC, CV, UNUSED, REF)
}
ZEND_ASSERT(GC_REFCOUNT(ht) == 1);

value = (zval*)((char*)ht->arData + (opline->extended_value & ~(ZEND_BIND_REF|ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT)));
value = (zval*)((char*)ht->arData + (opline->extended_value & ~ZEND_BIND_BITFLAG_COMBINATION));

if (opline->extended_value & ZEND_BIND_REF) {
const uint32_t extended_value = opline->extended_value;
if (extended_value & ZEND_BIND_REF) {
if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
SAVE_OPLINE();
if (UNEXPECTED(zval_update_constant_ex(value, EX(func)->op_array.scope) != SUCCESS)) {
HANDLE_EXCEPTION();
}
}

i_zval_ptr_dtor(variable_ptr);
if (UNEXPECTED(!Z_ISREF_P(value))) {

if (extended_value & ZEND_BIND_ACTUALLY_NON_REF) {
ZVAL_COPY_DEREF(variable_ptr, value);
} else if (UNEXPECTED(!Z_ISREF_P(value))) {
zend_reference *ref = (zend_reference*)emalloc(sizeof(zend_reference));
GC_SET_REFCOUNT(ref, 2);
GC_TYPE_INFO(ref) = GC_REFERENCE;
Expand Down
12 changes: 8 additions & 4 deletions Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -47400,18 +47400,21 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_BIND_STATIC_SPEC_CV_UNUSED_HAN
}
ZEND_ASSERT(GC_REFCOUNT(ht) == 1);

value = (zval*)((char*)ht->arData + (opline->extended_value & ~(ZEND_BIND_REF|ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT)));
value = (zval*)((char*)ht->arData + (opline->extended_value & ~ZEND_BIND_BITFLAG_COMBINATION));

if (opline->extended_value & ZEND_BIND_REF) {
const uint32_t extended_value = opline->extended_value;
if (extended_value & ZEND_BIND_REF) {
if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
SAVE_OPLINE();
if (UNEXPECTED(zval_update_constant_ex(value, EX(func)->op_array.scope) != SUCCESS)) {
HANDLE_EXCEPTION();
}
}

i_zval_ptr_dtor(variable_ptr);
if (UNEXPECTED(!Z_ISREF_P(value))) {

if (extended_value & ZEND_BIND_ACTUALLY_NON_REF) {
ZVAL_COPY_DEREF(variable_ptr, value);
} else if (UNEXPECTED(!Z_ISREF_P(value))) {
zend_reference *ref = (zend_reference*)emalloc(sizeof(zend_reference));
GC_SET_REFCOUNT(ref, 2);
GC_TYPE_INFO(ref) = GC_REFERENCE;
Expand All @@ -47425,6 +47428,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_BIND_STATIC_SPEC_CV_UNUSED_HAN
ZVAL_REF(variable_ptr, Z_REF_P(value));
}
} else {
copy_raw_value:
i_zval_ptr_dtor(variable_ptr);
ZVAL_COPY(variable_ptr, value);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,7 @@ ZEND_METHOD(ReflectionFunctionAbstract, getClosureUsedVariables)

Bucket *bucket = (Bucket*)
(((char*)static_variables->arData) +
(opline->extended_value & ~(ZEND_BIND_REF|ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT)));
(opline->extended_value & ~ZEND_BIND_BITFLAG_COMBINATION));

if (Z_ISUNDEF(bucket->val)) {
continue;
Expand Down