Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/one-shot-markdown-export-memory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@docx-editor.dev/docx-to-markdown': patch
---

`exportMarkdown` now releases the document it converted before it returns. Before, consecutive exports of in-memory files kept each earlier document in memory until the calling task ended, about 200 MB per 500-page document.
14 changes: 14 additions & 0 deletions packages/docx-to-markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,18 @@ export function exportMarkdownLayout(layout: ExportSemanticLayout): MarkdownExpo
return translateMarkdownLayout(layout);
}

/**
* Let the runtime drop the objects that `WeakRef` creation and `deref()` keep alive for the
* current job. The engine caches use `WeakRef`, and the runtime keeps each target strongly until
* the microtask checkpoint ends, not until the export returns. An export of in-memory bytes runs
* entirely in microtasks, so without a task boundary every disposed document tree and the caches
* keyed by it stay live, and back-to-back exports add about 200 MB each. A timer callback runs
* after the checkpoint on every host, so it releases them before the next export starts.
*/
function releaseKeptWeakRefTargets(): Promise<void> {
return new Promise<void>((resolve) => setTimeout(resolve, 0));
}

/** Convert untrusted DOCX bytes with Node-safe resource and shaping defaults. @public */
export async function exportMarkdown(
source: ExportDocumentSource,
Expand Down Expand Up @@ -462,6 +474,8 @@ export async function exportMarkdown(
} finally {
opened.session.dispose();
}
await releaseKeptWeakRefTargets();
checkExportAbort(options.signal);
const result = images
? translateMarkdownLayout(layout, await resolveMedia(data, options), images.syntax)
: translateMarkdownLayout(layout);
Expand Down
13 changes: 8 additions & 5 deletions packages/docx-to-markdown/test/browser-node-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,11 @@ test('a real 500-page shaped export holds a bounded live set on a default heap',
expect(measurement.peakRssBytes).toBeLessThan(RESIDENT_BACKSTOP);
}, 120_000);

// The one-shot export releases the layout before measurement, so its live heap settles far
// below the retained-layout case: 224 MiB on `main` and 230 MiB with the PDF export work, on
// the same machine, against the 256 MiB ceiling asserted below. Its old-space cap only bounds
// The one-shot export releases the layout and yields to a timer before it returns, so the
// runtime can drop the `WeakRef` targets it keeps for the current job. Its live heap after two
// consecutive exports settles near 63 MiB on Node 24 (macOS arm64), against the 256 MiB ceiling
// asserted below. Without that yield, one export measured 256 MiB and two exceeded the 400 MiB
// cap, because each pinned its whole disposed document. Its old-space cap only bounds
// transient growth during the forced collections, and 364 MiB proved marginal for that on the
// Linux runners, which aborted with SIGABRT on two consecutive runs while macOS passed. 400 MiB
// keeps a hard bound well under the 512 MiB the retained case needs, for the same reason that
Expand All @@ -281,11 +283,12 @@ const RETAINED_LAYOUT_NODE_ARGUMENTS = [
'--max-semi-space-size=8',
] as const;

test('the one-shot 500-page export fits a constrained 400 MiB heap', () => {
test('two consecutive one-shot 500-page exports fit a constrained 400 MiB heap', () => {
const measurement = performanceMeasurement(ONE_SHOT_NODE_ARGUMENTS, 'one-shot-performance');
expectProductionFixture(measurement, { inspectLayout: false });
// The one-shot entry point returns markdown and drops the layout, so it settles well below the
// retained-layout ceiling. That gap is the point: it proves the export does not pin the layout.
// retained-layout ceiling. That gap is the point: it proves the export does not pin the layout,
// and the second export proves that the first one's document does not stay live.
expect(measurement.liveHeapBytes).toBeLessThan(ONE_SHOT_LIVE_HEAP_CEILING);
expect(measurement.externalBytes).toBeLessThan(EXTERNAL_MEMORY_CEILING);
}, 120_000);
Expand Down
4 changes: 4 additions & 0 deletions packages/docx-to-markdown/test/literal-node-worker-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ if (mode === 'performance' || mode === 'one-shot-performance') {
}
const fixture = await readFile(fixturePath);
if (mode === 'one-shot-performance') {
// Export twice in a row, as a batch converter does. The second export must not inherit the
// first one's document: an export that returns inside the same microtask checkpoint keeps
// every `WeakRef` target of the engine caches alive, and two such exports exceed the cap.
await exportMarkdown(fixture);
const translated = await exportMarkdown(fixture);
process.stdout.write(
JSON.stringify({
Expand Down
Loading