Skip to content

Fix GH-13193: Significant performance degradation in 'foreach' starting from PHP 8.2.13 (caused by garbage collection) #13265

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

Merged
merged 2 commits into from
Jan 30, 2024
Merged
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
41 changes: 38 additions & 3 deletions Zend/zend_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1465,14 +1465,19 @@ static int gc_remove_nested_data_from_buffer(zend_refcounted *ref, gc_root_buffe
}

static void zend_get_gc_buffer_release(void);
static void zend_gc_root_tmpvars(void);
static void zend_gc_check_root_tmpvars(void);
static void zend_gc_remove_root_tmpvars(void);

ZEND_API int zend_gc_collect_cycles(void)
{
int total_count = 0;
bool should_rerun_gc = 0;
bool did_rerun_gc = 0;

if (GC_G(num_roots) && GC_G(gc_active)) {
zend_gc_remove_root_tmpvars();
}

rerun_gc:
if (GC_G(num_roots)) {
int count;
Expand Down Expand Up @@ -1669,7 +1674,7 @@ ZEND_API int zend_gc_collect_cycles(void)

finish:
zend_get_gc_buffer_release();
zend_gc_root_tmpvars();
zend_gc_check_root_tmpvars();
return total_count;
}

Expand Down Expand Up @@ -1707,7 +1712,7 @@ static void zend_get_gc_buffer_release(void) {
* cycles. However, there are some rare exceptions where this is possible, in which case we rely
* on the producing code to root the value. If a GC run occurs between the rooting and consumption
* of the value, we would end up leaking it. To avoid this, root all live TMPVAR values here. */
static void zend_gc_root_tmpvars(void) {
static void zend_gc_check_root_tmpvars(void) {
zend_execute_data *ex = EG(current_execute_data);
for (; ex; ex = ex->prev_execute_data) {
zend_function *func = ex->func;
Expand Down Expand Up @@ -1737,6 +1742,36 @@ static void zend_gc_root_tmpvars(void) {
}
}

static void zend_gc_remove_root_tmpvars(void) {
zend_execute_data *ex = EG(current_execute_data);
for (; ex; ex = ex->prev_execute_data) {
zend_function *func = ex->func;
if (!func || !ZEND_USER_CODE(func->type)) {
continue;
}

uint32_t op_num = ex->opline - ex->func->op_array.opcodes;
for (uint32_t i = 0; i < func->op_array.last_live_range; i++) {
const zend_live_range *range = &func->op_array.live_range[i];
if (range->start > op_num) {
break;
}
if (range->end <= op_num) {
continue;
}

uint32_t kind = range->var & ZEND_LIVE_MASK;
if (kind == ZEND_LIVE_TMPVAR || kind == ZEND_LIVE_LOOP) {
uint32_t var_num = range->var & ~ZEND_LIVE_MASK;
zval *var = ZEND_CALL_VAR(ex, var_num);
if (Z_REFCOUNTED_P(var)) {
GC_REMOVE_FROM_BUFFER(Z_COUNTED_P(var));
}
}
}
}
}

#ifdef ZTS
size_t zend_gc_globals_size(void)
{
Expand Down