Skip to content

Commit

Permalink
fix: clear programs cache if >90% of heap is used (#643)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #642
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/eslint-plugin-expect-type/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/eslint-plugin-expect-type/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

I played around with different strategies a bit, but ended up doing
basically the same thing as
microsoft/DefinitelyTyped-tools#1024. Therefore:

Co-authored-by: Andrew Branch <andrew@wheream.io>
  • Loading branch information
JoshuaKGoldberg and andrewbranch authored Nov 29, 2024
1 parent 7ab053e commit 9596a34
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
47 changes: 47 additions & 0 deletions src/utils/programs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import ts from "typescript";
import { describe, expect, it, vi } from "vitest";

import { getProgramForVersion } from "./programs.js";

const mockGetHeapStatistics = vi.fn<() => Record<string, number>>();

vi.mock("node:v8", () => ({
get default() {
return { getHeapStatistics: mockGetHeapStatistics };
},
}));

const originalProgram = ts.createProgram({
options: {},
rootNames: [],
});

function actGetProgramForVersion() {
return getProgramForVersion("tsconfig.json", ts, "current", originalProgram);
}

describe("getProgramForVersion", () => {
it("reuses old programs when <90% of the heap is used", () => {
mockGetHeapStatistics.mockReturnValue({
heap_size_limit: 100,
used_heap_size: 90,
});

const programA = actGetProgramForVersion();
const programB = actGetProgramForVersion();

expect(programA).toBe(programB);
});

it("does not reuse old programs when >90% of the heap is used", () => {
mockGetHeapStatistics.mockReturnValue({
heap_size_limit: 100,
used_heap_size: 91,
});

const programA = actGetProgramForVersion();
const programB = actGetProgramForVersion();

expect(programA).not.toBe(programB);
});
});
19 changes: 13 additions & 6 deletions src/utils/programs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type * as ts from "typescript";

import fs from "node:fs";
import path from "node:path";
import v8 from "node:v8";

export type TSModule = typeof ts;

Expand Down Expand Up @@ -46,19 +47,25 @@ function createProgram(configFile: string, ts: TSModule): ts.Program {
export function getProgramForVersion(
configFile: string,
ts: TSModule,
versionName: string,
lintProgram: ts.Program,
version: string,
originalProgram: ts.Program,
): ts.Program {
let versionToProgram = programCache.get(lintProgram);
let versionToProgram = programCache.get(originalProgram);
if (versionToProgram === undefined) {
versionToProgram = new Map<string, ts.Program>();
programCache.set(lintProgram, versionToProgram);
programCache.set(originalProgram, versionToProgram);
}

let newProgram = versionToProgram.get(versionName);
let newProgram = versionToProgram.get(version);
if (newProgram === undefined) {
newProgram = createProgram(configFile, ts);
versionToProgram.set(versionName, newProgram);
versionToProgram.set(version, newProgram);
}

const heapStats = v8.getHeapStatistics();
const heapUsage = heapStats.used_heap_size / heapStats.heap_size_limit;
if (heapUsage > 0.9) {
versionToProgram.clear();
}

return newProgram;
Expand Down

0 comments on commit 9596a34

Please sign in to comment.