Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112745 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 79981 invoked from network); 4 Jan 2021 11:17:20 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Jan 2021 11:17:20 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9BBB0180547 for ; Mon, 4 Jan 2021 02:53:16 -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=-0.4 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,URIBL_SBL, URIBL_SBL_A autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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 ; Mon, 4 Jan 2021 02:53:15 -0800 (PST) Received: by mail-lf1-f43.google.com with SMTP id s26so63213445lfc.8 for ; Mon, 04 Jan 2021 02:53:15 -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; bh=WQItBYPXedoepqaZA3wvg76HuRo36+NsDbMHcKUI8Ng=; b=TrY7hwdVH6RWPBWaUcKXsRKsj+e5F3oJkkALiiDl532CfmmfDsfR3WrJfIsurOKRPQ /cYiGuBYkkq+L9l6O2gX5/e5zuDQ0T9ELdEdKdZZaIUg0japssquCUXI0fCq7P2lfZAk FCovuG5J06+Kw+p8wb07Pe85DDGGkx675shSZ9GBv/5IJWsMxT9X5D5vHx57MsYEd05/ REdyr9Uh89C5lVDukW89mr1VKRxohSgbfuPs/8BRt9bZXeumISTRb1L+LqrxZy1ttml9 JleItJ0iT4S5GsvZkgXojTY3b1fcmcoCgxiLckRnTi45srTaj/7qCnCvkXffOUoQwYU4 570A== 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; bh=WQItBYPXedoepqaZA3wvg76HuRo36+NsDbMHcKUI8Ng=; b=WlDMbGQ2ycOHJ2rl0jGbqDEWxWB+wylq/nDnxZhornKaPowCzqnZJqkokHGleVP1b/ GTggMA1bRgxCSDJzdWVJ5pWzhQB+NdvxWp+AXL84LtMwtqMvcUrQOXE3D6OzzEe1ZMwl ztINfRmCjopYM4/l3GX3RZJSojgu0WdzvOsBaYIQns7lYE8J5fh0folJMtWaF/qAHW5j H9VFGfe82q//h01LOdPNEBLVmBNJo0bGoZLZ+PY8ywWAWWMb7HVVC84jK6yIW6ZVuDbN G34JF10fkNu8nNG04Ts7SfFeI4HYRQM2QcTKy72EOhj+/eCgVVUmTpRcm2lsvrhQY63I 7sWg== X-Gm-Message-State: AOAM530UddsY0PLDLw9fRUz+D/rsTwweD5a7fhJgNyqh8+sonXkSG0c5 6O7hR0UwAZVcu06R1G+LVvaUbYwUMghX0pWSh94= X-Google-Smtp-Source: ABdhPJwVy+SqvujtkNPn+0cb1DDQdTV92pD8EWxXp0t+d6ASzOIUHMnCl5XA+onyU4pysKzDILqvkmlLvx+6peCf/NA= X-Received: by 2002:a19:cb05:: with SMTP id b5mr26747111lfg.61.1609757592979; Mon, 04 Jan 2021 02:53:12 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab3:7110:0:0:0:0:0 with HTTP; Mon, 4 Jan 2021 02:53:12 -0800 (PST) In-Reply-To: References: Date: Mon, 4 Jan 2021 10:53:12 +0000 Message-ID: To: tyson andre Cc: Sara Golemon , PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Does PHP ever stack allocate? From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2021-01-03 17:43 GMT, tyson andre : > 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