Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112730 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 91010 invoked from network); 3 Jan 2021 16:39:40 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jan 2021 16:39:40 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4697A1804D4 for ; Sun, 3 Jan 2021 08:15:25 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: *** X-Spam-Status: No, score=3.2 required=5.0 tests=BAYES_20, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 3 Jan 2021 08:15:24 -0800 (PST) Received: by mail-lf1-f46.google.com with SMTP id m12so58969198lfo.7 for ; Sun, 03 Jan 2021 08:15:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=h3v8zsfiTAZaET4R/2CScAk8skxqGPfMDEHiy4luc4w=; b=ih76lvY2pYOf0yH7v/9/bzY2bMGYQMdYtDxBsVgXnhH7CPtIHLHu36ZpdJCJ0s65A1 7n0JFybxOBmOWdIOSB1ie2Tb/35sboB76NxD+RJrF/UIPeXW11gAWokyms7xescXXo1Q gBfdTFQQJTjg3H16hLrRg6YLxoQhNnWW0Wx4gpgz5nEyJKYzKxBfD0C5NiFpPTjWSv20 mQZLj8MridZ2YCmaDokonftIFiccafqe7XwEj+sGrBcSBNG8kn5bhY2OEQ+46j9kJCn8 Q9ML25B1katFQce2lA9Ob0A00513X9sPoQSOAwAjBbr3Q8Xz/KWih2WX2lFgjV1ozI1Z VNdQ== X-Gm-Message-State: AOAM5323yQnOnClKs3gQ38RdYxxXKrn1yZF/i6Xqvs1m+LOQR1WM9Lkf 0cvMr4QctKFH45wEfI+zBLnKs14eCTEbbfR+obFe2A== X-Google-Smtp-Source: ABdhPJzcKZJNthDUPZrJYbQSczyGHyAZutb1hp5ltJss/fefR6S8YWnyWUo1L8fKjBzExONBPFLG7G4RZ93QOXhvr6A= X-Received: by 2002:ac2:5b4f:: with SMTP id i15mr28913918lfp.572.1609690521469; Sun, 03 Jan 2021 08:15:21 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 3 Jan 2021 10:15:10 -0600 Message-ID: To: =?UTF-8?Q?Olle_H=C3=A4rstedt?= Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000bb1e7105b80147dd" Subject: Re: [PHP-DEV] Does PHP ever stack allocate? From: pollita@php.net (Sara Golemon) --000000000000bb1e7105b80147dd Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Jan 1, 2021 at 3:18 PM Olle H=C3=A4rstedt = 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 th= e > memory when the scope ends. If PHP uses the stack, can this be seen in th= e > 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. --000000000000bb1e7105b80147dd--