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.

>> 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

Nice link, thanks!

Olle

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to