Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] RFC WeakRefs

Posted by Joe Watkins 
Joe Watkins
[PHP-DEV] RFC WeakRefs
May 17, 2018 08:20AM
Morning internals,

I'd like to raise for discussion https://wiki.php.net/rfc/weakrefs

Am I missing anything ?

Cheers
Joe
Rowan Collins
Re: [PHP-DEV] RFC WeakRefs
May 17, 2018 09:40AM
On 17 May 2018 07:09:28 BST, Joe Watkins <[email protected]> wrote:
>Morning internals,
>
>I'd like to raise for discussion https://wiki.php.net/rfc/weakrefs

Hi Joe,

Sounds exciting!

What does this look like from userland? I'm guessing you wrap the object you want a weak reference to in a special container? If so, has there been any previous discussion of baking it further into the language, e.g. as a keyword to wrap, and an implicit unwrap? Obviously something that could be revisited later, just curious if it's been considered.

Cheers,

--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Joe Watkins
Re: [PHP-DEV] RFC WeakRefs
May 17, 2018 10:30AM
Morning Rowan,

I've updated the RFC with a description of the API and comparison to the
current WeakRef API documented by the pecl extension.

Example code can be found in tests also ...

Cheers
Joe

On Thu, May 17, 2018 at 9:36 AM, Rowan Collins <[email protected]>
wrote:

> On 17 May 2018 07:09:28 BST, Joe Watkins <[email protected]> wrote:
> >Morning internals,
> >
> >I'd like to raise for discussion https://wiki.php.net/rfc/weakrefs
>
> Hi Joe,
>
> Sounds exciting!
>
> What does this look like from userland? I'm guessing you wrap the object
> you want a weak reference to in a special container? If so, has there been
> any previous discussion of baking it further into the language, e.g. as a
> keyword to wrap, and an implicit unwrap? Obviously something that could be
> revisited later, just curious if it's been considered.
>
> Cheers,
>
> --
> Rowan Collins
> [IMSoP]
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Etienne Kneuss
Re: [PHP-DEV] RFC WeakRefs
May 17, 2018 11:40AM
On Thu, May 17, 2018 at 8:10 AM Joe Watkins <[email protected]> wrote:

> Morning internals,
>
> I'd like to raise for discussion https://wiki.php.net/rfc/weakrefs
>
> Am I missing anything ?
>

I'm all for implementing all this natively, the way it was implemented in
pecl-weakref always felt hackish and brittle.

That said, if the plan is to subsume pecl-weakref, I suggest we also
reimplement weakmaps, which offer a convenient way of storing meta/cache
data and is not implementable in userland without trade-offs.

Best,


>
> Cheers
> Joe
Joe Watkins
Re: [PHP-DEV] RFC WeakRefs
May 17, 2018 12:30PM
Morning,

I'm aware the pecl extension also has weakmap support.

I think we've enough time to vote on the implementation of weakrefs, and
then think about how we want weak maps to work and vote on the
implementation of that.

Cheers
Joe

On Thu, May 17, 2018 at 11:35 AM, Etienne Kneuss <[email protected]> wrote:

> On Thu, May 17, 2018 at 8:10 AM Joe Watkins <krakj[email protected]> wrote:
>
>> Morning internals,
>>
>> I'd like to raise for discussion https://wiki.php.net/rfc/weakrefs
>>
>> Am I missing anything ?
>>
>
> I'm all for implementing all this natively, the way it was implemented in
> pecl-weakref always felt hackish and brittle.
>
> That said, if the plan is to subsume pecl-weakref, I suggest we also
> reimplement weakmaps, which offer a convenient way of storing meta/cache
> data and is not implementable in userland without trade-offs.
>
> Best,
>
>
>>
>> Cheers
>> Joe
>
>
Rowan Collins
Re: [PHP-DEV] RFC WeakRefs
May 18, 2018 02:10PM
On 17 May 2018 10:35:11 BST, Etienne Kneuss <[email protected]> wrote:
>That said, if the plan is to subsume pecl-weakref, I suggest we also
>reimplement weakmaps, which offer a convenient way of storing
>meta/cache
>data and is not implementable in userland without trade-offs.

