Skip to content

JIT: Optimize array checks in comparisons - #24084

Merged
ndossche merged 1 commit into
php:masterfrom
ndossche:jit-array-check
Oct 8, 2026
Merged

ndossche merged 1 commit into
php:masterfrom
ndossche:jit-array-check

Conversation

@ndossche

@ndossche ndossche commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

There are two optimizations:

  1. This patch implements the JIT counterpart of the VM change 2d6b869 to optimize comparisons against empty arrays.
  2. This patch ensures that if a type can be array, the element count check happens inline rather than going to a slow path.

PHP demo code for #1:

function f(array $a) {
	if ($a) return 0;
	return 1;
}

Before #1 (for func JIT in this case):

.L1:
	movq $0x418d88c0, (%r14)        ; EX(opline) = opline
	leaq 0x50(%r14), %rdi           ; arg = &CV0($a)
	movabsq $zend_is_true, %rax
	callq *%rax
	testb %al, %al
	je .L2

After #1:

.L1:
	movq 0x50(%r14), %rax           ; Z_ARRVAL($a)
	cmpl $0, 0x1c(%rax)             ; compare nNumOfElements
	je .L2

PHP demo code for #2:

function is_empty(array $a) {
	if ($a === []) return 1;
	return 2;
}

Before #2:

.L1:
	leaq 0x50(%r14), %rdi           ; arg1 = &CV0($a)
	movl $0x418d8ad8, %esi          ; arg2 = &literal []
	movabsq $zend_is_identical, %rax
	callq *%rax
	testb %al, %al
	je .L2

After #2:

.L1:
	movq 0x50(%r14), %rax           ; Z_ARRVAL($a)
	cmpl $0, 0x1c(%rax)             ; compare nNumOfElements
	jne .L2

It ain't much, but it should help.
I think many small frontend improvements can make a nice change in total together.

There are two optimizations:
1. This patch implements the JIT counterpart of the VM change 2d6b869 to
   optimize comparisons against empty arrays.
2. This patch ensures that if a type can be array, the element count
   check happens inline rather than going to a slow path.

PHP demo code for #1:
```php
function f(array $a) {
	if ($a) return 0;
	return 1;
}
```

Before #1 (for func JIT in this case):
```asm
.L1:
	movq $0x418d88c0, (%r14)        ; EX(opline) = opline
	leaq 0x50(%r14), %rdi           ; arg = &CV0($a)
	movabsq $zend_is_true, %rax
	callq *%rax
	testb %al, %al
	je .L2
```

After #1:
```asm
.L1:
	movq 0x50(%r14), %rax           ; Z_ARRVAL($a)
	cmpl $0, 0x1c(%rax)             ; compare nNumOfElements
	je .L2
```

PHP demo code for #2:
```php
function is_empty(array $a) {
	if ($a === []) return 1;
	return 2;
}
```

Before #2:
```asm
.L1:
	leaq 0x50(%r14), %rdi           ; arg1 = &CV0($a)
	movl $0x418d8ad8, %esi          ; arg2 = &literal []
	movabsq $zend_is_identical, %rax
	callq *%rax
	testb %al, %al
	je .L2
```

After #2:
```asm
.L1:
	movq 0x50(%r14), %rax           ; Z_ARRVAL($a)
	cmpl $0, 0x1c(%rax)             ; compare nNumOfElements
	jne .L2
```

It ain't much, but it should help.
I think many small frontend improvements can make a nice change in
total together.

@arnaud-lb arnaud-lb left a comment

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.

Looks good to me!

@arnaud-lb

Copy link
Copy Markdown
Member

I think many small frontend improvements can make a nice change in total together.

I agree, especially when it reduces code size like this

@ndossche
ndossche merged commit 78d56e0 into php:master Oct 8, 2026
18 checks passed
arnaud-lb added a commit to frodeborli/php-src that referenced this pull request Oct 9, 2026
* up/master: (180 commits)
  Changed the test expected result of `pdo_mysql/bug76815_pdo_mysql_f to %d (php#13808)
  ext/standard: name the real parameter in the unpack() offset error (php#24215)
  ext/readline: Refactor CLI readline completion generators
  Fix phpGH-24081: User opcode DISPATCH runs on a stale frame in the TAILCALL VM
  ext/standard: Validate the bcrypt cost before reading it
  JIT: Avoid object type check if the object is known to be a type (php#24086)
  zend_alloc: move a small block shrunk to the size of the bin below
  Fix phpGH-23979: Nullsafe operator must not flush delayed oplines of an enclosing function
  ext/zip: Reject ZipArchive mutators during close() (php#24025)
  Fix OSS-Fuzz #568005340: FETCH_DIM_FUNC_ARG partial conversion
  Fix too wide type inference for ASSIGN_DIM_OP
  Fix type inference of ADD_ARRAY_UNPACK with integer keys
  Evaluate ZEND_SPACESHIP in SCCP
  Add range inference for SPACESHIP
  JIT: Optimize array checks in comparisons (php#24084)
  Fix leak when the added previous exception is already in the chain (php#24177)
  date: Add `php_date_time_duration_create()` in a new `time_duration.h` (php#24072)
  ext/zip: Fix use-after-free in the archive destructor path (php#23779)
  ext/standard: Optimize array_chunk() by filling packed chunks directly
  Fix phpGH-17626: JIT corrupts opline handler when blacklisting root trace
  ...
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