Skip to content

JIT: Minor optimizations to static property access - #25381

Merged
ndossche merged 3 commits into
php:masterfrom
ndossche:jit-optimize-static-prop-access
Nov 11, 2026
Merged

ndossche merged 3 commits into
php:masterfrom
ndossche:jit-optimize-static-prop-access

Conversation

@ndossche

Copy link
Copy Markdown
Member

This PHP code:

<?php
final class Foo {
    public static $prop = 1;
    function foo() {
        $test = 0;
        for ($i = 0; $i < 100; $i++)
            $test += static::$prop;
        return $test;
    }
}

(new Foo)->foo();

previously generated these loop checks:

.L3:
	cmpq $0x64, %rbx
	jge jit$$trace_exit_1
	movq 0x20(%r14), %rax ; dependency for scope check
	movq 0x40(%r14), %rcx ; load runtime cache ptr
	movq 8(%rcx), %rcx ; scope check
	cmpq 0x10(%rax), %rcx ; scope check
	jne .L5 ; scope check
	movq 0x40(%r14), %rax ; load runtime cache ptr
	movq 0x10(%rax), %rax
	testq %rax, %rax
	je .L5
	cmpb $0, 8(%rax)
	je .L6

Notice how the commented lines are unnecessary: the runtime cache ptr is loaded twice, and the dependency isn't necessary for this final CE case.

Now it generates this:

.L3:
	cmpq $0x64, %rbx
	jge jit$$trace_exit_1
	movq 0x40(%r14), %rcx
	movq 0x10(%rcx), %rax
	testq %rax, %rax
	je .L5
	cmpb $0, 8(%rax)
	je .L6

This is almost optimal, the last check isn't necessary but that's out of scope here.

Comment thread ext/opcache/jit/zend_jit_ir.c Outdated
&& op_array->function_name
&& op_array->scope
&& (op_array->scope->ce_flags & ZEND_ACC_FINAL)
&& !(op_array->scope->ce_flags & ZEND_ACC_TRAIT)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this check is redundant. final trait T {} is a parse error, so a trait scope never has ZEND_ACC_FINAL, and the check above already excludes it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're correct, I added this after checking for TRAIT_CLONE below, but didn't realize that the combination with final already excludes a trait

@ndossche
ndossche merged commit 90fb1e6 into php:master Nov 11, 2026
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants