Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112682 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98880 invoked from network); 30 Dec 2020 20:16:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Dec 2020 20:16:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2DE50180384 for ; Wed, 30 Dec 2020 11:51:07 -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-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 ; Wed, 30 Dec 2020 11:51:06 -0800 (PST) Received: by mail-lf1-f43.google.com with SMTP id b26so39897096lff.9 for ; Wed, 30 Dec 2020 11:51:06 -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=IF7T9TVewd+p7h8kg3EEWZJ+sspvhJDqgZevk7HYyHA=; b=fw5GrQKZoSkz9rXsi4tSItSi44AxkgSm5WcxPlMw/Yt7u8JBgPghcDtaNXexl066Ad dfdFNg2hqy5L3yXMvO3zSBCZPK3RFV9DcHOBoFQj75v1N9QNGR8HBc9vwyHeKc9BcEXm cw6bH4a9XA25VG+dDlb4D8K5QkJW2eG2JUtfCt+7TXDTS5vgOzZ5CVnX7IYIQC1VOmkc 6RXpfcgm9AJyGunMB77n3sxQxueRg/j14UBgUsiYLiTo5tlt3NOXKGWf+OxxHHqRJAvl 7nKdJs0oAiBlDh7Y35UtYg+xetSbykcYjnJNoTtAp/3QTDXCqPsEcYpoctp46EYUazQE 6/rg== 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=IF7T9TVewd+p7h8kg3EEWZJ+sspvhJDqgZevk7HYyHA=; b=X4kBHUHdAehFL/+4OSyAHjKUw0Md0HEJrVhKUYeiXxCP35dpIL9XdkjuTYZf3HApDV zVdbuLw+9b8MlG5GkJUqPGJyV+vA6F42v2r25qWSdrGR6nZKLZK4I37S/YnxAKi2i0zi ZSQ1SpDByjMoqdBgplUbPixnH4cYyMplzvy9c0XboFzMWBM21CI3RLLDg9x6AfMEeSx7 E/xud4VnrB2w09LCn9zhSSrQAS11jvoUpNM3VvhW3xxpgzv3fCSJ/jlBlNLrtWm8iapl LVte/iG3wIbgckfY784xPRr13dlpFVjbTpmKBIHNV4G9nBcSX9rTx7NUnHw/gVu1K3xX hk5w== X-Gm-Message-State: AOAM533cl7sem8mkaS9TACk7jyGRZUfzyabkjaPLkCoSk2aoq0X6afxO dOiFN1tZJN/a9bAepMQXbomaWWzRlmIUrfGaW4MzxTFzCfU= X-Google-Smtp-Source: ABdhPJzof0dq2IX3CDj33r8FW3vfBZikDalirbNPGg4vIR5R0+S4o5SozwjuW3jy03cqm0X3T2lG2FqcHWQIzoi7Glk= X-Received: by 2002:a2e:9f06:: with SMTP id u6mr24497106ljk.194.1609357863798; Wed, 30 Dec 2020 11:51:03 -0800 (PST) MIME-Version: 1.0 References: <1d0abb04-4987-43a9-85bc-bccc3bd6be9a@www.fastmail.com> <03108284-740a-4a5d-130f-15b2e67e9df9@mabe.berlin> <459d7ff7-e553-dce9-7d43-c3b1e772e572@gmail.com> <7f4fe9ca-1c20-6f69-cef0-a9718af742a3@gmail.com> <3e683903-66cb-4a1a-9ff8-22887dbb8dce@www.fastmail.com> In-Reply-To: Date: Wed, 30 Dec 2020 20:50:50 +0100 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000c9cceb05b7b3d34b" Subject: Re: [PHP-DEV] Analysis of property visibility, immutability, and cloning proposals From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) --000000000000c9cceb05b7b3d34b Content-Type: text/plain; charset="UTF-8" On Wed, 30 Dec 2020, 20:27 Larry Garfield, wrote: > > > > That's a good summary of why immutability and with-er methods (or some > > > equivalent) are more ergonomic. > > > > > > Another point to remember: Because of PHP's copy-on-write behavior, > full on > > > immutability doesn't actually waste that much memory. It does use up > some, > > > but far less than you think. (Again, based on the tests MWOP ran for > PSR-7 > > > a ways back.) > > > > I thought copy-on-write was only for arrays, not objects? > > > > Olle > > Copy on write applies to all values; the caveat is that with objects, the > value being copied is the handle that points to an object in memory, rather > than the object itself. That means passing an object by reference can do > some seriously unexpected things, which is why you basically never do so. > > The point here is that if you have an object with 15 internal properties, > it's memory usage is 15 zvals plus one zval for the object, plus one zval > for the variable that points to it. (I'm over-simplifying here. A lot.) > If you pass it to a function, only the one zval for the handle is > duplicated, which is the same as for an integer. > > If you clone the object, you don't duplicate 15+1 zvals. You duplicate > just the one zval for the object itself, which reuses the existing 15 > internal property entries. If in the new object you then update just the > third one, PHP then duplicates just that one internal zval and modifies the > new one. So you still are using only 18 zvals, not 36 zvals. (Engine > people: Yes, I am *very* over-simplifying. I know.) > > Basically, what in most languages would require manually implementing > "immutable data structures" we get for free in PHP, which is seriously > sweet. > > The net result is that a with-er chain like this: > > $foo2 = $foo->withBar('x')->withBaz('y')->withBeep('z'); > > is way, way less expensive than it looks, both on memory and CPU. It is > more expensive than setters, but not by much. > Ok. You have a benchmark for this? I can make one otherwise, for the query example. It worries me a little that immutablility is pushed into the ecosystem as a silver bullet. Main reason functional languages are using it is because ownership is a newer concept, so it hasn't been adapted as much. > --000000000000c9cceb05b7b3d34b--