mirror of
https://github.com/compiler-explorer/compiler-explorer.git
synced 2026-11-30 18:34:31 -06:00
Profiling a production startup (2026-08-20, 6232 compilers) showed two avoidable CPU costs in the last couple of seconds of startup: - `setupCompilerChangeHandling`'s `onCompilerChange` ran `JSON.stringify` over all 6232 compiler objects purely to detect whether the compiler set had changed since the last discovery. The serialised form is comparable in scale to the 74MB discovery JSON, and it was computed (and retained in memory) even in production, where rescanning is disabled (`rescanCompilerSecs` unset / `--prediscovered`) and so the comparison can never trigger. - The `OPTIONS HASH` log line appeared twice per startup: `ClientOptionsHandler`'s constructor eagerly serialised and hashed the options blob, only for the result to be immediately discarded and recomputed by the first `setCompilers()` call. Changes: - `lib/app/compiler-changes.ts`: only do change detection when rescanning is actually enabled, and compare a SHA-256 content hash instead of retaining the full serialised compiler list. In production the initial set is now applied with no serialisation at all; with rescanning enabled, behaviour is unchanged (identical rescans are skipped, changed sets applied) while dropping the tens-of-megabytes retained string. - `lib/options-handler.ts`: drop the constructor's `_updateOptionsHash()` call. The hash/JSON are computed exactly once at startup by the first `setCompilers()`, which always runs before the server starts listening. Tests: new `test/app/compiler-changes-tests.ts` covering initial application, no rescan when prediscovered, unchanged rescans skipped, and changed rescans applied; plus an options-handler test pinning that the hash is only computed once compilers are set. `ts-check`, `lint`, and the full test suite pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
90 lines
3.8 KiB
TypeScript
90 lines
3.8 KiB
TypeScript
// Copyright (c) 2025, Compiler Explorer Authors
|
|
// All rights reserved.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without
|
|
// modification, are permitted provided that the following conditions are met:
|
|
//
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
// this list of conditions and the following disclaimer.
|
|
// * Redistributions in binary form must reproduce the above copyright
|
|
// notice, this list of conditions and the following disclaimer in the
|
|
// documentation and/or other materials provided with the distribution.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
import {CompilerInfo} from '../../types/compiler.interfaces.js';
|
|
import type {Language, LanguageKey} from '../../types/languages.interfaces.js';
|
|
import type {AppArguments} from '../app.interfaces.js';
|
|
import {unwrap} from '../assert.js';
|
|
import {CompilerFinder} from '../compiler-finder.js';
|
|
import {RouteAPI} from '../handlers/route-api.js';
|
|
import {logger} from '../logger.js';
|
|
import {ClientOptionsHandler} from '../options-handler.js';
|
|
import {PropertyGetter} from '../properties.interfaces.js';
|
|
import {getHash} from '../utils.js';
|
|
|
|
/**
|
|
* Setup handling of compiler changes and periodic rescanning
|
|
* @param initialCompilers - The initial set of compilers
|
|
* @param clientOptionsHandler - Client options handler
|
|
* @param routeApi - Route API instance
|
|
* @param languages - Available languages
|
|
* @param ceProps - CE properties
|
|
* @param compilerFinder - Compiler finder instance
|
|
* @param appArgs - Application arguments
|
|
*/
|
|
export async function setupCompilerChangeHandling(
|
|
initialCompilers: CompilerInfo[],
|
|
clientOptionsHandler: ClientOptionsHandler,
|
|
routeApi: RouteAPI,
|
|
languages: Record<LanguageKey, Language>,
|
|
ceProps: PropertyGetter,
|
|
compilerFinder: CompilerFinder,
|
|
appArgs: AppArguments,
|
|
): Promise<void> {
|
|
const rescanCompilerSecs = ceProps('rescanCompilerSecs', 0);
|
|
const rescanEnabled = !!rescanCompilerSecs && !appArgs.prediscovered;
|
|
let prevCompilersHash: string | undefined;
|
|
|
|
/**
|
|
* Handle compiler change events
|
|
* @param compilers - New set of compilers
|
|
*/
|
|
async function onCompilerChange(compilers: CompilerInfo[]) {
|
|
if (rescanEnabled) {
|
|
const compilersHash = getHash(compilers);
|
|
if (prevCompilersHash === compilersHash) {
|
|
return;
|
|
}
|
|
prevCompilersHash = compilersHash;
|
|
}
|
|
logger.info(`Compiler scan count: ${compilers.length}`);
|
|
logger.debug('Compilers:', compilers);
|
|
await clientOptionsHandler.setCompilers(compilers);
|
|
const apiHandler = unwrap(routeApi.apiHandler);
|
|
apiHandler.setCompilers(compilers);
|
|
apiHandler.setLanguages(languages);
|
|
apiHandler.setOptions(clientOptionsHandler);
|
|
}
|
|
|
|
await onCompilerChange(initialCompilers);
|
|
|
|
if (rescanEnabled) {
|
|
logger.info(`Rescanning compilers every ${rescanCompilerSecs} secs`);
|
|
setInterval(
|
|
() => compilerFinder.find().then(result => onCompilerChange(result.compilers)),
|
|
rescanCompilerSecs * 1000,
|
|
);
|
|
}
|
|
}
|