Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124361 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id B57BD1A011A for ; Wed, 10 Jul 2024 17:56:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1720634303; bh=IAT0ahyG1xsGZOFqjXgPTVgS1urVGVSQtxGMtwejJ+g=; h=In-Reply-To:References:Date:From:To:Cc:Subject:From; b=MJDU1JAJpbtoqZba9cyDol8iIhr9f+h2bpk2/CbrGfmGOCZkqmJajht07qCBKFxwX H5YRHDSOxkZTdLygyA9vDEgaFDkWO0Tz9aLf1u94ZP11g8M2RSq9cih+5Zcw0x8A85 /Mh+Ou1Mm86cyFNwrZ5KEDUy0LzstWdpPf6g+HVUvK1tIwXpIekmt/ynszpLnGeqcQ sLBcAPyIJW/S6jPjXIT7Inmu1kWwLknp3lXvpGMqWla8Rhth4DDaizmsK/BUP+jpHZ GtJcCg4CuTZ6L1zMG9KmZsJirIoBb12qqeoG/7my36tmgDnzPk3AaoGuyPhNmKUW3k UgCiPU3gNXPRQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CB9B81806DF for ; Wed, 10 Jul 2024 17:58:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout2-smtp.messagingengine.com (fout2-smtp.messagingengine.com [103.168.172.145]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 10 Jul 2024 17:58:18 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.nyi.internal (Postfix) with ESMTP id 1E1421380643; Wed, 10 Jul 2024 13:56:52 -0400 (EDT) Received: from imap49 ([10.202.2.99]) by compute1.internal (MEProxy); Wed, 10 Jul 2024 13:56:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm3; t=1720634212; x= 1720720612; bh=IAT0ahyG1xsGZOFqjXgPTVgS1urVGVSQtxGMtwejJ+g=; b=k 4G55ewouSbJLjI02UNT6SE/4HoOcMWXnOtVSyCwM2pURE61pskta8psaPTrPBUhn HO7LKKRTvbIeVlwoL5fyoWxyqe2ZrhFIoHaqp0k3CilMUlGTa/KyVuC7bn/IjJ2r Z0E6RTezwuQWmhkCWwvRYTdDnZmUoghXVvK7bPl4IbJ8DYYQYFpZemJxJhGAGjOz TaP3HNJ8VkMB9S2Sn4nmni9ABdSx6gIOrK6abuUyvzjEjfprxaJhdWVUW9aqBym7 jCCHE8chOWm0La8nr8Q5Oxrlq/G/xWXhDBuxqLCOj8e2vxiSUS8bj0vk4gw2Ty6t CCcP8yd6AALDxMk4Al1uQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1720634212; x=1720720612; bh=IAT0ahyG1xsGZOFqjXgPTVgS1urV GVSQtxGMtwejJ+g=; b=PBU8sI9eY1WuG0X+wu8IeG6JA+kMy/Ih+naljOBZQDQy JGNJQE+yQPfC74UZKU1fub0o8Oa0Nz08/JLUCbwuXvHqFHIo+qGqy8kxvJHxCNaE T6w4+3Z6KERKDbisscvd1A40/J5BcBFR+RX+Lknfe32lUuV2sfJxNQiAS5/LTO6g AzH8ZZoCdhkwRDyccqoze9ymbvd74/hSibEBdgYzltZ7XhhwL/nvEKpIMwsWfM4Q GWQs7GkUZdjfeZ31mx/LZ+1QdAezVo1BenVApqQAjKbWmq80HhlJlcVSZ3XDKGd6 GdCsXqBAVPnWM30xTQYeDs0blEZPb3wDMj/ZCL3kEg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrfedvgdehjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkjghffffhvfevufgtsegrtd erreerreejnecuhfhrohhmpedftfhosgcunfgrnhguvghrshdfuceorhhosgessghothht lhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnhepgfdvfeejfedvvdetieegkefhve ejieffuedutedttefhtdeifeegtefhvdevvefgnecuffhomhgrihhnpehphhhprdhnvght necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprhhosg essghothhtlhgvugdrtghouggvsh X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id DF42E15A0093; Wed, 10 Jul 2024 13:56:50 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-568-g843fbadbe-fm-20240701.003-g843fbadb Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <0cfec69f-2b0c-4c1e-9e47-e6379cf90ea2@app.fastmail.com> In-Reply-To: References: <14b769e9-acb7-46d2-85ab-f276e82f3069@app.fastmail.com> Date: Wed, 10 Jul 2024 19:56:30 +0200 To: "Gina P. Banyard" Cc: internals@lists.php.net Subject: Re: [PHP-DEV] [low priority] WeakMaps with scalar keys Content-Type: multipart/alternative; boundary=4efc86cb64ce485594eaedf5ca7afd6b From: rob@bottled.codes ("Rob Landers") --4efc86cb64ce485594eaedf5ca7afd6b Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed, Jul 10, 2024, at 19:48, Gina P. Banyard wrote: > On Wednesday, 10 July 2024 at 18:34, Rob Landers w= rote: >> Hello internals, >>=20 >> I know now that some of you are swamped getting ready for 8.4, so I w= ant to be clear that this will be an 8.5+ thing. I am simply sending it = to the list in case anyone wants to discuss it, burn it with fire, give = it a gold star, or ignore until later. >>=20 >> Currently, a WeakMap may only have object keys, and this logically ma= kes a lot of sense. However, there are cases where you want to reference= an object by a value but if the value is no longer referenced, no longe= r need it. A good example of this might be used in dependency injection = containers, lookup tables for value objects, etc. >>=20 >> I'd like to propose a ReverseWeakMap where the keys must be scalar, a= nd given an object as the value. Once the value is no longer referenced,= the key and value is removed from the ReverseWeakMap. >=20 > Isn't this use case already solved with WeakReferences? > https://www.php.net/manual/en/class.weakreference.php >=20 >=20 > Best regards, >=20 > Gina P. Banyard >=20 >>=20 Hey Gina, The answer is: it depends. If you don=E2=80=99t need the array to clean = up after itself, you can indeed use an array of WeakReference to get mos= t of the way there. If you want it to clean up after an object gets remo= ved, you either need to add support to the stored object=E2=80=99s destr= uctor (which isn=E2=80=99t always possible for built-in or final types),= or create your own garbage collector that scans the array.=20 Now that I think about it, it might be simpler to add an =E2=80=9ConRemo= ve()=E2=80=9D method that takes a callback for the WeakReference class.=20 =E2=80=94 Rob --4efc86cb64ce485594eaedf5ca7afd6b Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Wed, Jul 10,= 2024, at 19:48, Gina P. Banyard wrote:
On Wedne= sday, 10 July 2024 at 18:34, Rob Landers <rob@bottled.codes> wrote= :
= Hello internals,

I know now that some of yo= u are swamped getting ready for 8.4, so I want to be clear that this wil= l be an 8.5+ thing. I am simply sending it to the list in case anyone wa= nts to discuss it, burn it with fire, give it a gold star, or ignore unt= il later.

Currently, a WeakMap may only hav= e object keys, and this logically makes a lot of sense. However, there a= re cases where you want to reference an object by a value but if the val= ue is no longer referenced, no longer need it. A good example of this mi= ght be used in dependency injection containers, lookup tables for value = objects, etc.

I'd like to propose a Reverse= WeakMap where the keys must be scalar, and given an object as the value.= Once the value is no longer referenced, the key and value is removed fr= om the ReverseWeakMap.

I= sn't this use case already solved with WeakReferences?


Best regards,

Gina P. Banyard
=



Hey Gina,

The answer is: it depends. If you don=E2=80=99t need the array to clean= up after itself, you can indeed use an array of WeakReference to get mo= st of the way there. If you want it to clean up after an object gets rem= oved, you either need to add support to the stored object=E2=80=99s dest= ructor (which isn=E2=80=99t always possible for built-in or final types)= , or create your own garbage collector that scans the array. 

Now that I think about it, it might be simpler t= o add an =E2=80=9ConRemove()=E2=80=9D method that takes a callback for t= he WeakReference class. 

=E2=80=94 Rob
--4efc86cb64ce485594eaedf5ca7afd6b--