Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110958 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16683 invoked from network); 11 Jul 2020 22:55:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Jul 2020 22:55:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EBBAE18053C for ; Sat, 11 Jul 2020 14:47:26 -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.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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.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, 11 Jul 2020 14:47:26 -0700 (PDT) Received: by mail-ej1-f41.google.com with SMTP id rk21so10078900ejb.2 for ; Sat, 11 Jul 2020 14:47:26 -0700 (PDT) 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=4U42LjHoMbNiyOGFxW+tFGcb1zDWzk7a/sCN5zuwqZ4=; b=DLYVqE8BWIblmDjWP4XM7yfwVp9UH/U+OvDPcIiCFqUa7PBKcZUigAL1oSEv7Q5ZPf bfJATTivZpTeI9Z8CLDLfTedr8So6anu6dVIYUcPEx1CLuGrvla6gYs4i3fY54lM0gTQ G3OK9/Y72PFHxZF5hp+Y/F60ykprivMEX1Cxe2mBx0OWQA1nYwwO/6vRPjFL3Q4OSCPi /EvNusWfl1y0d4lGivY0US4jei0ErDejKqQ5u//PWAuQj4uV6nnjU55bDSDQoCRjQRGx MKC/hmkdskeK1psjEFaMi9wdI7SwyXsET6q0riGWagd7aimXyaGxolBNWHDOx/Pb6Kw8 RHnQ== 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=4U42LjHoMbNiyOGFxW+tFGcb1zDWzk7a/sCN5zuwqZ4=; b=keSkygm1rgslxIbmI05+OudQ8SuTwjnkWX6JiJ5DV/UGT/73aiJ0i1mWQaUJqsQXXT 7bu79azibUHyx1M9tLaIObvWtFL8x2OsYYULo7rOuMi9rEEi0R2hdeGvUx5gZ9A8zGqg g+g3QYKTHS0zMaCTn63Bkc2rB5uNoc9fRtN+INUYupARhAndJOcBZXBAspZ9lauBDYzk oDSZHusELJDzeYteuxPYQAaaTWhbwUyhNqAvnlptsxUjPiX7r99SPIkDpi15CQ2BLbmV KCerpUIlIlwMXaMGh2WeFh6RvTTksQmNAwIPAz7NVqUdP2QDNbSqHfPYRmxY0FdZlrjf MfNQ== X-Gm-Message-State: AOAM530rUWs+d249bBd6eARpSZyUnqPlnTHMZfHmfGE2h6clYqFksBBi 0M3BO22tK60BTIyCbmN4QZP488fweSG4i0yRNQ0KKw== X-Google-Smtp-Source: ABdhPJzizL4CblgDfLeaJ+Wbn2+IgUWmZ9+xFSOs+SDYTTiUCI6jvqtsEf/l66vOkoL3ZXtAiaHDxDgyNsZYMsiopKA= X-Received: by 2002:a17:907:11db:: with SMTP id va27mr70277932ejb.175.1594504042145; Sat, 11 Jul 2020 14:47:22 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a54:3091:0:0:0:0:0 with HTTP; Sat, 11 Jul 2020 14:47:21 -0700 (PDT) In-Reply-To: <3bfb89c5-c8fd-4587-8c1f-8b7bbbaca692@www.fastmail.com> References: <1e39fdcb-16d2-42c3-95cf-507f7b647b50@www.fastmail.com> <3bfb89c5-c8fd-4587-8c1f-8b7bbbaca692@www.fastmail.com> Date: Sat, 11 Jul 2020 21:47:21 +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] Possible RFC: UniqueInterface that throws exception at refcount > 1 From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2020-07-11 21:25 GMT, Larry Garfield : > On Sat, Jul 11, 2020, at 4:04 PM, Olle H=C3=A4rstedt wrote: > >> > 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 >> > >> > -- >> > PHP Internals - PHP Runtime Development Mailing List >> > To unsubscribe, visit: https://www.php.net/unsub.php >> >> I think freezing objects might be better fit for my imagined use-case. >> The only problem I see is that you can't really unfreeze them. Imagine >> a database connection that only can be open/closed at refcount =3D 1: >> >> ``` >> $connection =3D new OwnershipConnection(); >> $connection->open(); >> $ps =3D new PostService($connection); >> $ps->updateAllPosts(); // Throws exception if $connection->close() >> $connection->close(); >> ``` >> >> With freeze, you could also do >> >> ``` >> $ps =3D new PostService($connection->freeze()); >> ``` >> >> to ensure it's not closed by mistake. But then you couldn't close the >> connection at all, except in __destruct. >> >> Especially, CoW for objects (at opt-in) is not a replacement where >> ownership is supposed to replace immutability for performance reason, >> e.g. creating a separate immutable database connection for every class >> that uses it. >> >> Immutable builder are already part of PSR, e.g. here: >> https://www.php-fig.org/psr/psr-7/ >> >> I have to wonder how reasonable this is, when freezing or ownership >> are also relevant solutions, with different trade-offs. >> >> Olle > > A database connection is the textbook example of an object that *should* = be > shared; giving a separate connection to every service object is only goin= g > to waste time opening multiple connections and waste open connections. > Please don't do that. > > Your example seems directly contrary to the goal you're describing. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > You're misunderstanding. :) Yes, it should be shared, but not with the same access level as the owner. If a dependent class closes the connection it will cause spooky-action-at-a-distance somewhere else in the program. Sorry, it's related to refcount as ownership checking and not to throwing exception at refcount > 1. More like, "limit access at refcount > 1" (at borrow, that is). Reference counting can be used for both poor man's ownership semantics and to enforce non-aliasing. So yeah, maybe I should just suggest that something like this extension gets merged into vanilla PHP: https://github.com/qix/php_refcount (proper way to get refcount instead of a zval dump). Or maybe make zval dump return something instead of dumping a string. Olle