It looks to me like WeakMap would be fairly trivial to implement in userland as a combination of WeakRef, ArrayAccess, Iterable, and an internal SplObjectStorage for the ability to use objects as keys (although I'm not clear why it uses object keys in the first place).

The only part that seems hard to write efficiently is Countable support, since you'd have to iterate the internal hash checking for unset objects. I suppose iteration would also slow down a bit of lots of the references were now unset. Is that the trade-off you're referring to, that the C implementation has some magic to avoid?

Regards,

--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Etienne Kneuss
Re: [PHP-DEV] RFC WeakRefs
May 18, 2018 02:20PM
On Fri, May 18, 2018 at 1:59 PM Rowan Collins <[email protected]>
wrote:

> On 17 May 2018 10:35:11 BST, Etienne Kneuss <[email protected]> wrote:
> >That said, if the plan is to subsume pecl-weakref, I suggest we also
> >reimplement weakmaps, which offer a convenient way of storing
> >meta/cache
> >data and is not implementable in userland without trade-offs.
>
> It looks to me like WeakMap would be fairly trivial to implement in
> userland as a combination of WeakRef, ArrayAccess, Iterable, and an
> internal SplObjectStorage for the ability to use objects as keys (although
> I'm not clear why it uses object keys in the first place).
>

In weakmaps it is the keys that are weak, hence objects only; the values
are arbitrary data associated with them.
The goal is to provide a way to associate metadata that may also be
collected in case the key gets collected.
In some sense it would behave as if this metadata was stored as a property
of the object, only it is stored externally in a weakmap.

>
>
>> The only part that seems hard to write efficiently is Countable support,
>> since you'd have to iterate the internal hash checking for unset objects. I
>> suppose iteration would also slow down a bit of lots of the references were
>> now unset. Is that the trade-off you're referring to, that the C
>> implementation has some magic to avoid?
>>
>
You indeed would need a O(n) count. But it's not the only issue: you also
should DELREF the values associated with invalid weakrefs once in a while,
and doing this manually makes this entire thing less attractive.
Rowan Collins
Re: [PHP-DEV] RFC WeakRefs
May 18, 2018 02:40PM
On 18 May 2018 13:15:21 BST, Etienne Kneuss <[email protected]> wrote:
>On Fri, May 18, 2018 at 1:59 PM Rowan Collins <[email protected]>
>wrote:
>
>> On 17 May 2018 10:35:11 BST, Etienne Kneuss <[email protected]> wrote:
>> >That said, if the plan is to subsume pecl-weakref, I suggest we also
>> >reimplement weakmaps, which offer a convenient way of storing
>> >meta/cache
>> >data and is not implementable in userland without trade-offs.
>>
>> It looks to me like WeakMap would be fairly trivial to implement in
>> userland as a combination of WeakRef, ArrayAccess, Iterable, and an
>> internal SplObjectStorage for the ability to use objects as keys
>(although
>> I'm not clear why it uses object keys in the first place).
>>
>
>In weakmaps it is the keys that are weak, hence objects only; the
>values
>are arbitrary data associated with them.
>The goal is to provide a way to associate metadata that may also be
>collected in case the key gets collected.
>In some sense it would behave as if this metadata was stored as a
>property
>of the object, only it is stored externally in a weakmap.

Ah, that makes more sense; there didn't seem to be any accrual description in the manual of what they were for; maybe I was looking in the wrong place. In that case, I understand your request to include it, and apologise for the noise :)

Regards,

--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Bogdan Padalko
[PHP-DEV] Re: RFC WeakRefs
May 29, 2018 12:30PM
On 5/17/18 9:09 AM, Joe Watkins wrote:
> Morning internals,
>
> I'd like to raise for discussion https://wiki.php.net/rfc/weakrefs
>
> Am I missing anything ?
>
> Cheers
> Joe
>

Hi,

Would we benefit having notification mechanism in weak references? I
implemented such notification in https://github.com/pinepain/php-ref,
(used https://github.com/colder/php-weakref as a reference). The only
issue is that how notification logic should behave when unclean shutdown
happens, the culprit is some exception thrown which leads to referent
object destruction and this uncertainty doubles when notification logic
(callback) also triggers exception. So I bring this question more for
discussion and it could be definitely implemented later (small working
feature is better then large non-working one).

Seen that there is weak data structs discussion below, there is
https://github.com/pinepain/php-object-maps weak/soft map implementatoin
I made at the time of php-ref extension. It somehow based on java maps
interface and I think with notification interface for weak refs it's
fairly simple to implement such maps in userland THOUGH without
notification, having primitive weakmap in the psl needs to be taken with
care to do not end up like other psl data structs.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login