Re: Does PHP ever stack allocate?
| From: | tyson andre | Date: | Sun, 03 Jan 2021 17:43:24 +0000 |
| Subject: | Re: Does PHP ever stack allocate? | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112734@lists.php.net to get a copy of this message | ||
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.
> A single PHP call frame holds a block of storage space for (among other
> things) all* local variables. This can be thought of analogously to "the
> stack" as it's used by native applications. Basic scalars (null, bool,
> int, float) sit in this space with no additional pointers to anywhere.
> Non-scalars use pointers to elsewhere in the heap to store the actual
> payload. This isn't unique to PHP, as these structures have runtime
> determined size and thus can't** be stack allocated.
https://nikic.github.io/2017/04/14/PHP-7-Virtual-machine.html
may help if you want to learn more about what the PHP VM currently does
> So what’s the difference between TMP and VAR? Not much.
> The distinction was inherited from PHP 5, where TMPs were VM stack allocated,
> while VARs were heap allocated. In PHP 7 all variables are stack allocated.
> As such, nowadays the main difference between TMPs and VARs is that only the latter are allowed
> to contain REFERENCEs
> (this allows us to elide DEREFs on TMPs). Furthermore VARs may hold two types of special
> values,
> namely class entries and INDIRECT values. The latter are used to handle non-trivial
> assignments.
-Tyson