Performance testing: Bind static variables as non-references. #18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The goal is to see if this is worth doing in opcache.
This creates a static variable that is a value instead of a reference,
subsequent assignments such as
$x = null;
do not modify the originaldeclaration.
(If $x was previously a reference before the
static
var statement,it continues to be unset before the
expression on the right hand side is assigned to $x)
(e.g.
static $x : = [new ArrayObject(['foo' => 'bar'])]
Locally benchmark until implementing changes so that opcache can
infer when it is safe to bind a
static $var
with $var as anon-reference (e.g. only used by value, no foreach by ref, no $$ variable
access or functions such as extract, no
require*/include*
statements,not in the top-level file scope, etc.).
For example,
function (): SomeClass { static $var = new SomeClass(); return $var; }
is likely much less efficient as a non-reference rather than a static variable
but opcache doesn't attempt to optimize it.
In php 8.1, the workaround of using
private const SOME_METHOD_LOCAL_OBJ = new SomeClass();
exists but initialization later than that may be useful, e.g. if initializing SomeClass or its dependencies is slow.Work out how to make the opcache changes correctly and safely
Being able to do this automatically may result in more efficient code, especially with the opcache JIT compiler
This benchmark was around 3% faster, for example.