jeremeamia/super_closure

Improving speed

TRPB opened this issue · 1 comments

TRPB commented

I was looking at the code and I think a better way to improve the speed by avoiding most reflection

Once you have the code for the closure as a string e.g.

function($foo) use ($bar, $baz) {
    return $foo + $bar;
};

You should rewrite it with preg_replace to:

function($foo) use ($state) {
    return $foo + $state->bar + $state->baz;
};

Then you can recreate a closure object using:

$state = $this->state;
$func = null;
eval('$func = function($foo) use ($state) {
    return $foo + $state->bar + $state->baz;
};);

then you can just call $func() as if it were the original closure rather than using a reflection object. This way, you only need to use reflection when serializing, and not when unserialzing. This would vastly improve speed whenever you're not serializing and unserializing the object in the same request.

TRPB commented

Never mind after looking at it again, I can see it does something similar