Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow GC of arguments during microtask execution #60

Merged
merged 1 commit into from
Aug 1, 2022
Merged

Allow GC of arguments during microtask execution #60

merged 1 commit into from
Aug 1, 2022

Conversation

Nevay
Copy link
Contributor

@Nevay Nevay commented Jul 30, 2022

No description provided.

@@ -418,7 +418,8 @@ private function invokeMicrotasks(): void
[$callback, $args] = $this->microtaskQueue->dequeue();

try {
$callback(...$args);
// Clear $args to allow garbage collection
$callback(...$args, ...($args = []));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how does this work? You first pass all the elements of the $args array into the argument list for $callback, and then you overwrite $args with an empty array and also "push" that to the same argument list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it overwrites the $args array (and pushes zero arguments to the argument list) after pushing the original arguments and before invoking the callback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Near trick, it's a tad confusing when you first read the line tbh.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better documentation what happens here would be good especially for people like me that doesn't see what's happening at the first glance and why it's needed.

@bwoebi
Copy link
Contributor

bwoebi commented Jul 31, 2022

The alternative implementation would be \call_user_func_array(...$this->microtaskQueue->dequeue()); directly. Which looks cleaner to me than this clever hack :-D

... Though, it might be that this doesn't work here, as cufa() stores its own CoW copy of its args until its end...

@trowski trowski merged commit 4e74555 into revoltphp:main Aug 1, 2022
@trowski
Copy link
Member

trowski commented Aug 1, 2022

@Nevay Thanks for finding this and for the creative solution. Seems we rarely pass arguments, let alone instantiating objects in the argument list, so this hadn't come up. We appreciate the real-world testing to find these cases!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants