|
1 | 1 | /** |
2 | 2 | * Shared infrastructure for profiler modules (startupProfiler, queryProfiler, |
3 | | - * headlessProfiler). All three use the same perf_hooks timeline and the same |
4 | | - * line format for detailed reports. |
| 3 | + * headlessProfiler). |
| 4 | + * |
| 5 | + * Uses process.hrtime.bigint() for timing instead of perf_hooks.performance |
| 6 | + * to avoid a Bun/JSC memory leak: JSC's Performance object stores marks in a |
| 7 | + * C++ Vector that never shrinks even after clearMarks(). Long-running sessions |
| 8 | + * (daemon, /loop) accumulate hundreds of MB of dead capacity. |
| 9 | + * |
| 10 | + * The LightweightPerf class provides the same interface the profilers need |
| 11 | + * (mark, getEntriesByType, clearMarks, now) backed by a plain JS Map. |
5 | 12 | */ |
6 | 13 |
|
7 | | -import type { performance as PerformanceType } from 'perf_hooks' |
8 | 14 | import { formatFileSize } from './format.js' |
9 | 15 |
|
10 | | -// Lazy-load performance API only when profiling is enabled. |
11 | | -// Shared across all profilers — perf_hooks.performance is a process-wide singleton. |
12 | | -let performance: typeof PerformanceType | null = null |
| 16 | +/** Minimal PerformanceEntry-like object used by profilers */ |
| 17 | +export interface CheckpointEntry { |
| 18 | + readonly name: string |
| 19 | + readonly startTime: number |
| 20 | + readonly entryType: 'mark' |
| 21 | +} |
13 | 22 |
|
14 | | -export function getPerformance(): typeof PerformanceType { |
15 | | - if (!performance) { |
16 | | - // eslint-disable-next-line @typescript-eslint/no-require-imports |
17 | | - performance = require('perf_hooks').performance |
| 23 | +/** |
| 24 | + * Lightweight replacement for perf_hooks.performance that stores marks in a |
| 25 | + * plain JavaScript Map instead of JSC's C++ Vector. This avoids the memory |
| 26 | + * leak where clearMarks() sets the count to 0 but never frees Vector capacity. |
| 27 | + */ |
| 28 | +class LightweightPerf { |
| 29 | + private marks = new Map<string, number>() |
| 30 | + private _origin: number |
| 31 | + |
| 32 | + constructor() { |
| 33 | + this._origin = Number(process.hrtime.bigint() / 1000n) / 1000 |
| 34 | + } |
| 35 | + |
| 36 | + mark(name: string): void { |
| 37 | + this.marks.set(name, this.now()) |
18 | 38 | } |
19 | | - return performance! |
| 39 | + |
| 40 | + getEntriesByType(type: 'mark'): CheckpointEntry[] { |
| 41 | + if (type !== 'mark') return [] |
| 42 | + const entries: CheckpointEntry[] = [] |
| 43 | + for (const [name, startTime] of this.marks) { |
| 44 | + entries.push({ name, startTime, entryType: 'mark' }) |
| 45 | + } |
| 46 | + return entries |
| 47 | + } |
| 48 | + |
| 49 | + clearMarks(name?: string): void { |
| 50 | + if (name !== undefined) { |
| 51 | + this.marks.delete(name) |
| 52 | + } else { |
| 53 | + this.marks.clear() |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + now(): number { |
| 58 | + return Number(process.hrtime.bigint() / 1000n) / 1000 - this._origin |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Singleton — shared across all profilers (same as the old perf_hooks singleton) |
| 63 | +const perf = new LightweightPerf() |
| 64 | + |
| 65 | +export function getPerformance(): LightweightPerf { |
| 66 | + return perf |
20 | 67 | } |
21 | 68 |
|
22 | 69 | export function formatMs(ms: number): string { |
|
0 commit comments