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

Make AbortSignal.timeout() 6x faster #15387

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

Jarred-Sumner
Copy link
Collaborator

What does this PR do?

This makes it so the only usage of us_create_timer is the sweep and date timers used by us_loop_t. The GC timers and AbortSignal.timeout use the timer heap. This will have memory usage implications so we need to measure memory usage changes in some scenarios before merging this.

This also switches the list of pending fetch() calls to shutdown to a HashMap instead of an ArrayHashMap, and swaps the multi-threaded queue of shutdowns

Stress test below gets 10x faster:

 mem bun ~/Desktop/signals.js
[196.05ms] [100000x] fetch(url, { signal })
[239.94ms] [100000x] await Promise.all(promises)

Peak memory usage: 509 MB

 mem bun-1.1.36 ~/Desktop/signals.js
[2.19s] [100000x] fetch(url, { signal })
[4.66s] [100000x] await Promise.all(promises)

Peak memory usage: 503 MB

Code:

const count = 100_000;
import { createServer } from "http";

const server = createServer((req, res) => {
  res.end("Hello, world!");
});

const promises = new Array(count);
const listenPromise = Promise.withResolvers();

server.listen(0, () => {
  listenPromise.resolve();
});
await listenPromise.promise;
const port = server.address().port;

console.time("[" + count + "x] " + "fetch(url, { signal })");
for (let i = 0; i < count; i++) {
  const signal = AbortSignal.timeout(1);
  promises[i] = fetch(`http://localhost:${port}`, {
    signal,
  }).catch(() => {});
}
console.timeEnd("[" + count + "x] " + "fetch(url, { signal })");

console.time("[" + count + "x] " + "await Promise.all(promises)");
await Promise.all(promises);
console.timeEnd("[" + count + "x] " + "await Promise.all(promises)");

server.close();

How did you verify your code works?

@robobun
Copy link

robobun commented Nov 24, 2024

@dylan-conway, your commit 35d16be has some failures in #6860

@Jarred-Sumner Jarred-Sumner changed the title Make AbortSignal.timeout() 10x faster Make AbortSignal.timeout() 6 faster Nov 24, 2024
@Jarred-Sumner Jarred-Sumner changed the title Make AbortSignal.timeout() 6 faster Make AbortSignal.timeout() 6x faster Nov 24, 2024
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.

3 participants