Skip to content

Commit aacfb8e

Browse files
committed
Fix memory issues with packages mode
Resolves #2607
1 parent f031595 commit aacfb8e

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Anchor links are no longer incorrectly checked for relative paths, #2604.
1111
- Fixed an issue where line numbers reported in error messages could be incorrect, #2605.
1212
- Fixed relative link detection for markdown links containing code in their label, #2606.
13+
- Fixed an issue with packages mode where TypeDoc would use (much) more memory than required, #2607.
1314
- TypeDoc will no longer crash when asked to render highlighted code for an unsupported language, #2609.
1415

1516
### Thanks!

src/lib/application.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ export class Application extends ChildableComponent<
648648
return;
649649
}
650650

651+
const origFiles = this.files;
651652
const origOptions = this.options;
652653
const projects: JSONOutput.ProjectReflection[] = [];
653654

@@ -693,18 +694,29 @@ export class Application extends ChildableComponent<
693694
for (const { dir, options } of projectsToConvert) {
694695
this.logger.info(this.i18n.converting_project_at_0(nicePath(dir)));
695696
this.options = options;
696-
const project = await this.convert();
697+
this.files = new ValidatingFileRegistry();
698+
let project = await this.convert();
697699
if (project) {
698700
this.validate(project);
699-
projects.push(
700-
this.serializer.projectToObject(project, process.cwd()),
701+
const serialized = this.serializer.projectToObject(
702+
project,
703+
process.cwd(),
701704
);
705+
projects.push(serialized);
702706
}
703707

708+
// When debugging memory issues, it's useful to set these
709+
// here so that a breakpoint on resetReflectionID below
710+
// gets the memory as it ought to be with all TS objects released.
711+
project = undefined;
712+
this.files = undefined!;
713+
// global.gc!();
714+
704715
resetReflectionID();
705716
}
706717

707718
this.options = origOptions;
719+
this.files = origFiles;
708720

709721
if (projects.length !== packageDirs.length) {
710722
this.logger.error(this.i18n.failed_to_convert_packages());

src/lib/models/FileRegistry.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { basename, dirname, parse, relative, resolve } from "path";
22
import type { Deserializer, Serializer } from "../serialization";
3-
import type { FileRegistry as JSONMediaRegistry } from "../serialization/schema";
3+
import type { FileRegistry as JSONFileRegistry } from "../serialization/schema";
44
import { normalizePath } from "../utils";
55
import { existsSync } from "fs";
66
import type { Reflection } from "./reflections";
@@ -88,8 +88,8 @@ export class FileRegistry {
8888
return result;
8989
}
9090

91-
toObject(ser: Serializer): JSONMediaRegistry {
92-
const result: JSONMediaRegistry = {
91+
toObject(ser: Serializer): JSONFileRegistry {
92+
const result: JSONFileRegistry = {
9393
entries: {},
9494
reflections: {},
9595
};
@@ -104,7 +104,12 @@ export class FileRegistry {
104104
return result;
105105
}
106106

107-
fromObject(de: Deserializer, obj: JSONMediaRegistry): void {
107+
/**
108+
* Revive a file registry from disc.
109+
* Note that in the packages context this may be called multiple times on
110+
* a single object, and should merge in files from the other registries.
111+
*/
112+
fromObject(de: Deserializer, obj: JSONFileRegistry): void {
108113
for (const [key, val] of Object.entries(obj.entries)) {
109114
const absolute = normalizePath(resolve(de.projectRoot, val));
110115
de.oldFileIdToNewFileId[+key] = this.registerAbsolute(absolute);
@@ -138,7 +143,7 @@ export class ValidatingFileRegistry extends FileRegistry {
138143
return this.registerAbsolute(absolute);
139144
}
140145

141-
override fromObject(de: Deserializer, obj: JSONMediaRegistry) {
146+
override fromObject(de: Deserializer, obj: JSONFileRegistry) {
142147
for (const [key, val] of Object.entries(obj.entries)) {
143148
const absolute = normalizePath(resolve(de.projectRoot, val));
144149
if (!existsSync(absolute)) {

0 commit comments

Comments
 (0)