From 3e4727d67cf6888f5fb262a0f8a8fe3cfa25c96a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 13 Jun 2026 13:50:51 -0400 Subject: [PATCH] perf: Server startup time (#12680) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: lazy-load jsdom in DocumentHelper to keep it off startup path DocumentHelper is imported by the Document and Collection models, so its eager `import { JSDOM } from "jsdom"` pulled jsdom (~140ms) into every server process at boot. jsdom is only needed by toEmailDiff for HTML export, so load it lazily there — matching the existing pattern in the sibling ProsemirrorHelper.toHTML. Co-Authored-By: Claude Opus 4.8 * perf: defer AWS CRT signer load out of the startup path S3Storage had a top-level `import "@aws-sdk/signature-v4-crt"` side effect that loaded the native CRT binding (~140ms) whenever the module was imported — which happens at boot via the Attachment model in every process, even for local-storage deployments that never construct S3Storage. Move the import into the S3Storage constructor so the native binding only loads when S3 storage is actually instantiated, keeping it off the startup path for local-storage deployments. Co-Authored-By: Claude Opus 4.8 * perf: load dd-trace only when APM is enabled tracer.ts imported dd-trace unconditionally, so the library (which patches Node internals on require, ~35-80ms) loaded at startup in every process — including the common self-hosted case where DD_API_KEY is unset and APM does nothing. tracer.ts is the first import in server/index.ts, so this cost is fully serial on the boot path. Require dd-trace only when DD_API_KEY is set; otherwise fall back to a no-op tracer covering the surface used by callers (scope/startSpan and a no-op span), so tracing.ts and the named helpers work unchanged. The dd-trace/ext/tags import used by tracing.ts is a standalone constants module and does not pull in the full tracer. Co-Authored-By: Claude Opus 4.8 --------- Co-authored-by: Claude Opus 4.8 --- server/logging/tracer.ts | 29 +++++++++++++++++++++--- server/models/helpers/DocumentHelper.tsx | 3 ++- server/storage/files/S3Storage.ts | 6 ++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/server/logging/tracer.ts b/server/logging/tracer.ts index 61c9bd4cdc..ebe2a56949 100644 --- a/server/logging/tracer.ts +++ b/server/logging/tracer.ts @@ -1,5 +1,4 @@ -import type { Span } from "dd-trace"; -import tracer from "dd-trace"; +import type { Span, Tracer } from "dd-trace"; import env from "@server/env"; interface ReportableError extends Error { @@ -22,9 +21,25 @@ type PrivateDatadogContext = { }; }; +// dd-trace patches Node internals on require and is comparatively expensive to +// load, so it is kept off the startup path unless APM is actually enabled. When +// disabled we fall back to a no-op tracer that satisfies the surface consumed by +// callers (see tracing.ts) so they don't need to branch. +const noopSpan = { + addTags: () => noopSpan, + setTag: () => noopSpan, + finish: () => undefined, +} as unknown as Span; + +let tracer: Tracer; + // If the DataDog agent is installed and the DD_API_KEY environment variable is -// in the environment then we can safely attempt to start the DD tracer +// in the environment then we can safely attempt to start the DD tracer. This +// must happen before any instrumented module is imported. if (env.DD_API_KEY) { + const ddTrace = require("dd-trace") as { default?: Tracer }; + tracer = ddTrace.default ?? (ddTrace as unknown as Tracer); + tracer.init({ version: env.VERSION, service: env.DD_SERVICE, @@ -35,6 +50,14 @@ if (env.DD_API_KEY) { // Disable per-middleware spans so that non-reportable errors are not captured // This is also generally excessive noise tracer.use("koa", { middleware: false }); +} else { + tracer = { + scope: () => ({ + active: () => null, + activate: (_span: Span, fn: () => T) => fn(), + }), + startSpan: () => noopSpan, + } as unknown as Tracer; } const getCurrentSpan = (): Span | null => tracer.scope().active(); diff --git a/server/models/helpers/DocumentHelper.tsx b/server/models/helpers/DocumentHelper.tsx index 08813948c5..767dd7c108 100644 --- a/server/models/helpers/DocumentHelper.tsx +++ b/server/models/helpers/DocumentHelper.tsx @@ -1,4 +1,3 @@ -import { JSDOM } from "jsdom"; import { Node, Fragment, type NodeType } from "prosemirror-model"; import ukkonen from "ukkonen"; import { updateYFragment, yDocToProsemirrorJSON } from "y-prosemirror"; @@ -367,6 +366,8 @@ export class DocumentHelper { } const html = await DocumentHelper.diff(before, after, options); + // Loaded lazily to keep jsdom off the startup path — only HTML export needs it. + const { JSDOM } = await import("jsdom"); const dom = new JSDOM(html); const doc = dom.window.document; diff --git a/server/storage/files/S3Storage.ts b/server/storage/files/S3Storage.ts index b63bf36a0c..cf0663d3c9 100644 --- a/server/storage/files/S3Storage.ts +++ b/server/storage/files/S3Storage.ts @@ -9,7 +9,6 @@ import { CopyObjectCommand, } from "@aws-sdk/client-s3"; import { Upload } from "@aws-sdk/lib-storage"; -import "@aws-sdk/signature-v4-crt"; // https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt import type { PresignedPostOptions } from "@aws-sdk/s3-presigned-post"; import { createPresignedPost } from "@aws-sdk/s3-presigned-post"; import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; @@ -26,6 +25,11 @@ export default class S3Storage extends BaseStorage { constructor() { super(); + // Loaded here rather than at module top-level so the native CRT binding + // only loads when S3 storage is actually used, keeping it off startup. + // https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt + require("@aws-sdk/signature-v4-crt"); + this.client = new S3Client({ bucketEndpoint: env.AWS_S3_ACCELERATE_URL ? true : false, forcePathStyle: env.AWS_S3_FORCE_PATH_STYLE,