Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112820 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 29806 invoked from network); 9 Jan 2021 16:48:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jan 2021 16:48:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 268C41804F4 for ; Sat, 9 Jan 2021 08:25:29 -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-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (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 ; Sat, 9 Jan 2021 08:25:28 -0800 (PST) Received: by mail-lf1-f41.google.com with SMTP id v67so2250668lfa.0 for ; Sat, 09 Jan 2021 08:25:28 -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=Tymo8DLFs3k2+1iSOQhPtVdsJU5ddR8Ygih6wAeePMw=; b=YLGurJhQ3iX9oS1PQo/yCwWZnqAD9hcM71X7HUcYotynsqRsHIG2+/bE3VITVf/Slr GVJzJTjqFiuy8hG9Ce3QoQxYQQTuqqCNm0taMUxi9vvW/SFsOBHlW6RM2VUJhLcMTVGE FS0GKt3yCL5n/3PMQ3axwATEFPITLcmOyMcdVX+iSake4w4tmYlpNT2d1OBJe7XsUWJp byYq1MJD+yosoWJeobOKinpSgVdoajm4EMlUTyXZ8YrLNckQi9OT7T1syjdPSDiI1e2I BsMI4dM9g9dUhITVr70VUNvf1pNenIzT5Ceyg8slkCeRAkKWXizyBkTZiGa01AejWVTt lx4A== 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=Tymo8DLFs3k2+1iSOQhPtVdsJU5ddR8Ygih6wAeePMw=; b=Blch5bWtcx9kP94UmNjy8yMZpJlDzvQlLiaBU23PYVZaoVnmjofXTiBgJZkW9t/suf ZU2QDqB0SUuWYazSgHJccIBbptR691RNKrzg0rfVA4DrR/iPca1Om64WumFl6KoMBrjh oLVle8A6kq9e0lS0PimM5FzglmRz6siqvC7CPOk3mR4LCKYrVMYsdWeaXFiBovMjaAtL mFx0nFFuC/hGxAyFO+BVAd5JR2bHzuQsq0VtLwd3XVPqSBagFGKjaiGTsijugCbZTzCi tLReJmf3Za51t3PXy8tAHSs1sDBcAQE4D/Vsw3he95uhIHpxuSGRsuq8kkfDZQA1aWRM ViKg== X-Gm-Message-State: AOAM530vifCMim4sJtKXZGCvNZP0q3UdqFstPf8a87vw2OAPaIZ9ns1a gazTx3R+FTH9+Fuw94qOCmZTIiG4UhyQCCZanR0= X-Google-Smtp-Source: ABdhPJxF0RTvDDwqvDNbzV6enespXn7jH4AANMiEUyegN/AchrAzEXATq+M+fGv1UmmOpTaU88DNmG78GN0hsJ0OW8w= X-Received: by 2002:a2e:b610:: with SMTP id r16mr4064589ljn.470.1610209527044; Sat, 09 Jan 2021 08:25:27 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab3:7190:0:0:0:0:0 with HTTP; Sat, 9 Jan 2021 08:25:26 -0800 (PST) In-Reply-To: References: Date: Sat, 9 Jan 2021 16:25:26 +0000 Message-ID: To: Nikita Popov Cc: tyson andre , Sara Golemon , 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-05 10:51 GMT, Nikita Popov : > 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 whe= n >> >> 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-se= rvices/ >> > >> > 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 o= n >> > 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_= analysis.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 fol= d > 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 line= s > of dropping the object and storing its properties as local variables > instead. Hm, this is called scalar replacement, I think. Olle