2021-01-03 16:15 GMT, Sara Golemon <poll...@php.net>: > On Fri, Jan 1, 2021 at 3:18 PM Olle Härstedt <olleharst...@gmail.com> > wrote: > >> Or is everything reference counted with heap allocation? Since PHP has >> escape analysis, this could be used to use the stack instead, and kill >> the >> memory when the scope ends. If PHP uses the stack, can this be seen in >> the >> opcode? >> >> > Well, you're not going to like this answer, but.... yes and no. > > 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. > > There's further asterii below all of those statements, but that's the > high-level generalized answer to your question as posed. > > The implied question you asked is actually handled using another > mechanism. PHP's internals have two separate memory allocation pools. > "Persistent" memory allocation, which creates blocks of memory for the > lifetime of the process, and "Engine" memory allocation, which are bulk > de-allocated*** at the end of every request (after relevant > destructors have fired). > > -Sara > > * All variables which are explicitly references as $foo (excluding > auto-globals). ${'bar'} and $$baz style references to locals are special > and require their own separate conversation. > ** C's alloca() and similar techniques in other languages can reserve > dynamic amounts of stack space, but let's ignore that power-move for the > sake of this argument. > *** Deallocated from the request handler's point of view, though the > runtime's memory manager (usually) doesn't give it back to the OS > immediately, since it's likely to be used by the next request anyway. >
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/ Olle -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php