Skip to content

Commit e3d21f4

Browse files
committed
make it faster
1 parent 72fe63f commit e3d21f4

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/redlet-node.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,34 @@ function getPort() {
1616
const u = esmurl(import.meta, async () => {
1717
const { workerData } = await import("node:worker_threads");
1818
const port = workerData.port;
19+
/** @type {((...a: any[]) => any)[]} */
20+
const rememberedFunctions = [];
1921
port.onmessage = async (e) => {
20-
/** @type {[SharedArrayBuffer, string, any[]]} */
21-
const [lockBuffer, moduleURL, args] = e.data;
22+
/** @type {SharedArrayBuffer} */
23+
const lockBuffer = e.data[0];
24+
/** @type {string | number} */
25+
const executorURLOrId = e.data[1];
26+
/** @type {any[]} */
27+
const args = e.data[2];
2228
const lock = new Int32Array(lockBuffer);
29+
2330
/** @type {[any] | [void, any]} */
2431
let r;
2532
try {
26-
const module = await import(moduleURL);
27-
r = [await module.default.apply(undefined, args)];
33+
/** @type {((...a: any[]) => any)} */
34+
let f;
35+
if (typeof executorURLOrId === "number") {
36+
f = rememberedFunctions[executorURLOrId];
37+
} else {
38+
const module = await import(executorURLOrId);
39+
f = await module.default;
40+
rememberedFunctions.push(f);
41+
}
42+
r = [await f(...args)];
2843
} catch (e) {
2944
r = [, e];
3045
}
46+
3147
port.postMessage(r);
3248
Atomics.store(lock, 0, 1);
3349
Atomics.notify(lock, 0);
@@ -48,34 +64,48 @@ function getPort() {
4864
/** @type {{ worker: NodeWorker, port: MessagePort } | null | undefined} */
4965
getPort.c;
5066

67+
/** @type {string[]} */
68+
const rememberedURLs = [];
69+
5170
/**
5271
* @template {any[]} A
5372
* @template R
5473
* @param {((...args: A) => R) | string | URL} functionOrURL
5574
* @returns {(...args: A) => Awaited<R>}
5675
*/
5776
function redlet(functionOrURL) {
77+
/** @type {string} */
5878
let executorURL;
79+
/** @type {((...args: A) => R) | null | undefined} */
5980
let maybeFunction;
6081
if (typeof functionOrURL === "function") {
6182
maybeFunction = functionOrURL;
6283
const code = `export default ${functionOrURL}`;
6384
executorURL =
6485
"data:text/javascript;base64," + Buffer.from(code).toString("base64");
6586
} else if (URLCanParse(functionOrURL)) {
66-
executorURL = functionOrURL;
87+
executorURL = `${functionOrURL}`;
6788
} else {
6889
const code = `export default ${functionOrURL}`;
6990
executorURL =
7091
"data:text/javascript;base64," + Buffer.from(code).toString("base64");
7192
}
7293

94+
/**
95+
* @param {A} args
96+
* @returns {Awaited<R>}
97+
*/
7398
function run(...args) {
7499
const port = getPort();
75100

76101
const lockBuffer = new SharedArrayBuffer(4);
77102
const lock = new Int32Array(lockBuffer);
78-
port.postMessage([lockBuffer, executorURL, args]);
103+
if (rememberedURLs.includes(executorURL)) {
104+
port.postMessage([lockBuffer, rememberedURLs.indexOf(executorURL), args]);
105+
} else {
106+
port.postMessage([lockBuffer, executorURL, args]);
107+
rememberedURLs.push(executorURL);
108+
}
79109
Atomics.wait(lock, 0, 0);
80110

81111
/** @type {[any] | [void, any]} */

test/async-to-sync-comparison.bench.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import { createWorker } from "await-sync";
44
import makeSynchronous from "make-synchronous";
55
import { createSyncFn } from "synckit";
66
import { readFile, writeFile } from "node:fs/promises";
7-
import { unlinkSync } from "node:fs";
7+
import { appendFileSync, unlinkSync } from "node:fs";
88

99
await writeFile("test.log", "test\n".repeat(1000));
10-
process.on("exit", () => {
11-
unlinkSync("test.log");
12-
});
1310
const testFile = process.cwd() + "/test.log";
1411

1512
const bench = new Bench({ time: 3000 });
@@ -23,13 +20,6 @@ await writeFile("synckit-runAsWorker.mjs", `
2320
})
2421
`
2522
);
26-
process.on("exit", () => {
27-
try {
28-
unlinkSync("synckit-runAsWorker.mjs");
29-
} catch (e) {
30-
console.log(e);
31-
}
32-
});
3323
const synckit = createSyncFn(process.cwd() + "/synckit-runAsWorker.mjs");
3424
bench.add("synckit", () => {
3525
synckit(testFile);

0 commit comments

Comments
 (0)