Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112731 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 93526 invoked from network); 3 Jan 2021 17:02:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jan 2021 17:02:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5A51C1804E3 for ; Sun, 3 Jan 2021 08:37:55 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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:37:55 -0800 (PST) Received: by mail-lf1-f50.google.com with SMTP id 23so58994529lfg.10 for ; Sun, 03 Jan 2021 08:37:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=vIwRqnM5+h0nXN7F1d1p364SIykukNLMkH5UdvEjmaU=; b=HB/ddg5Y28mLJkK5xmAbywcHEj9M6BoxixqjKLMGM/aeHG2q8qNaPoWrbSxGoieWhl 41Iv6m7NIPpuoiVVyb2nupYXA+Cwhhl+2sj7Vmdvi/Pp6/w5Krs3RzowerKqTqQga4kA KhDUY1YrPuwC6gIcMjSD104hPv2tHm4TOmUvZQY1qRfRiAYQ/Dwl/D8Ewbc04OeVfvl9 EqbVHQQSJnjgBFF78U1P+Z8V7+zr6rJvSnU55GgJa52/caI/4DYmWUf2pOb06FEmjK0h eDc8MQB0d0LLtMVr6mytuc7WxGWP0hrxZg93Iz/TDUF5snOlYwGG4YteZ429eBgYEpKF psbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=vIwRqnM5+h0nXN7F1d1p364SIykukNLMkH5UdvEjmaU=; b=HTHrgMLpzEfjrhjngMLBPVq4vE/gMksxQgzwoOBYxeYrNPrRduONCZPnzj6pXY5U2w 5TxqdjkfTRFDNXJSAMfgSWIHuQllVJfREkpSOg6syNn2mFkUz5Qrt2Az/P3MilfSmT0x 8mK3vfIPbIlGURgX0zhz47PTeXp4MCBuXgTsQ0szgAjRKmr4TL5OnLG+GY/bL/x+b4XI /EQ8df8bKKd5O5wA+J6hai3fSazwEu4jPqYGKzCNHej0Ng8oQlGrrYXy2W/urCknMW1W 8paIkkq4zK0XemsePJtUPBEmPHXW7eR5z3KI7hymHlzyk4EwTYw2fZIUFcdZAjNVHKzx UBfQ== X-Gm-Message-State: AOAM532uCHSO+LbVwYgFIelXBqfVdG2Usn/1rn7ahWofUCSp5ZwZm7cJ GFJ9YMyJU92uL8bOSR7cIOvgih8wD0J2FKPpLlU= X-Google-Smtp-Source: ABdhPJzwS3TNMzdvpxVIRikNaVr96Fhwi6ABFhqNQAJkyQy+VCmeq3UxyP94LesCZGeISdalB2lOZHWiHhFH08aWM0g= X-Received: by 2002:a2e:9f06:: with SMTP id u6mr31732414ljk.194.1609691871166; Sun, 03 Jan 2021 08:37:51 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab3:7110:0:0:0:0:0 with HTTP; Sun, 3 Jan 2021 08:37:50 -0800 (PST) In-Reply-To: References: Date: Sun, 3 Jan 2021 16:37:50 +0000 Message-ID: To: Sara Golemon Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Does PHP ever stack allocate? From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2021-01-03 16:15 GMT, Sara Golemon : > 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 >> 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-servi= ces/ Olle