Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112758 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99443 invoked from network); 5 Jan 2021 11:15:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Jan 2021 11:15:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 81A681804C6 for ; Tue, 5 Jan 2021 02:51:52 -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,HTML_MESSAGE, 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-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (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 ; Tue, 5 Jan 2021 02:51:51 -0800 (PST) Received: by mail-lf1-f51.google.com with SMTP id h205so71524625lfd.5 for ; Tue, 05 Jan 2021 02:51:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Y5pKvdfRenOthpqm7liVitkYa1nmi/G6Tz2cXM6I8Q8=; b=GZ5PBSYpv/Q5P74q91fvpiPS9p11obhxNSuWAZAzFJqwKf63OVicCMuEVdhufVftgZ OYN2OV8boeSxPCV9r2V7UIjMyweyLUTmYZVFCzUckcXOHdF7Z+9s1W05GNfD8bx6LQbb Zb7b2bkEVXACQqzdw+QhoujITXOJ7Z64ooE1A3fqnQkYira2YkA5Dpq5NcEuLL3AKivY f5WuVu3lgtP+J4pzAOAEjjNiwtWT+iLHdxLSdJBzqyOF556er4ukCBLL/Uqsn2hmAcmi 96OgELIWPUDrA2FTltEmM0U1JhGwKM2ffU8svJ4V2cM1shPKGhLuhjLhcu4fZpUHfDMr NUTA== 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=Y5pKvdfRenOthpqm7liVitkYa1nmi/G6Tz2cXM6I8Q8=; b=j8ICxE0ngDlXjBYOXn4a0BQFwi1SMP+1YfnKCpFno8s2iWMDAVkS03U/2arDW0xdDD 89Gkhc5LsZ5JcFcVsFvHDYbNZRVDuxVsyyoK2Hphgp9WuT2ZB2BCR2/kvv4m1m8L39A8 9PxdM2tv9oxSMpkXszHZ88Zhq6IXJg97lNKdubT0KgGwNIxDi8uBFNHeKif12zlse35e HGn+UrI8VeFPlD53bxox/Q1TFV2qt+756kT47oiLxNHc9DauqgTDTcZd5RE3kDNHD6Ip +OOEuPNKw6kJmswp2x+fX7cZoHNxw8X1Y3rsbLfOqPzi0aTXIdR7h0bQOV3dnlNtmlRO kScw== X-Gm-Message-State: AOAM530rRoWmwmWTTuWSLJ/udJV/qzfDFSXC+ASN3y7AnTdCAk66t+bt SYRJicnuhZqa8SuDwG9PgSQWjxWyMAXCjCn5Ciw= X-Google-Smtp-Source: ABdhPJz+NrQTlShV5BJnmcfzEIuRCPvti23VrrzpPVDdTnO1dBvLpqMed9IBU7F5gtAJRmwUKK2EPwARhJe57lqRir0= X-Received: by 2002:a2e:9f14:: with SMTP id u20mr39371452ljk.244.1609843909472; Tue, 05 Jan 2021 02:51:49 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 5 Jan 2021 11:51:33 +0100 Message-ID: To: =?UTF-8?Q?Olle_H=C3=A4rstedt?= Cc: tyson andre , Sara Golemon , PHP internals Content-Type: multipart/alternative; boundary="0000000000005e352905b824fe07" Subject: Re: [PHP-DEV] Does PHP ever stack allocate? From: nikita.ppv@gmail.com (Nikita Popov) --0000000000005e352905b824fe07 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jan 4, 2021 at 11:53 AM Olle H=C3=A4rstedt wrote: > 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-ser= vices/ > > > > Could you give some concrete examples of what type of code you're talki= ng > > 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 =3D 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_a= nalysis.c > > My question was about how this functionality is used. > I believe the escape analysis is currently only used as part of SCCP to perform constant folding on objects. For example, we would be able to fold the following function down to "return 3;": function foo() { $p =3D new Point(); $p->x =3D 1; $p->y =3D 2; return $p->x + $p->y; } However, I think the optimization you have in mind is more along the lines of dropping the object and storing its properties as local variables instead. I don't think the escape analysis is particularly useful outside of stdClass possibly, as it will bail on classes from different files, and classes with constructors anyway. Nikita --0000000000005e352905b824fe07--