cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Events & Weak References

Open igorgolovanov opened this issue 13 years ago • 12 comments

Weak references provide a non-intrusive gateway to ephemeral objects. Unlike normal (strong) references, weak references do not prevent the garbage collector from freeing that object. For this reason, an object may be destroyed even though a weak reference to that object still exists. In such conditions, the weak reference seamlessly becomes invalid.

I suggest to use Weak References with Events (with Listeners).

Base idea:

class MyListener
{
    public function afterQuery()
    {
        echo 'Running afterQuery';
    }
}

$eventsManager = new Phalcon\Events\Manager();
$myListener = new MyListener();
$eventManager->attachUsingWeakRef('somecomponent', $myListener);

$eventsManager->fire('somecomponent:afterQuery', null); 
// this shows "Running afterQuery"

unset($myListener); 
// after this step, myListener doesn't have any references anymore and it must die.

$eventsManager->fire('somecomponent:afterQuery', null); 
// nothing happens, because myListener has died.

I have three suggests (must be selected only one):

1.Always uses WeakRef for all event-handlers

p.s. But i don't like this solution, because we need strong references in some cases.


$eventsManager = new Phalcon\Events\Manager();
$myListener = new MyListener();
$eventManager->attach('somecomponent', $myListener); 
// always use WeakRef for all event handlers.
// can't be uses with strong references
2.Add a flag/method for use or not weak references.
$eventsManager = new Phalcon\Events\Manager();
$myListener = new MyListener();

// flag
$eventManager->attach('somecomponent', $myListener, $priority, $isWeak);
// method
$eventManager->attachUsingWeakRef('somecomponent', $myListener, $priority);
3.Use weakref-php-extension and add possibility register Event-handler as "WeakRef" object.

So when event fired then will checked that event-handler is WeakRef object or not. If is WeakRef object then will check whether the object referenced still exists and if exists - fire! If is not WeakRef object then standart fire.

$eventsManager = new Phalcon\Events\Manager();
$myListener = new MyListener();

$eventManager->attach('somecomponent', new WeakRef($myListener));

Avaliable php-extension for Weak References: http://www.php.net/manual/en/book.weakref.php


Zend Framework 2 uses WeakRef extension.

igorgolovanov avatar May 21 '13 14:05 igorgolovanov

  1. You unlinked $myListener variable of MyListener object, but $eventsManager still keeps this reference

iforp avatar May 21 '13 16:05 iforp

Garbage collector copes poorly with only cyclical references:

class A
{
    public $b;
}

class B {
    public $a;
}

$a = new A;
$b = new B;
$a->b = $b;
$b->a = $a;

unset($a, $b); // memory still leaks

But you may always call gc_collect_cycles()

iforp avatar May 21 '13 17:05 iforp

I know how GC working. gc_collect_cycles() - it's other.

1) You unlinked $myListener variable of MyListener object, but $eventsManager still keeps this reference

In first way i suggest always use WeakRef in Event\Manager without strong references. So in this case after unset($myListener) Event\Manager must always lose references to $myListener.

igorgolovanov avatar May 21 '13 17:05 igorgolovanov

What will happen with $myListener if it was initialized inside some method after it is executed?

call_user_func(function() use($di)
{
    $myListener = new MyListener();
    $di->get('eventsManager')->attachUsingWeakRef('somecomponent', $myListener);
});

Should the GC deallocate $myListener?

iforp avatar May 22 '13 12:05 iforp

@Agent-J In this case - yes. Because after your Closure will be executed the $myListener will not have any strong references anymore. And all event-handlers provided by this listener will be "unatached" from EventsManager.

igorgolovanov avatar May 22 '13 13:05 igorgolovanov

So you will be able to attach the listener only in the global scope or ongoing call stack otherwise the reference will be lost.

iforp avatar May 22 '13 14:05 iforp

And what about this case?

$eventsManager->attachUsingWeakRef('somecomponent', new MyListener());

iforp avatar May 22 '13 14:05 iforp

I sent a simple pull. You can try how it works :)

igorgolovanov avatar May 22 '13 14:05 igorgolovanov

@golovanov

Zend Framework 2 uses WeakRef extension.

No! nowadays, no! https://github.com/zendframework/zf2/pull/4373 https://github.com/weierophinney/zf2/commit/b7e2abfed61568f7b4a5b871bfcad8c2e0f5e60e

sasezaki avatar May 23 '13 14:05 sasezaki

@sasezaki You're right! To this point has already been removed this functionality from zend, because there it used by default. So when weakref was installed or wasn't installed, this was causing the different behavior of logic of zf2. I not suggest using by default, i suggest add a possibility to use weakref. And who don't know how it works will not use this, but who know how it works can use this (and will able to achieve an increase in productivity and a significant reduction in memory usage)

igorgolovanov avatar May 23 '13 18:05 igorgolovanov

Implemented in 1.3.0

ghost avatar Oct 02 '13 05:10 ghost

Coool! Thank you!

igorgolovanov avatar Oct 02 '13 09:10 igorgolovanov