|
| 1 | +/** |
| 2 | + * Performance shim — replaces globalThis.performance to prevent JSC's C++ Vector |
| 3 | + * from growing without bound. |
| 4 | + * |
| 5 | + * In Bun, globalThis.performance is JSC's native Performance object. It stores |
| 6 | + * marks, measures, and resource timings in a C++ Vector that never shrinks even |
| 7 | + * after clearMarks(). Long-running sessions (daemon, /loop) accumulate hundreds |
| 8 | + * of MB of dead capacity. |
| 9 | + * |
| 10 | + * This shim keeps performance.now() on the native object (fast, no memory cost) |
| 11 | + * but redirects mark/measure/getEntries operations to a plain JS Map that the GC |
| 12 | + * can reclaim. Third-party code (React reconciler, OTel/Langfuse) uses |
| 13 | + * performance.now() for timing — that stays native. The accumulating operations |
| 14 | + * go to GC-able JS memory instead. |
| 15 | + * |
| 16 | + * MUST be installed before React/OTel import — see cli.tsx first import. |
| 17 | + */ |
| 18 | + |
| 19 | +const original = globalThis.performance |
| 20 | + |
| 21 | +// JS-backed storage — fully GC-able |
| 22 | +const marks = new Map<string, number>() |
| 23 | +const measures = new Map< |
| 24 | + string, |
| 25 | + { name: string; startTime: number; duration: number } |
| 26 | +>() |
| 27 | + |
| 28 | +function now(): number { |
| 29 | + return original.now() |
| 30 | +} |
| 31 | + |
| 32 | +function mark(name: string): PerformanceMark { |
| 33 | + marks.set(name, now()) |
| 34 | + // Return a minimal PerformanceMark-like object to satisfy the interface. |
| 35 | + // React/OTel only use mark() for side effects, not the return value. |
| 36 | + return { |
| 37 | + name, |
| 38 | + entryType: 'mark', |
| 39 | + startTime: marks.get(name)!, |
| 40 | + duration: 0, |
| 41 | + } as PerformanceMark |
| 42 | +} |
| 43 | + |
| 44 | +function measure( |
| 45 | + name: string, |
| 46 | + startMarkOrOptions?: string | MeasureOptions, |
| 47 | + endMark?: string, |
| 48 | +): void { |
| 49 | + let startTime: number |
| 50 | + let duration: number |
| 51 | + |
| 52 | + if (typeof startMarkOrOptions === 'string') { |
| 53 | + const start = marks.get(startMarkOrOptions) |
| 54 | + const end = endMark ? marks.get(endMark) : now() |
| 55 | + startTime = start ?? now() |
| 56 | + duration = (end ?? now()) - startTime |
| 57 | + } else if (startMarkOrOptions && typeof startMarkOrOptions === 'object') { |
| 58 | + startTime = startMarkOrOptions.start ?? 0 |
| 59 | + duration = (startMarkOrOptions.end ?? now()) - startTime |
| 60 | + } else { |
| 61 | + startTime = 0 |
| 62 | + duration = now() |
| 63 | + } |
| 64 | + |
| 65 | + measures.set(name, { name, startTime, duration }) |
| 66 | +} |
| 67 | + |
| 68 | +interface MeasureOptions { |
| 69 | + start?: number |
| 70 | + end?: number |
| 71 | + detail?: unknown |
| 72 | +} |
| 73 | + |
| 74 | +interface PerformanceEntryLike { |
| 75 | + readonly name: string |
| 76 | + readonly entryType: string |
| 77 | + readonly startTime: number |
| 78 | + readonly duration: number |
| 79 | +} |
| 80 | + |
| 81 | +function getEntriesByType(type: string): PerformanceEntryLike[] { |
| 82 | + if (type === 'mark') { |
| 83 | + return [...marks.entries()].map(([name, startTime]) => ({ |
| 84 | + name, |
| 85 | + entryType: 'mark', |
| 86 | + startTime, |
| 87 | + duration: 0, |
| 88 | + })) |
| 89 | + } |
| 90 | + if (type === 'measure') { |
| 91 | + return [...measures.values()].map(m => ({ |
| 92 | + name: m.name, |
| 93 | + entryType: 'measure', |
| 94 | + startTime: m.startTime, |
| 95 | + duration: m.duration, |
| 96 | + })) |
| 97 | + } |
| 98 | + return [] |
| 99 | +} |
| 100 | + |
| 101 | +function getEntriesByName(name: string, type?: string): PerformanceEntryLike[] { |
| 102 | + const entries = getEntriesByType(type ?? 'mark').concat( |
| 103 | + type === undefined ? getEntriesByType('measure') : [], |
| 104 | + ) |
| 105 | + return entries.filter(e => e.name === name) |
| 106 | +} |
| 107 | + |
| 108 | +function clearMarks(name?: string): void { |
| 109 | + if (name !== undefined) { |
| 110 | + marks.delete(name) |
| 111 | + } else { |
| 112 | + marks.clear() |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function clearMeasures(name?: string): void { |
| 117 | + if (name !== undefined) { |
| 118 | + measures.delete(name) |
| 119 | + } else { |
| 120 | + measures.clear() |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// Plain object shim — must NOT inherit from Performance.prototype because |
| 125 | +// native getters (onresourcetimingbufferfull, timeOrigin, toJSON) check |
| 126 | +// that `this` is an actual JSC Performance instance and throw otherwise. |
| 127 | +const shim = { |
| 128 | + now, |
| 129 | + mark, |
| 130 | + measure: measure as typeof performance.measure, |
| 131 | + getEntriesByType: getEntriesByType as typeof performance.getEntriesByType, |
| 132 | + getEntriesByName: getEntriesByName as typeof performance.getEntriesByName, |
| 133 | + clearMarks: clearMarks as typeof performance.clearMarks, |
| 134 | + clearMeasures: clearMeasures as typeof performance.clearMeasures, |
| 135 | + clearResourceTimings: (() => {}) as typeof performance.clearResourceTimings, |
| 136 | + setResourceTimingBufferSize: |
| 137 | + (() => {}) as typeof performance.setResourceTimingBufferSize, |
| 138 | + // Delegate read-only properties to the original |
| 139 | + get timeOrigin() { |
| 140 | + return original.timeOrigin |
| 141 | + }, |
| 142 | + get onresourcetimingbufferfull() { |
| 143 | + return (original as any).onresourcetimingbufferfull |
| 144 | + }, |
| 145 | + set onresourcetimingbufferfull(_v: any) { |
| 146 | + // no-op — prevent accumulation |
| 147 | + }, |
| 148 | + toJSON() { |
| 149 | + return original.toJSON() |
| 150 | + }, |
| 151 | +} as typeof performance |
| 152 | + |
| 153 | +/** |
| 154 | + * Install the shim onto globalThis.performance. Safe to call multiple times. |
| 155 | + * Must run before React and OTel import to prevent them from capturing the |
| 156 | + * native Performance reference. |
| 157 | + */ |
| 158 | +export function installPerformanceShim(): void { |
| 159 | + if ((globalThis as any).__performanceShimInstalled) return |
| 160 | + ;(globalThis as any).__performanceShimInstalled = true |
| 161 | + globalThis.performance = shim |
| 162 | +} |
| 163 | + |
| 164 | +// Auto-install on import |
| 165 | +installPerformanceShim() |
0 commit comments