Skip to content

Fetch dim r cache slot #157

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 6 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
6 changes: 6 additions & 0 deletions Zend/Optimizer/compact_literals.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,12 @@ void zend_optimizer_compact_literals(zend_op_array *op_array, zend_optimizer_ctx
cache_size += 2 * sizeof(void *);
}
break;
case ZEND_FETCH_DIM_R:
if (opline->op1_type != IS_CONST && opline->op2_type == IS_CONST) {
opline->extended_value = cache_size;
cache_size += sizeof(void *);
}
break;
}
opline++;
}
Expand Down
7 changes: 5 additions & 2 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3082,6 +3082,9 @@ static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t

opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
zend_adjust_for_fetch_type(opline, result, type);
if (type == BP_VAR_R && opline->op1_type != IS_CONST && opline->op2_type == IS_CONST) {
opline->extended_value = zend_alloc_cache_slot();
}
if (by_ref) {
opline->extended_value = ZEND_FETCH_DIM_REF;
}
Expand Down Expand Up @@ -8361,10 +8364,10 @@ static zend_op_array *zend_compile_func_decl_ex(
"nodiscard",
sizeof("nodiscard")-1
);

if (nodiscard_attribute) {
op_array->fn_flags |= ZEND_ACC_NODISCARD;
}
}
}

/* Do not leak the class scope into free standing functions, even if they are dynamically
Expand Down
94 changes: 94 additions & 0 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -10265,6 +10265,100 @@ ZEND_VM_HOT_TYPE_SPEC_HANDLER(ZEND_QM_ASSIGN, ((op->op1_type == IS_CONST) ? !Z_R
ZEND_VM_NEXT_OPCODE();
}

ZEND_VM_HOT_TYPE_SPEC_HANDLER(ZEND_FETCH_DIM_R, (op->op1_type != IS_CONST && op->op2_type == IS_CONST && !(op2_info & (MAY_BE_ANY-MAY_BE_STRING-MAY_BE_LONG))), ZEND_FETCH_DIM_R_CACHED_INDEX, TMPVAR|CV, CONST, CACHE_SLOT)
{
USE_OPLINE

zval *container = GET_OP1_ZVAL_PTR_UNDEF(BP_VAR_R);
zval *dim = GET_OP2_ZVAL_PTR_UNDEF(BP_VAR_R);
zval *value;

if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
ZEND_VM_C_LABEL(fetch_dim_r_const_index_array):;
HashTable *ht = Z_ARRVAL_P(container);
if (Z_TYPE_P(dim) == IS_LONG) {
zend_long offset = Z_LVAL_P(dim);
if (HT_IS_PACKED(ht)) {
if (EXPECTED((zend_ulong)offset < (zend_ulong)ht->nNumUsed)) {
value = &ht->arPacked[offset];
if (EXPECTED(Z_TYPE_P(value) != IS_UNDEF)) {
ZEND_VM_C_LABEL(fetch_dim_r_cached_index_found):
ZVAL_COPY_DEREF(EX_VAR(opline->result.var), value);
if (OP1_TYPE & (IS_TMP_VAR|IS_VAR)) {
SAVE_OPLINE();
FREE_OP1();
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
} else {
ZEND_VM_NEXT_OPCODE();
}
}
}
ZEND_VM_C_GOTO(fetch_dim_r_cached_index_undef);
}
uintptr_t cached_offset = (uintptr_t)CACHED_PTR(opline->extended_value);
if (cached_offset && ht->nNumUsed >= cached_offset) {
Bucket *b = &ht->arData[cached_offset - 1];
if (!b->key && b->h == offset) {
value = &b->val;
ZEND_VM_C_GOTO(fetch_dim_r_cached_index_found);
}
}

value = _zend_hash_index_find(ht, offset);
ZEND_VM_C_LABEL(fetch_dim_r_cached_index_check):
if (EXPECTED(value)) {
Bucket *bucket = (Bucket*)((uintptr_t)value - XtOffsetOf(Bucket, val));
CACHE_PTR(opline->extended_value, (void *)(bucket - ht->arData + 1));
ZEND_VM_C_GOTO(fetch_dim_r_cached_index_found);
} else {
ZEND_VM_C_GOTO(fetch_dim_r_cached_index_undef);
}
} else {
if (UNEXPECTED(HT_IS_PACKED(ht))) {
ZEND_VM_C_GOTO(fetch_dim_r_cached_index_undef);
}
uintptr_t cached_offset = (uintptr_t)CACHED_PTR(opline->extended_value);
if (cached_offset && ht->nNumUsed >= cached_offset) {
Bucket *b = &ht->arData[cached_offset - 1];
if (b->key && zend_string_equals(b->key, Z_STR_P(dim))) {
value = &b->val;
ZEND_VM_C_GOTO(fetch_dim_r_cached_index_found);
}
}

value = zend_hash_find(ht, Z_STR_P(dim));
ZEND_VM_C_GOTO(fetch_dim_r_cached_index_check);
}
} else if (EXPECTED(Z_TYPE_P(container) == IS_REFERENCE)) {
container = Z_REFVAL_P(container);
if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
ZEND_VM_C_GOTO(fetch_dim_r_const_index_array);
} else {
ZEND_VM_C_GOTO(fetch_dim_r_const_index_slow);
}
} else {
ZEND_VM_C_LABEL(fetch_dim_r_const_index_slow):
if (OP2_TYPE == IS_CONST && Z_EXTRA_P(dim) == ZEND_EXTRA_VALUE) {
dim++;
}
SAVE_OPLINE();
zend_fetch_dimension_address_read_R_slow(container, dim OPLINE_CC EXECUTE_DATA_CC);
FREE_OP1();
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}

ZEND_VM_C_LABEL(fetch_dim_r_cached_index_undef):
SAVE_OPLINE();
ZVAL_NULL(EX_VAR(opline->result.var));
if (Z_TYPE_P(dim) == IS_LONG) {
zend_undefined_offset(Z_LVAL_P(dim));
} else {
zend_undefined_index(Z_STR_P(dim));
}
FREE_OP1();
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}

ZEND_VM_HOT_TYPE_SPEC_HANDLER(ZEND_FETCH_DIM_R, (!(op2_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))), ZEND_FETCH_DIM_R_INDEX, CONST|TMPVAR|CV, CONST|TMPVARCV, SPEC(NO_CONST_CONST))
{
USE_OPLINE
Expand Down
Loading