Skip to content

Commit

Permalink
vscode: use "sliding window" average for language feature delays, mic…
Browse files Browse the repository at this point in the history
…rosoft/vscode#140557

Commit: c79dcf8c5c162168562d491c520bee585c1bdf01
  • Loading branch information
Johannes Rieken authored and sourcegraph-bot committed Jan 12, 2022
1 parent 0012c8e commit 8c50215
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
34 changes: 34 additions & 0 deletions vscode/src/vs/base/common/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,37 @@ export class MovingAverage {
return this._val;
}
}

export class SlidingWindowAverage {

private _n: number = 0;
private _val = 0;

private readonly _values: number[] = [];
private _index: number = 0;
private _sum = 0;

constructor(size: number) {
this._values = new Array(size);
this._values.fill(0, 0, size);
}

update(value: number) {
const oldValue = this._values[this._index];
this._values[this._index] = value;
this._index = (this._index + 1) % this._values.length;

this._sum -= oldValue;
this._sum += value;

if (this._n < this._values.length) {
this._n += 1;
}

this._val = this._sum / this._n;
}

get value(): number {
return this._val;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Emitter, Event } from 'vs/base/common/event';
import { doHash } from 'vs/base/common/hash';
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { LRUCache } from 'vs/base/common/map';
import { MovingAverage } from 'vs/base/common/numbers';
import { SlidingWindowAverage } from 'vs/base/common/numbers';
import { ITextModel } from 'vs/editor/common/model';
import { LanguageFilter, LanguageSelector, score } from 'vs/editor/common/languages/languageSelector';
import { shouldSynchronizeModel } from 'vs/editor/common/services/model';
Expand Down Expand Up @@ -197,7 +197,7 @@ function weakHash(obj: object): number {
*/
export class LanguageFeatureRequestDelays {

private readonly _cache = new LRUCache<string, MovingAverage>(50, 0.7);
private readonly _cache = new LRUCache<string, SlidingWindowAverage>(50, 0.7);


constructor(
Expand Down Expand Up @@ -228,7 +228,7 @@ export class LanguageFeatureRequestDelays {
const key = this._key(model);
let avg = this._cache.get(key);
if (!avg) {
avg = new MovingAverage();
avg = new SlidingWindowAverage(12);
this._cache.set(key, avg);
}
avg.update(value);
Expand Down

0 comments on commit 8c50215

Please sign in to comment.