On Mon, Jan 4, 2021 at 11:53 AM Olle Härstedt <olleharst...@gmail.com>
wrote:

> 2021-01-03 17:43 GMT, tyson andre <tysonandre...@hotmail.com>:
> > Hi Olle,
> >
> >> Thanks Sara! I realize I should have been more precise: Can PHP
> >> allocate non-reference counted memory that automatically is freed when
> >> leaving scope, similar to what Go does with escape analysis?
> >>
> >> Article describing the Go mechanism:
> >>
> https://segment.com/blog/allocation-efficiency-in-high-performance-go-services/
> >
> > Could you give some concrete examples of what type of code you're talking
> > about?
> > As Sara Golemon said, scalars (null, bool, int, float) are allocated on a
> > php call frame,
> > and the call frames go on a stack. That stack is separate from the C
> stack,
> > but still a stack
> >
> > The call frame is "freed" when leaving scope - i.e. that part of the
> stack
> > will be reused on subsequent calls.
>
> Sure. Consider the following trivial snippet:
>
> ```php
> class Point {
>   public $x;
>   public $y;
> }
> function foo() {
>   $p = new Point();
>   return $p->x + $p->y;
> }
> ```
>
> Since we know the lifetime of $p, we don't have to ref count it.
> Escape analysis helps with checking lifetimes and to remove needles
> ref counting (and heap allocations in compiled languages). There is a
> file in php-src for this:
>
> https://github.com/php/php-src/blob/master/ext/opcache/Optimizer/escape_analysis.c
>
> My question was about how this functionality is used.
>

I believe the escape analysis is currently only used as part of SCCP to
perform constant folding on objects. For example, we would be able to fold
the following function down to "return 3;":

function foo() {
    $p = new Point();
    $p->x = 1;
    $p->y = 2;
    return $p->x + $p->y;
}

However, I think the optimization you have in mind is more along the lines
of dropping the object and storing its properties as local variables
instead.

I don't think the escape analysis is particularly useful outside of
stdClass possibly, as it will bail on classes from different files, and
classes with constructors anyway.

Nikita

Reply via email to