Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110953 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 90575 invoked from network); 11 Jul 2020 20:17:32 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Jul 2020 20:17:32 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CB41B180533 for ; Sat, 11 Jul 2020 12:09:16 -0700 (PDT) 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 out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.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 ; Sat, 11 Jul 2020 12:09:16 -0700 (PDT) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 6B68B5C006C for ; Sat, 11 Jul 2020 15:09:15 -0400 (EDT) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Sat, 11 Jul 2020 15:09:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding: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=fm3; bh=bcd2wXcpindxMLjgbpra2if5PtSiM1jVYawSnuUJr X4=; b=SsfYtA/aef2wtJ4g8al40RcwDUEkMJImj1OU0nje0eTu5O3B4GZjNrSyc iBwalBXjG7lVlS24k0kUoPWVdOpGi/nMyYWxdnmL/eFhUgSaSmMpPoRG0LqYmikQ sCNhpuX7hi4OnN2viLcUFSv13R9QgmUjTOWPjVweSjJ/anygzHUi+5EWuJeUmAW2 APt5J2M4edLPQ+0f8TmH2teAcbu6GJQi0M3EsHh7ZzYjrnP0YaH4ro9v1EsjI3ei M9iJB13cL1Wp0518h86/+74eQco36EML1AngF78FRBAWyY8g97lzhqoeoc+wb6Dd Nrrv63VLQRxXqoOvVYIR2YFzyDISw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrvdefgddufeeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepffffffejffdugfegvedviedttedvgfejffefffej leefjeetveehgefhhfdvgfelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 0EA9B14200A2; Sat, 11 Jul 2020 15:09:15 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.0-dev0-613-g8a73ad6-fm-20200709.001-g8a73ad6e Mime-Version: 1.0 Message-ID: <1e39fdcb-16d2-42c3-95cf-507f7b647b50@www.fastmail.com> In-Reply-To: References: Date: Sat, 11 Jul 2020 14:08:54 -0500 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: =?UTF-8?Q?Re:_[PHP-DEV]_Possible_RFC:_UniqueInterface_that_throws_except?= =?UTF-8?Q?ion_at_refcount_>_1?= From: larry@garfieldtech.com ("Larry Garfield") On Sat, Jul 11, 2020, at 9:06 AM, Olle H=C3=A4rstedt wrote: > Dear internals, >=20 > I'd like to discuss the concept of ownership in PHP, from the point of= > view of a new interface UniqueInterface (or SplUniqueInterface or > something), which will throw a UniqueException if refcount > 1. >=20 > Use-case: Mutable objects that cannot be shared (without (deep) clonin= g). >=20 > Rational: Immutable objects are considered "easier to reason about" > and generally more safe, and as a way to avoid > spooky-action-at-a-distance. I argue that mutability is totally fine > as long as it's not *being shared*. Currently, there's no way to > enforce this in PHP, *but* PHP has a refcounting GC which could do > this. This would be a more performant alternative than to build lots > of immutable objects using $obj->whereSomething('bla') constructs. >=20 > Cons: If PHP switches to tracing GC, it will be unusable. >=20 > What do you think? Should I write it down as an RFC? Implementation > should be straight-forward, assuming refcount is manipulated at a > single place in the source. >=20 > Regards > Olle I like the goal of addressing shared mutable state is a laudable one and= I think most here share it. However, I think uniqueness checking is th= e wrong approach, for reasons others have already explained. You're correct that the thing to avoid is shared mutable state. That ca= n be avoided either by getting rid of mutable state or shared state. Ei= ther would work. (Channeling Kevlin Henny here.) In the case of PHP, since we don't have threads to worry about the main = source of shared state is reference variables, or refrence-esque objects= . (Or globals, but I'm assuming most here know to avoid those already.)= That is, mutability or shared-ness within a function is, meh, who care= s. It's at function boundaries that we care. As others noted, a ref count check wouldn't help with that much, because= the act of passing an object to another function necessarily increases = the refcount, thus eliminating the main benefit. Rather, the way to eliminate shared-ness is to have "structs," that is, = objects that pass by value-ish rather than pass-by-reference-ish. (Yes = it's more complicated than that, I know, I'm talking about the user-spac= e implications.) PHP 4 objects all worked that way, which was a problem= for anything with services our resources behind it. That's why it chan= ged in PHP 5 to the more common by-ref-ish behavior, which... is a probl= em for value objects. Both use cases are valid. It's why newer languages like Go and Rust tak= e a completely different approach to what "object" even means in the fir= st place. It's been discussed before but never really implemented; I do think the = way to help break the "shared state" side is to allow objects to get fla= gged as "will use by-value passing" or "will use by-reference passing" (= again, implications, not implementation). That is, if you have a by-val= object and pass it to a function, it behaves the same way as passing a = string or an array: Copy-on-write of the object when any changes are mad= e to properties of the object. Probably we'd also need to require that = properties of by-val objects cannot be by-ref objects or resources, whic= h seems reasonable to me. I think that would do a far better job of addressing the shared-mutable-= state issue than reference counting, because it attacks the root problem= rather than a symptom. --Larry Garfield