Skip to content

Commit

Permalink
fix: should not cache tracing instance (#9086)
Browse files Browse the repository at this point in the history
* fix(tracing): should not cache tracing instance

* chore: lint
  • Loading branch information
h-a-n-a authored Jan 23, 2025
1 parent 404bfb3 commit 64bc74a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/rspack-tracing/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export function initOpenTelemetry(): Promise<void>;
export function shutdownOpenTelemetry(): Promise<void>;
export { trace, propagation, context } from "@opentelemetry/api";
export type { TraceAPI, ContextAPI, PropagationAPI } from "@opentelemetry/api";
export type { Tracer, Context } from "@opentelemetry/api";
60 changes: 44 additions & 16 deletions packages/rspack/src/loader-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
SourceMapSource
} from "webpack-sources";

import type { Context, Tracer } from "@rspack/tracing";
import type { ContextAPI, PropagationAPI, TraceAPI } from "@rspack/tracing";
import type { Compilation } from "../Compilation";
import type { Compiler } from "../Compiler";
import { Module } from "../Module";
Expand Down Expand Up @@ -340,36 +340,64 @@ function getCurrentLoader(
}
return null;
}
let tracingCache!: { tracer?: Tracer; activeContext?: Context };
async function tryTrace(context: JsLoaderContext) {

// FIXME: a temporary fix, we may need to change @rspack/tracing to commonjs really fix it
let cachedTracing:
| {
trace: TraceAPI;
propagation: PropagationAPI;
context: ContextAPI;
}
| null
| undefined;

async function getCachedTracing() {
// disable tracing in non-profile mode
if (!process.env.RSPACK_PROFILE) {
return {};
cachedTracing = null;
return cachedTracing;
}
try {
const {
trace,
propagation,
context: tracingContext
} = await import("@rspack/tracing");
if (cachedTracing) {
return cachedTracing;
}
if (cachedTracing === undefined) {
try {
const tracing = await import("@rspack/tracing");
cachedTracing = {
trace: tracing.trace,
propagation: tracing.propagation,
context: tracing.context
};
return cachedTracing;
} catch (e) {
cachedTracing = null;
return cachedTracing;
}
} else {
cachedTracing = null;
return cachedTracing;
}
}

async function tryTrace(context: JsLoaderContext) {
const cachedTracing = await getCachedTracing();
if (cachedTracing) {
const { trace, propagation, context: tracingContext } = cachedTracing;
const tracer = trace.getTracer("rspack-loader-runner");
const activeContext = propagation.extract(
tracingContext.active(),
context.__internal__tracingCarrier
);
tracingCache = { tracer, activeContext };
return tracingCache;
} catch (error) {
tracingCache = {};
return tracingCache;
return { trace, tracer, activeContext };
}
return null;
}

export async function runLoaders(
compiler: Compiler,
context: JsLoaderContext
): Promise<JsLoaderContext> {
const { tracer, activeContext } = await tryTrace(context);
const { tracer, activeContext } = (await tryTrace(context)) ?? {};

const loaderState = context.loaderState;

Expand Down

2 comments on commit 64bc74a

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 64bc74a Jan 23, 2025

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2025-01-21 c06787b) Current Change
10000_big_production-mode_disable-minimize + exec 36.7 s ± 515 ms 39 s ± 694 ms +6.33 %
10000_development-mode + exec 1.86 s ± 52 ms 1.84 s ± 82 ms -1.22 %
10000_development-mode_hmr + exec 681 ms ± 9.8 ms 684 ms ± 7.6 ms +0.44 %
10000_production-mode + exec 2.45 s ± 138 ms 2.42 s ± 225 ms -0.97 %
10000_production-mode_persistent-cold + exec 2.61 s ± 155 ms 2.56 s ± 44 ms -1.90 %
10000_production-mode_persistent-hot + exec 1.77 s ± 116 ms 1.77 s ± 38 ms +0.17 %
arco-pro_development-mode + exec 1.76 s ± 98 ms 1.8 s ± 100 ms +2.37 %
arco-pro_development-mode_hmr + exec 388 ms ± 2.3 ms 389 ms ± 6.1 ms +0.21 %
arco-pro_production-mode + exec 3.64 s ± 257 ms 3.77 s ± 297 ms +3.72 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.75 s ± 314 ms 3.75 s ± 96 ms -0.13 %
arco-pro_production-mode_persistent-cold + exec 3.8 s ± 165 ms 3.91 s ± 151 ms +2.84 %
arco-pro_production-mode_persistent-hot + exec 2.34 s ± 79 ms 2.57 s ± 154 ms +9.91 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.67 s ± 99 ms 3.82 s ± 219 ms +4.32 %
large-dyn-imports_development-mode + exec 2.11 s ± 29 ms 2.11 s ± 45 ms +0.21 %
large-dyn-imports_production-mode + exec 2.16 s ± 40 ms 2.14 s ± 38 ms -0.73 %
threejs_development-mode_10x + exec 1.53 s ± 22 ms 1.56 s ± 73 ms +2.26 %
threejs_development-mode_10x_hmr + exec 788 ms ± 39 ms 794 ms ± 24 ms +0.71 %
threejs_production-mode_10x + exec 5.25 s ± 61 ms 5.45 s ± 300 ms +3.78 %
threejs_production-mode_10x_persistent-cold + exec 5.35 s ± 65 ms 5.58 s ± 354 ms +4.28 %
threejs_production-mode_10x_persistent-hot + exec 4.5 s ± 44 ms 4.81 s ± 323 ms +6.78 %
10000_big_production-mode_disable-minimize + rss memory 8680 MiB ± 34.2 MiB 8711 MiB ± 88.2 MiB +0.36 %
10000_development-mode + rss memory 656 MiB ± 19.6 MiB 651 MiB ± 30.8 MiB -0.67 %
10000_development-mode_hmr + rss memory 1264 MiB ± 163 MiB 1256 MiB ± 298 MiB -0.67 %
10000_production-mode + rss memory 639 MiB ± 31 MiB 645 MiB ± 23.4 MiB +0.85 %
10000_production-mode_persistent-cold + rss memory 744 MiB ± 15.6 MiB 749 MiB ± 20.3 MiB +0.56 %
10000_production-mode_persistent-hot + rss memory 723 MiB ± 37.7 MiB 732 MiB ± 18.5 MiB +1.19 %
arco-pro_development-mode + rss memory 553 MiB ± 38.1 MiB 573 MiB ± 17.8 MiB +3.62 %
arco-pro_development-mode_hmr + rss memory 631 MiB ± 36 MiB 656 MiB ± 26 MiB +3.95 %
arco-pro_production-mode + rss memory 738 MiB ± 66.3 MiB 710 MiB ± 24.3 MiB -3.77 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 730 MiB ± 23.1 MiB 730 MiB ± 30.6 MiB -0.01 %
arco-pro_production-mode_persistent-cold + rss memory 844 MiB ± 36.6 MiB 852 MiB ± 33 MiB +1.03 %
arco-pro_production-mode_persistent-hot + rss memory 695 MiB ± 19.9 MiB 706 MiB ± 15.4 MiB +1.57 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 727 MiB ± 32.5 MiB 715 MiB ± 36.5 MiB -1.61 %
large-dyn-imports_development-mode + rss memory 642 MiB ± 4.5 MiB 642 MiB ± 6.14 MiB -0.04 %
large-dyn-imports_production-mode + rss memory 523 MiB ± 4.79 MiB 523 MiB ± 1.82 MiB -0.03 %
threejs_development-mode_10x + rss memory 551 MiB ± 32.9 MiB 543 MiB ± 30.7 MiB -1.35 %
threejs_development-mode_10x_hmr + rss memory 1145 MiB ± 125 MiB 1125 MiB ± 137 MiB -1.73 %
threejs_production-mode_10x + rss memory 865 MiB ± 36.9 MiB 835 MiB ± 21.2 MiB -3.55 %
threejs_production-mode_10x_persistent-cold + rss memory 968 MiB ± 136 MiB 939 MiB ± 36.3 MiB -2.96 %
threejs_production-mode_10x_persistent-hot + rss memory 870 MiB ± 25.8 MiB 846 MiB ± 52.3 MiB -2.78 %

Threshold exceeded: ["10000_big_production-mode_disable-minimize + exec","arco-pro_production-mode_persistent-hot + exec","threejs_production-mode_10x_persistent-hot + exec"]

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 64bc74a Jan 23, 2025

Choose a reason for hiding this comment

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

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
rsdoctor ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

Please sign in to comment.