Skip to content

Commit

Permalink
feat: automatically clear collected types when memory pressure is too…
Browse files Browse the repository at this point in the history
… high (#2187)
  • Loading branch information
fwouts authored Nov 6, 2023
1 parent b21812f commit a6b1e18
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions type-analyzer/src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs";
import { globbySync } from "globby";
import path from "path";
import ts from "typescript";
import v8 from "v8";
import type {
CollectedTypes,
OptionalType,
Expand Down Expand Up @@ -39,18 +40,18 @@ import { typescriptServiceHost } from "./ts-service-host.js";
import { computeUnion } from "./union.js";
export type { TypeAnalyzer, TypeResolver };

const HEAP_SIZE_QUOTA_LIMIT_BEFORE_GC = 0.7;

export function createTypeAnalyzer(options: {
rootDir: string;
reader: Reader;
collected?: CollectedTypes;
tsCompilerOptions?: Partial<ts.CompilerOptions>;
specialTypes?: Record<string, ValueType>;
warn?: (message: string) => void;
}): TypeAnalyzer {
return new TypeAnalyzer(
options.rootDir,
options.reader,
options.collected || {},
options.specialTypes || {},
options.tsCompilerOptions || {},
options.warn
Expand All @@ -62,11 +63,11 @@ class TypeAnalyzer {
private entryPointFilePaths: string[] = [];
private printedWarnings = new Set<string>();
private declarationFilePaths = new Set<string>();
private collected: CollectedTypes = {};

constructor(
private readonly rootDir: string,
private readonly reader: Reader,
private readonly collected: CollectedTypes,
private readonly specialTypes: Record<string, ValueType>,
tsCompilerOptions: Partial<ts.CompilerOptions>,
private readonly warn?: (message: string) => void
Expand Down Expand Up @@ -153,6 +154,10 @@ class TypeAnalyzer {
this.printedWarnings.add(messageText);
}
}
const { used_heap_size, heap_size_limit } = v8.getHeapStatistics();
if (used_heap_size > HEAP_SIZE_QUOTA_LIMIT_BEFORE_GC * heap_size_limit) {
this.collected = {};
}
return new TypeResolver(
this.rootDir,
this.collected,
Expand Down

0 comments on commit a6b1e18

Please sign in to comment.