Skip to content

Make AbortSignal.timeout() 6x faster - #15387

Closed
Jarred-Sumner wants to merge 17 commits into
mainfrom
jarred/change-timers
Closed

Jarred-Sumner wants to merge 17 commits into
mainfrom
jarred/change-timers

Conversation

@Jarred-Sumner

Copy link
Copy Markdown
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

robobun commented Nov 24, 2024

Copy link
Copy Markdown
Collaborator

@Jarred-Sumner, your commit 2d78d45 has 19 failures in #7442:

  • test/js/node/util/test-aborted.test.ts - 1 failing on 🍎 14 x64
  • test/js/web/fetch/body-stream.test.ts - timeout on 🍎 14 x64
  • test/js/web/fetch/body-stream.test.ts - timeout on 🍎 14 x64
  • test/js/web/fetch/body-stream.test.ts - timeout on 🍎 14 x64
  • test/js/web/fetch/body-stream.test.ts - timeout on 🍎 14 x64
  • test/js/bun/patch/patch.test.ts - segmentation fault on 🍎 14 x64
  • test/js/bun/patch/patch.test.ts - segmentation fault on 🍎 14 x64
  • test/js/bun/patch/patch.test.ts - segmentation fault on 🍎 14 x64
  • test/v8/v8.test.ts - 22 failing on 🐧 3.20 aarch64
  • test/v8/v8.test.ts - 22 failing on 🐧 3.20 x64-baseline
  • test/v8/v8.test.ts - 22 failing on 🐧 3.20 x64
  • test/cli/hot/hot.test.ts - timeout on 🐧 24.04 x64-baseline
  • test/cli/hot/hot.test.ts - timeout on 🐧 3.20 x64-baseline
  • test/js/bun/util/filesink.test.ts - 1 failing on 🐧 24.04 x64
  • test/js/web/fetch/fetch.stream.test.ts - 1 failing on 🐧 22.04 aarch64
  • test/js/web/fetch/fetch.stream.test.ts - 1 failing on 🐧 22.04 x64-baseline
  • test/js/web/fetch/fetch.stream.test.ts - 1 failing on 🐧 3.20 x64-baseline
  • test/js/web/abort/abort.test.ts - 1 failing on 🐧 24.04 x64-baseline
  • test/js/web/abort/abort.test.ts - 1 failing on 🐧 22.04 x64
  • test/js/web/abort/abort.test.ts - 1 failing on 🍎 14 x64
  • test/js/web/abort/abort.test.ts - 1 failing on 🍎 14 x64
  • test/js/web/timers/setTimeout.test.js - 1 failing on 🐧 22.04 x64
  • test/js/node/http/node-http.test.ts - 1 failing on 🐧 24.04 x64-baseline
  • test/js/node/http/node-http.test.ts - 1 failing on 🐧 24.04 x64
  • test/js/web/fetch/abortsignal-standalone.test.ts - timeout on 🪟 2019 x64-baseline
  • test/js/web/fetch/abortsignal-standalone.test.ts - timeout on 🪟 2022 x64-baseline
  • test/js/web/fetch/abortsignal-standalone.test.ts - timeout on 🪟 2025 x64-baseline
  • test/js/web/fetch/abortsignal-standalone.test.ts - timeout on 🪟 2025 x64
  • test/js/web/fetch/abortsignal-standalone.test.ts - timeout on 🪟 2022 x64
  • test/js/web/fetch/abortsignal-standalone.test.ts - timeout on 🪟 2019 x64
  • test/js/bun/spawn/spawn.test.ts - timeout on 🪟 2025 x64-baseline
  • test/js/bun/spawn/spawn.test.ts - timeout on 🪟 2019 x64
  • test/js/node/tls/node-tls-server.test.ts - 1 failing on 🪟 2025 x64
  • test/js/bun/spawn/spawn-stdin-destroy.test.ts - 1 failing on 🪟 2019 x64-baseline
  • test/cli/install/registry/bun-install-registry.test.ts - 1 failing on 🪟 2025 x64-baseline
  • test/cli/install/registry/bun-install-registry.test.ts - 1 failing on 🪟 2022 x64
  • test/cli/install/registry/bun-install-registry.test.ts - timeout on 🐧 24.04 x64
  • test/cli/install/registry/bun-install-registry.test.ts - timeout on 🐧 3.20 x64-baseline
  • test/js/deno/performance/performance.test.ts - 1 failing on 🪟 2022 x64
  • test/js/deno/performance/performance.test.ts - 1 failing on 🐧 22.04 x64-baseline
  • test/cli/run/require-cache.test.ts - 1 failing on 🪟 2022 x64-baseline
  • test/cli/run/require-cache.test.ts - 1 failing on 🪟 2022 x64
  • package.json - timeout on 🪟 2019 x64-baseline
  • test/integration/next-pages/test/next-build.test.ts - 1 failing on 🍎 14 aarch64
  • test/integration/next-pages/test/next-build.test.ts - 1 failing on 🐧 22.04 x64-baseline
  • test/integration/next-pages/test/next-build.test.ts - 1 failing on 🐧 3.20 aarch64
  • @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

    @cirospaciari cirospaciari left a comment

    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

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

    Tests failures

    @kravetsone

    Copy link
    Copy Markdown

    Please make abortSignal timeout works (#13302)

    @cirospaciari

    Copy link
    Copy Markdown
    Member

    Please make abortSignal timeout works (#13302)

    Yes sir, you can follow the fix in this PR #15623

    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.

    5 participants