Re: Does PHP ever stack allocate?
| From: | Olle Härstedt | Date: | Sat, 09 Jan 2021 16:25:26 +0000 |
| Subject: | Re: Does PHP ever stack allocate? | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112820@lists.php.net to get a copy of this message | ||
2021-01-05 10:51 GMT, Nikita Popov <nikita.ppv@gmail.com>:
> On Mon, Jan 4, 2021 at 11:53 AM Olle Härstedt <olleharstedt@gmail.com>
> wrote:
>
>> 2021-01-03 17:43 GMT, tyson andre <tysonandre775@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.
Hm, this is called scalar replacement, I think.
Olle