Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124379 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 E834B1A00B7 for ; Thu, 11 Jul 2024 06:45:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1720680437; bh=3MRB3VEXMcKgOoszfx3EQBOtjPpPhzvbTyJWrxntMcg=; h=In-Reply-To:References:Date:From:To:Cc:Subject:From; b=baO0Q7rsBtkIdH8IUKP0qr5qUpTk61BvBT6/+0PKwfXcA2ypizhJyEnl/3yqQl2AE w4pyRI3dgoElBqsm8JUVCKK4sdrCVvOkRDMGunEUlIxbYiZMw9elm2ID91lVV+sbsr ZBg1YDxEq2Kdraqn87aL3CBZja530lcCGgYrIqCbedZRhMxuTQz8KpPsZ40kTjWDFK Icqr/43wM9xJlpuKN/JyMDJSwdyU/aGkvHwh5C46b3kdjDx0lvqgfuEGtLLJRKYwJK Y5Pdwi4JrdTwbS4lCy2kAj7SMQmmJgcG/yXE430U/2oskMLbM9uI8IHElZDU2U2DJm xNGMQkeXb8wIw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AF3E0180076 for ; Thu, 11 Jul 2024 06:47:15 +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 fhigh6-smtp.messagingengine.com (fhigh6-smtp.messagingengine.com [103.168.172.157]) (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 ; Thu, 11 Jul 2024 06:47:14 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.nyi.internal (Postfix) with ESMTP id BD7D61141F5F; Thu, 11 Jul 2024 02:45:47 -0400 (EDT) Received: from imap49 ([10.202.2.99]) by compute1.internal (MEProxy); Thu, 11 Jul 2024 02:45:47 -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=1720680347; x= 1720766747; bh=7HHxAOBeoAiIAa9d9EezPQlA3YH9CRe6zVD6ve9D2rs=; b=T SG0ChGYrbBsw8/dcRFR7pcp5eeKwZRSdJYbanzwsYnBhKqPRtHNSUlSWv4Xfni5e 53CleqXNyPKb++xMVDu+ntrg1hUasRyZG+5wPVvWcaDkCfCfvEmzeCkZgRqZvRNy gW0eklAnzLv4i3b5W7vApvMOxkYnv9bx/zoJnoCRaZp2EhunaTAiqfFuIrJDemka kjPLqRr/khHtIh113Aynv6FN+MKPPv/haQVR+5wHh9XImr4jUMMvyGa7mi1T29uX 7UZSOpnNI3KjERCB3ENQUQg9a7IxjU2UIaj/UKI2PoqEvRt09OFVfAjCk40fopkL 7YMTpzsTFO7M3TrcxZphg== 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=1720680347; x=1720766747; bh=7HHxAOBeoAiIAa9d9EezPQlA3YH9 CRe6zVD6ve9D2rs=; b=nznDIry607PlXgLKK7EuFDEeTZF37WPRS0/k2JGIEj42 tc5v01yjRgn0efAg6gIAwkgVMXhoIYxTLbZccO6eTJ7AdxeBbFSDdxIGK9L/UAra +PLSCUvP/qYnTtmO+4IvNUM8GNQbd61/rdxokXXgkCgrFUX34r8BH0vQjnwmypEe 5iaezcjDaxGGyM2a1bKRE7VNhiT3lGOILsmQ9++6+qQBWOf8BZi2+T+975MY+VYz Lhure7gbkVO4/uWuJ/E0c55pOMHtgmFkijbUy9LRUUGM3iB+Y1xiCaDGmInPP5bV AMOb41QoMtVtH2JiBUIEgBdTKI0YeQMrrBXw7mvFuQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrfeefgdduuddvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvfevufgtsegrtderreerreejnecuhfhrohhmpedftfho sgcunfgrnhguvghrshdfuceorhhosgessghothhtlhgvugdrtghouggvsheqnecuggftrf grthhtvghrnhepvdehkeetleevteefveegkefgffdvuefhleevhedvteeigfegtdefjeeh tefghfeunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh eprhhosgessghothhtlhgvugdrtghouggvsh X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 5922115A0092; Thu, 11 Jul 2024 02:45:46 -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: <5b87c29d-dfb9-4b47-957c-f89bf0209640@app.fastmail.com> In-Reply-To: References: <14b769e9-acb7-46d2-85ab-f276e82f3069@app.fastmail.com> <0cfec69f-2b0c-4c1e-9e47-e6379cf90ea2@app.fastmail.com> Date: Thu, 11 Jul 2024 08:45:26 +0200 To: "Benjamin Morel" Cc: "Gina P. Banyard" , internals@lists.php.net Subject: Re: [PHP-DEV] [low priority] WeakMaps with scalar keys Content-Type: multipart/alternative; boundary=1ff71a7775fa44388c33805f8ccf164e From: rob@bottled.codes ("Rob Landers") --1ff71a7775fa44388c33805f8ccf164e Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Thu, Jul 11, 2024, at 01:11, Benjamin Morel wrote: >> The answer is: it depends. If you don=E2=80=99t need the array to cle= an up after itself, you can indeed use an array of WeakReference to get = most of the way there. If you want it to clean up after an object gets r= emoved, you either need to add support to the stored object=E2=80=99s de= structor (which isn=E2=80=99t always possible for built-in or final type= s), or create your own garbage collector that scans the array.=20 >=20 > It is indeed doable in userland using WeakReferences, with a small per= formance penalty: >=20 > ``` > class ReverseWeakMap implements Countable, IteratorAggregate, ArrayAcc= ess > { > /** > * @var array > */ > private array $map =3D []; >=20 > public function count(): int > { > foreach ($this->map as $value =3D> $weakReference) { > if ($weakReference->get() =3D=3D=3D null) { > unset($this->map[$value]); > } > } >=20 > return count($this->map); > } >=20 > public function getIterator(): Generator > { > foreach ($this->map as $value =3D> $weakReference) { > $object =3D $weakReference->get(); >=20 > if ($object =3D=3D=3D null) { > unset($this->map[$value]); > } else { > yield $value =3D> $object; > } > } > } >=20 > public function offsetExists(mixed $offset) > { > if (isset($this->map[$offset])) { > $object =3D $this->map[$offset]->get(); >=20 > if ($object !=3D=3D null) { > return true; > } >=20 > unset($this->map[$offset]); > } >=20 > return false; > } >=20 > public function offsetGet(mixed $offset): object > { > if (isset($this->map[$offset])) { > $object =3D $this->map[$offset]->get(); >=20 > if ($object !=3D=3D null) { > return $object; > } >=20 > unset($this->map[$offset]); > } >=20 > throw new Exception('Undefined offset'); > } >=20 > public function offsetSet(mixed $offset, mixed $value): void > { > $this->map[$offset] =3D WeakReference::create($value); > } >=20 > public function offsetUnset(mixed $offset): void > { > unset($this->map[$offset]); > } > } > ```=20 >=20 >> Now that I think about it, it might be simpler to add an =E2=80=9ConR= emove()=E2=80=9D method that takes a callback for the WeakReference clas= s.=20 >>=20 >> =E2=80=94 Rob >=20 >=20 > A callback when an object goes out of scope would be a great addition = to both WeakReference & WeakMap indeed, it would allow custom userland w= eak maps like the above, with next to no performance penalty! >=20 > - Benjamin > =20 The callback is surprisingly easy to implement, at least for WeakReferen= ce (did it in about 10 minutes on the train as a hack). I haven=E2=80=99= t looked into WeakMap yet, but I suspect much of the plumbing is the sam= e.=20 I also looked into the ReverseWeakMap a bit and it seems there are just = too many foorguns to make it worthwhile. For example: $reverseWeakMap[$key] =3D new Obj(); is actually a noop as in it does absolutely nothing. It gets worse, but = I won=E2=80=99t bore you with the details since I won=E2=80=99t be doing= it.=20 Anyway, while I feel like the implementation for a callback will be extr= emely straightforward and the RFC rather simple, I need to go back and r= ead the original discussion threads for this feature first to see if a c= allback was addressed. So, still not until after 8.4. =E2=80=94 Rob --1ff71a7775fa44388c33805f8ccf164e Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable

=
On Thu, Jul 11, 2024, at 01:11, Benjamin Morel wrote:
=
The answer is: it depends. If you don=E2=80=99t need the array to clea= n up after itself, you can indeed use an array of WeakReference to get m= ost of the way there. If you want it to clean up after an object gets re= moved, you either need to add support to the stored object=E2=80=99s des= tructor (which isn=E2=80=99t always possible for built-in or final types= ), or create your own garbage collector that scans the array. 
<= /div>

It is indeed doable in userl= and using WeakReferences, with a small performance penalty:

```
class ReverseWeakMap implements = Countable, IteratorAggregate, ArrayAccess
{
=     /**
     * @var array<int|= string, WeakReference>
     */
=
    private array $map =3D [];

    public function count(): int
   = ; {
        foreach ($this->map as = $value =3D> $weakReference) {
      &nbs= p;     if ($weakReference->get() =3D=3D=3D null) {
                unset($thi= s->map[$value]);
          &nb= sp; }
        }

        return count($this->map);
    }

    public = function getIterator(): Generator
    {
        foreach ($this->map as $value =3D&g= t; $weakReference) {
          &n= bsp; $object =3D $weakReference->get();

=             if ($object =3D=3D=3D null) {<= br>
                un= set($this->map[$value]);
        &n= bsp;   } else {
          &n= bsp;     yield $value =3D> $object;
  &n= bsp;         }
      &n= bsp; }
    }

 =   public function offsetExists(mixed $offset)
 =   {
        if (isset($this->= map[$offset])) {
           = $object =3D $this->map[$offset]->get();

<= div>            if ($object !=3D=3D null) = {
                = return true;
            }

            un= set($this->map[$offset]);
        }=

        return false;<= br>
    }

   = public function offsetGet(mixed $offset): object
  &= nbsp; {
        if (isset($this->ma= p[$offset])) {
            $= object =3D $this->map[$offset]->get();

            if ($object !=3D=3D null) {<= br>
                re= turn $object;
            }<= br>

            u= nset($this->map[$offset]);
        = }

        throw new Exc= eption('Undefined offset');
    }
=
    public function offsetSet(mixed $offset, mi= xed $value): void
    {
  &nb= sp;     $this->map[$offset] =3D WeakReference::create($valu= e);
    }

  &n= bsp; public function offsetUnset(mixed $offset): void
&nbs= p;   {
        unset($this->ma= p[$offset]);
    }
}
``` 

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

=E2=80=94 Rob
<= /div>


A callback when an = object goes out of scope would be a great addition to both WeakReference= & WeakMap indeed, it would allow custom userland weak maps like the= above, with next to no performance penalty!

- Benjamin
 

The callback is surprisingly easy to implement, at least= for WeakReference (did it in about 10 minutes on the train as a hack). = I haven=E2=80=99t looked into WeakMap yet, but I suspect much of the plu= mbing is the same. 

I also looked into the= ReverseWeakMap a bit and it seems there are just too many foorguns to m= ake it worthwhile. For example:

$reverseWea= kMap[$key] =3D new Obj();

is actually a noo= p as in it does absolutely nothing. It gets worse, but I won=E2=80=99t b= ore you with the details since I won=E2=80=99t be doing it. 

Anyway, while I feel like the implementation for = a callback will be extremely straightforward and the RFC rather simple, = I need to go back and read the original discussion threads for this feat= ure first to see if a callback was addressed. So, still not until after = 8.4.

=E2=80=94 Rob
--1ff71a7775fa44388c33805f8ccf164e--