Perf: run reverse chn cidr in worker_threads

This commit is contained in:
SukkaW
2025-01-10 00:17:28 +08:00
parent 996575236d
commit 7e5fad3362
5 changed files with 78 additions and 21 deletions

View File

@@ -1,3 +1,4 @@
import { noop } from 'foxts/noop';
import { basename, extname } from 'node:path';
import process from 'node:process';
import picocolors from 'picocolors';
@@ -98,8 +99,12 @@ export function createSpan(name: string, parentTraceResult?: TraceResult): Span
export const dummySpan = createSpan('');
export function task(importMetaMain: boolean, importMetaPath: string) {
return <T>(fn: (span: Span) => Promise<T>, customName?: string) => {
return <T>(fn: (span: Span, onCleanup: (cb: () => Promise<void> | void) => void) => Promise<T>, customName?: string) => {
const taskName = customName ?? basename(importMetaPath, extname(importMetaPath));
let cleanup: () => Promise<void> | void = noop;
const onCleanup = (cb: () => void) => {
cleanup = cb;
};
const dummySpan = createSpan(taskName);
if (importMetaMain) {
@@ -112,7 +117,7 @@ export function task(importMetaMain: boolean, importMetaPath: string) {
process.exit(1);
});
dummySpan.traceChildAsync('dummy', fn).finally(() => {
dummySpan.traceChildAsync('dummy', (childSpan) => fn(childSpan, onCleanup)).finally(() => {
dummySpan.stop();
printTraceResult(dummySpan.traceResult);
whyIsNodeRunning();
@@ -121,9 +126,9 @@ export function task(importMetaMain: boolean, importMetaPath: string) {
return async (span?: Span) => {
if (span) {
return span.traceChildAsync(taskName, fn);
return span.traceChildAsync(taskName, (childSpan) => fn(childSpan, onCleanup).finally(() => cleanup()));
}
return fn(dummySpan);
return fn(dummySpan, onCleanup).finally(() => cleanup());
};
};
}