Skip to content

Commit

Permalink
Fix memory issues with packages mode
Browse files Browse the repository at this point in the history
Resolves #2607
  • Loading branch information
Gerrit0 committed Jun 22, 2024
1 parent f031595 commit aacfb8e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Anchor links are no longer incorrectly checked for relative paths, #2604.
- Fixed an issue where line numbers reported in error messages could be incorrect, #2605.
- Fixed relative link detection for markdown links containing code in their label, #2606.
- Fixed an issue with packages mode where TypeDoc would use (much) more memory than required, #2607.
- TypeDoc will no longer crash when asked to render highlighted code for an unsupported language, #2609.

### Thanks!
Expand Down
18 changes: 15 additions & 3 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ export class Application extends ChildableComponent<
return;
}

const origFiles = this.files;
const origOptions = this.options;
const projects: JSONOutput.ProjectReflection[] = [];

Expand Down Expand Up @@ -693,18 +694,29 @@ export class Application extends ChildableComponent<
for (const { dir, options } of projectsToConvert) {
this.logger.info(this.i18n.converting_project_at_0(nicePath(dir)));
this.options = options;
const project = await this.convert();
this.files = new ValidatingFileRegistry();
let project = await this.convert();
if (project) {
this.validate(project);
projects.push(
this.serializer.projectToObject(project, process.cwd()),
const serialized = this.serializer.projectToObject(
project,
process.cwd(),
);
projects.push(serialized);
}

// When debugging memory issues, it's useful to set these
// here so that a breakpoint on resetReflectionID below
// gets the memory as it ought to be with all TS objects released.
project = undefined;
this.files = undefined!;
// global.gc!();

resetReflectionID();
}

this.options = origOptions;
this.files = origFiles;

if (projects.length !== packageDirs.length) {
this.logger.error(this.i18n.failed_to_convert_packages());
Expand Down
15 changes: 10 additions & 5 deletions src/lib/models/FileRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename, dirname, parse, relative, resolve } from "path";
import type { Deserializer, Serializer } from "../serialization";
import type { FileRegistry as JSONMediaRegistry } from "../serialization/schema";
import type { FileRegistry as JSONFileRegistry } from "../serialization/schema";
import { normalizePath } from "../utils";
import { existsSync } from "fs";
import type { Reflection } from "./reflections";
Expand Down Expand Up @@ -88,8 +88,8 @@ export class FileRegistry {
return result;
}

toObject(ser: Serializer): JSONMediaRegistry {
const result: JSONMediaRegistry = {
toObject(ser: Serializer): JSONFileRegistry {
const result: JSONFileRegistry = {
entries: {},
reflections: {},
};
Expand All @@ -104,7 +104,12 @@ export class FileRegistry {
return result;
}

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

override fromObject(de: Deserializer, obj: JSONMediaRegistry) {
override fromObject(de: Deserializer, obj: JSONFileRegistry) {
for (const [key, val] of Object.entries(obj.entries)) {
const absolute = normalizePath(resolve(de.projectRoot, val));
if (!existsSync(absolute)) {
Expand Down

0 comments on commit aacfb8e

Please sign in to comment.