Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112677 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 91224 invoked from network); 30 Dec 2020 19:52:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Dec 2020 19:52:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D13401804CF for ; Wed, 30 Dec 2020 11:27:04 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 30 Dec 2020 11:27:03 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id D6D64444 for ; Wed, 30 Dec 2020 14:27:02 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute4.internal (MEProxy); Wed, 30 Dec 2020 14:27:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=uu7tXA i+2e3Xxe9pujOy8Vg1XIgHCOTF91ZyRYEGu3c=; b=qtJrgvVbGUCFqjP2PepxP4 7tJgIVebQQ7i+xzeeG/swYxMoMdLI3xbAY/x4zjdA5xTjWZd4trcTrXiS58PYSLy eJ47IuxA9qmt5ZZhC8DUThLzhp1fkKuxer5ASiPI0jlbkYky34sv/NAL6PZMToE5 lq7hMgYlMeF3nLLIU8c8P8sdw/7xWysqRYN0taEUQFcv0WXoFC8wM30dR3rc4QDO LeWYvBwjmDzFhFk36yzbxw4fVrkaTZXu4XqsZKfeLkzAWO4LpnWnP9VNNSInvB4U RqhivcG3+5mbXOyx2R12X4mKKcqM5J4uOUH2dUL7LJKaIWYtSouNcItzkqmOnALg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvddvfedguddvhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnheplefhjeduueegudeghfehudfgvdeflefffeeuteev uedtjeffkeetkeeiveejkefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 1B4B214200A2; Wed, 30 Dec 2020 14:27:02 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.1-61-gb52c239-fm-20201210.001-gb52c2396 Mime-Version: 1.0 Message-ID: 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 13:26:25 -0600 To: "php internals" Content-Type: text/plain Subject: =?UTF-8?Q?Re:_[PHP-DEV]_Analysis_of_property_visibility,_immutability,_a?= =?UTF-8?Q?nd_cloning_proposals?= From: larry@garfieldtech.com ("Larry Garfield") > > 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. That's why I don't think the distinction between unique and immutable mentioned up-thread is that big of a deal in PHP, specifically. Yes, they're different things, but the cost of them is not all that different because of CoW, so considering them separately is not as important as it would be in a language that doesn't automatically do CoW in the background for us. (Whoever in the 90s decided to bake CoW into the engine, thank you. It's an incredibly nice foundational feature.) --Larry Garfield