Skip to content

perf(optimizer): inline count for known array variables - #98

Merged
matyhtf merged 2 commits into
swoole:masterfrom
yavon007:codex/optimize-known-array-count
Sep 8, 2026
Merged

perf(optimizer): inline count for known array variables#98
matyhtf merged 2 commits into
swoole:masterfrom
yavon007:codex/optimize-known-array-count

Conversation

@yavon007

@yavon007 yavon007 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Single-argument count($items) still calls the generic php::fn::count when $items already has a fixed array representation. The runtime checks the type before returning the array's stored element count.

Lower that case to static_cast<php::Int>(items.count()). The signed cast preserves PHP integer arithmetic. The shortcut accepts only known local array variables and retains argument-safety checks; recursive/two-argument calls, global wrappers, dynamic values, objects and compound expressions keep their existing paths.

Adds codegen and runtime regression coverage plus a standalone benchmark with raw results and reproduction instructions.

Performance

Linux ARM64 in Docker, PHP 8.5.10 ZTS, PHPX 6a68f38, GCC -O2, no LTO; baseline 692841a6. A 30-million-iteration array-update/count workload decreased from a median 319.63 ms to 192.73 ms (39.70% less elapsed time). All nine alternating measured pairs were faster after a discarded warm-up pair; checksums matched native PHP. Array updates keep the count from being a loop-invariant-only measurement.

These are isolated workload measurements on a shared development machine, not an application-wide improvement or a comparison of execution speed against native PHP.

Validation

  • CountLiteralFoldTest|CountGlobalsTest: 6 tests / 10 assertions pass; the added codegen test fails on the original implementation.
  • Four targeted PHPTs pass: known-array mutation/recursive/COW counting, Countable dispatch, invalid-type errors, and global-variable access. Codegen coverage also checks that $GLOBALS and $_SERVER retain runtime calls.
  • Native PHP and compiled benchmark checksums match (2999995050).
  • git diff --check passes. Full-suite and cross-platform validation were not run.

@matyhtf
matyhtf merged commit f3e2e32 into swoole:master Sep 8, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants