Skip to content

support op_add in CollectConstVarsPass#1625

Open
apolyakov wants to merge 4 commits into
masterfrom
apolyakov/support-constant-op-add
Open

support op_add in CollectConstVarsPass#1625
apolyakov wants to merge 4 commits into
masterfrom
apolyakov/support-constant-op-add

Conversation

@apolyakov
Copy link
Copy Markdown
Contributor

@apolyakov apolyakov commented May 2, 2026

This PR extends the CollectConstVarsPass compiler optimization to support the + operator (array union/addition) for constant expressions. This moves evaluation of constant arr + arr expressions from request execution time to kPHP initialization stage.

Implementation Details:
The optimization is currently limited to arr + arr case only. While the optimization would be safe for some other cases as well (the runtime handles type coercion), K2 runtime's implementation of string addition requires RuntimeContext to be initialized (which is not initialized at the time of constant initialization stage). To ensure compatibility with both legacy and K2 runtimes, we restrict this optimization to array addition only.

Example:
Before this change, an expression like ['a' => 1] + ['b' => 2] would be evaluated on every request. After this change, it's evaluated once during initialization and stored as a constant variable.

@apolyakov apolyakov self-assigned this May 2, 2026
@apolyakov apolyakov added optimization Memory comsumption / CPU speedup compiler Feature related to compiler k2 Affects compiler or runtime in K2 mode kphp Affects compiler or runtime in default mode (not K2) labels May 2, 2026
@apolyakov apolyakov force-pushed the apolyakov/support-constant-op-add branch from 6b173c4 to f35ed92 Compare May 9, 2026 13:54
@apolyakov apolyakov added this to the next milestone May 10, 2026
@apolyakov apolyakov marked this pull request as ready for review May 12, 2026 08:15
Copy link
Copy Markdown
Contributor

@PetrShumilov PetrShumilov left a comment

Choose a reason for hiding this comment

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

OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler Feature related to compiler k2 Affects compiler or runtime in K2 mode kphp Affects compiler or runtime in default mode (not K2) optimization Memory comsumption / CPU speedup

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants