Skip to content

do not release weak refs twice #19149

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

DonIsaac
Copy link
Contributor

@DonIsaac DonIsaac commented Apr 20, 2025

What does this PR do?

Makes several improvements and fixes to microtask-related code

  • perf: Do not call releaseWeakRefs before draining microtask queue. It's already done afterwards.
  • fix: JSGlobalObject.reload() now drains nextTick queue as well as microtask queue
  • fix: do not drain nextTick queue when there's a pending termination exception

JSC__JSGlobalObject__drainMicrotasks will already call finalizeSynchronousJSExecution, meaning we increment m_currentWeakRefVersion twice (once before draining, once after draining).

While finalizeSynchronousJSExecution is cheap, It is defined in C++ code, making it opaque to the Zig compiler and preventing inlining optimizations. Draining microtasks is a critical perf path, and avoiding this extra function call may lead to perf gains.

How did you verify your code works?

let's see what happens in CI

@robobun
Copy link

robobun commented Apr 20, 2025

Updated 3:58 PM PT - Apr 22nd, 2025

@DonIsaac, your commit 27b8a3afd473357a9f7191f93fc7a08128fd44ba passed in Build #15335! 🎉


🧪   try this PR locally:

bunx bun-pr 19149

@DonIsaac
Copy link
Contributor Author

DonIsaac commented Apr 21, 2025

I still need to benchmark this but it's ready for review

edit: no noticeable difference in async and websocket benchmarks.

@DonIsaac DonIsaac marked this pull request as ready for review April 21, 2025 04:24
@@ -3948,6 +3948,7 @@ void GlobalObject::drainMicrotasks()
if (auto nextTickQueue = this->m_nextTickQueue.get()) {
Bun::JSNextTickQueue* queue = jsCast<Bun::JSNextTickQueue*>(nextTickQueue);
queue->drain(vm, this);
vm.finalizeSynchronousJSExecution();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note: also called by vm.drainMicrotasks()

@DonIsaac DonIsaac changed the title experiment: do not release weak refs twice do not release weak refs twice Apr 21, 2025
@DonIsaac DonIsaac marked this pull request as draft April 22, 2025 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants