For the above code, the current implementation requires 4 allocations, while with this patch only one allocation is required.
Adaptability
This patch is not effective when the vector is referenced by any variables, including $this.
This means not even calling $this->xxx() from a subclass of Vector3 would utilize this optimization,
nor $foo->xxx() (even if $foo is no longer used).
This patch is not effective in some chain calls. For example, this does not optimize anything in $event->getPlayer()->add().
Drawbacks
This requires users to install ext-thisrc. A polyfill or cpp-based alternative may be used to support non-thisrc users.
This involves extra function call overhead in the thisrc() calls. ext-thisrc may be implemented at a lower level of PHP internals (rather than just as a PHP_FUNCTION) may be useful, but that would be much more challenging.
Tasks
[x] Adopt thisrc in Vector3
[ ] Adopt thisrc in Vector2
[ ] Implement polyfill for users without ext-thisrc installed
[ ] Perform benchmarks to contrast the overhead of thisrc() against overhead of cloning. In particular, benchmark this in PocketMine, where not too many Vector3 calls are chained, to see if this makes anything worse.
This pull request optimizes unique-reference calls to Vector3, which minimizes object allocations for long call chains like this:
For the above code, the current implementation requires 4 allocations, while with this patch only one allocation is required.
Adaptability
This patch is not effective when the vector is referenced by any variables, including
$this
. This means not even calling$this->xxx()
from a subclass ofVector3
would utilize this optimization, nor$foo->xxx()
(even if$foo
is no longer used).This patch is not effective in some chain calls. For example, this does not optimize anything in
$event->getPlayer()->add()
.Drawbacks
This requires users to install ext-thisrc. A polyfill or cpp-based alternative may be used to support non-thisrc users.
This involves extra function call overhead in the
thisrc()
calls. ext-thisrc may be implemented at a lower level of PHP internals (rather than just as aPHP_FUNCTION
) may be useful, but that would be much more challenging.Tasks
Vector3
Vector2
thisrc()
against overhead of cloning. In particular, benchmark this in PocketMine, where not too many Vector3 calls are chained, to see if this makes anything worse.