Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112683 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2834 invoked from network); 30 Dec 2020 20:42:17 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Dec 2020 20:42:17 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D598E1804DC for ; Wed, 30 Dec 2020 12:17:03 -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-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (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 12:17:03 -0800 (PST) Received: by mail-lf1-f45.google.com with SMTP id m12so40020509lfo.7 for ; Wed, 30 Dec 2020 12:17:03 -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=Ivxf9ewN9v7qOQNzDeDq0kqHAOHRpbkFzi3Ws0aADYY=; b=jo9hi0kE0dcKGi6KCjv0Cm0pfpiEH2ZdeleyqN/NAYNP99A0AWCNVf+onJR3cIkhoR mXEeLMZdjuauqAIsxcsCS20vVriPX+EZCnWWlhOpEKPcOr+YE1Knkpe1Fj6NnBeRIPcH /hphK4cb8Nknk+7fVuGkhWhAn2sArH0uFaTFYBJ6ijWfs9liqAHuaTGoi9drOLv0Y6pr wYxh5pMjhTFhomabG3MK/8xDpCLvv3zk6vCVCvAVJYyyR0j5WSwsLW4nf41Bb4Nzt77y qUX+bcnZGNKtgScFXGE75MA1N66QcHUw8pg4Zotytpr8XjPkEeI/yBvNmILuguOwCg20 BX9Q== 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=Ivxf9ewN9v7qOQNzDeDq0kqHAOHRpbkFzi3Ws0aADYY=; b=nKZg6LAd4/KjDRqSZqQ7Kk5PnEVAKVAOGQT0Ya+MoVD9KZyIEK4qDPURcMGGKreXNi wJm8m/+ZYLy28qkckZL6gMQ0XNI3/HvOMqesPy9+NEs/bUTVXA+yBUc9epKA7axuPnTz X+RzJT0b3JasfYZ7BlM5wUivo+68sstsL7VORGyoBna8hn9L8eZ0aqKI5Mj09Ru2hbXV Tfnw8Tqi8SGJyU25ninaxSCsBgjzycg73FtEWWk5nPavKMaH6a5kb7270oo/MVA0iPkJ cyHKV4Z4deejtykqUoOEIUHCLAKJ93wkC1HckE+gS7xf+MHq0r+msoWjx1STSUa5RtC5 kHPQ== X-Gm-Message-State: AOAM532/MB2BasmQyB6/irQFggJeIFOy/wXv/xsN+i7KKe9Uo1RWoSe8 7B1Ik5haNDeQPoEaIAqjkIMiHTgZtNZX+yPCUbw= X-Google-Smtp-Source: ABdhPJwND/VhJAHSxsXlxMo8IzZMif/BkIzCFVaLhjQcLU5Do8Ey6lOhu+7CNeCyXALicXJnLGldQkhwA47/h2foC3c= X-Received: by 2002:a05:651c:512:: with SMTP id o18mr25413645ljp.367.1609359419780; Wed, 30 Dec 2020 12:16:59 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab3:7110:0:0:0:0:0 with HTTP; Wed, 30 Dec 2020 12:16:58 -0800 (PST) In-Reply-To: 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> Date: Wed, 30 Dec 2020 20:16:58 +0000 Message-ID: To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Analysis of property visibility, immutability, and cloning proposals From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2020-12-30 19:50 GMT, Olle H=C3=A4rstedt : > 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 u= p >> some, >> > > but far less than you think. (Again, based on the tests MWOP ran fo= r >> 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, th= e >> 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 d= o >> 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 zva= l >> 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 th= e >> 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 =3D $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 quer= y > 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. Tiny benchmark here: https://gist.github.com/olleharstedt/eaaf1dd40be541f84aa0f3954a0ea54a Running this on my ARM machine with PHP 7.2 gives ~1.2s for the immutable loop, ~0.35s for the mutable one, meaning immutability is ~3x as expensive performance wise. And this is for a SMALL object - I suspect the performance hit will grow the bigger the class gets (more properties to clone). Correct me if I'm wrong. :/ Olle