src/vs/editor/common/services/languageFeatureDebounce.ts
165 LOC · 80 covered · 85 uncovered · 14 ranges · 1341 concepts · 1 introducers · 662 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
testThemeService.ts ×18
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { doHash } from '../../../base/common/hash.js';
import { LRUCache } from '../../../base/common/map.js';
import { clamp, MovingAverage, SlidingWindowAverage } from '../../../base/common/numbers.js';
import { LanguageFeatureRegistry } from '../languageFeatureRegistry.js';
import { ITextModel } from '../model.js';
import { IEnvironmentService } from '../../../platform/environment/common/environment.js';
import { InstantiationType, registerSingleton } from '../../../platform/instantiation/common/extensions.js';
import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
import { ILogService } from '../../../platform/log/common/log.js';
import { matchesScheme } from '../../../base/common/network.js';
export const ILanguageFeatureDebounceService = createDecorator<ILanguageFeatureDebounceService>('ILanguageFeatureDebounceService');
export interface ILanguageFeatureDebounceService {
readonly _serviceBrand: undefined;
for(feature: LanguageFeatureRegistry<object>, debugName: string, config?: { min?: number; max?: number; salt?: string }): IFeatureDebounceInformation;
}
export interface IFeatureDebounceInformation {
get(model: ITextModel): number;
update(model: ITextModel, value: number): number;
default(): number;
}
namespace IdentityHash {
const _hashes = new WeakMap<object, number>();
let pool = 0;
export function of(obj: object): number {
let value = _hashes.get(obj);
if (value === undefined) {
value = ++pool;
_hashes.set(obj, value);
}
return value;
}
class NullDebounceInformation implements IFeatureDebounceInformation {
constructor(private readonly _default: number) { }
get(_model: ITextModel): number {
return this._default;
}
return this._default;
}
return this._default;
}
class FeatureDebounceInformation implements IFeatureDebounceInformation {
private readonly _cache = new LRUCache<string, SlidingWindowAverage>(50, 0.7);
constructor(
private readonly _logService: ILogService,
private readonly _name: string,
private readonly _registry: LanguageFeatureRegistry<object>,
private readonly _default: number,
private readonly _min: number,
private readonly _max: number,
) { }
private _key(model: ITextModel): string {
return model.id + this._registry.all(model).reduce((hashVal, obj) => doHash(IdentityHash.of(obj), hashVal), 0);
}
get(model: ITextModel): number {
const key = this._key(model);
const avg = this._cache.get(key);
return avg
? clamp(avg.value, this._min, this._max)
: this.default();
}
update(model: ITextModel, value: number): number {
const key = this._key(model);
let avg = this._cache.get(key);
if (!avg) {
avg = new SlidingWindowAverage(6);
this._cache.set(key, avg);
}
const newValue = clamp(avg.update(value), this._min, this._max);
if (!matchesScheme(model.uri, 'output')) {
this._logService.trace(`[DEBOUNCE: ${this._name}] for ${model.uri.toString()} is ${newValue}ms`);
}
return newValue;
}
private _overall(): number {
const result = new MovingAverage();
for (const [, avg] of this._cache) {
result.update(avg.value);
}
return result.value;
}
default() {
const value = (this._overall() | 0) || this._default;
return clamp(value, this._min, this._max);
}
export class LanguageFeatureDebounceService implements ILanguageFeatureDebounceService {
declare _serviceBrand: undefined;
private readonly _data = new Map<string, IFeatureDebounceInformation>();
private readonly _isDev: boolean;
constructor(
@ILogService private readonly _logService: ILogService,
@IEnvironmentService envService: IEnvironmentService,
) {
this._isDev = envService.isExtensionDevelopment || !envService.isBuilt;
}
for(feature: LanguageFeatureRegistry<object>, name: string, config?: { min?: number; max?: number; key?: string }): IFeatureDebounceInformation {
const min = config?.min ?? 50;
const max = config?.max ?? min ** 2;
const extra = config?.key ?? undefined;
const key = `${IdentityHash.of(feature)},${min}${extra ? ',' + extra : ''}`;
let info = this._data.get(key);
if (!info) {
if (this._isDev) {
this._logService.debug(`[DEBOUNCE: ${name}] is disabled in developed mode`);
info = new NullDebounceInformation(min * 1.5);
} else {
info = new FeatureDebounceInformation(
this._logService,
name,
feature,
(this._overallAverage() | 0) || (min * 1.5), // default is overall default or derived from min-value
min,
max
);
}
this._data.set(key, info);
}
return info;
}
private _overallAverage(): number {
// Average of all language features. Not a great value but an approximation
const result = new MovingAverage();
for (const info of this._data.values()) {
result.update(info.default());
}
return result.value;
}
registerSingleton(ILanguageFeatureDebounceService, LanguageFeatureDebounceService, InstantiationType.Delayed);