31186cc8c344f7ebe50f19099d1a43d6fa38936f7319ba744e0502eb03cd99efd0b2483a17e3f03792760158994e73d40f98187b37076fa0d31428fc043c3d 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. import { doHash } from '../../../base/common/hash.js';
  15. import { LRUCache } from '../../../base/common/map.js';
  16. import { clamp, MovingAverage, SlidingWindowAverage } from '../../../base/common/numbers.js';
  17. import { registerSingleton } from '../../../platform/instantiation/common/extensions.js';
  18. import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
  19. import { ILogService } from '../../../platform/log/common/log.js';
  20. import { matchesScheme } from '../../../platform/opener/common/opener.js';
  21. export const ILanguageFeatureDebounceService = createDecorator('ILanguageFeatureDebounceService');
  22. var IdentityHash;
  23. (function (IdentityHash) {
  24. const _hashes = new WeakMap();
  25. let pool = 0;
  26. function of(obj) {
  27. let value = _hashes.get(obj);
  28. if (value === undefined) {
  29. value = ++pool;
  30. _hashes.set(obj, value);
  31. }
  32. return value;
  33. }
  34. IdentityHash.of = of;
  35. })(IdentityHash || (IdentityHash = {}));
  36. class FeatureDebounceInformation {
  37. constructor(_logService, _name, _registry, _default, _min, _max) {
  38. this._logService = _logService;
  39. this._name = _name;
  40. this._registry = _registry;
  41. this._default = _default;
  42. this._min = _min;
  43. this._max = _max;
  44. this._cache = new LRUCache(50, 0.7);
  45. }
  46. _key(model) {
  47. return model.id + this._registry.all(model).reduce((hashVal, obj) => doHash(IdentityHash.of(obj), hashVal), 0);
  48. }
  49. get(model) {
  50. const key = this._key(model);
  51. const avg = this._cache.get(key);
  52. return avg
  53. ? clamp(avg.value, this._min, this._max)
  54. : this.default();
  55. }
  56. update(model, value) {
  57. const key = this._key(model);
  58. let avg = this._cache.get(key);
  59. if (!avg) {
  60. avg = new SlidingWindowAverage(6);
  61. this._cache.set(key, avg);
  62. }
  63. const newValue = clamp(avg.update(value), this._min, this._max);
  64. if (!matchesScheme(model.uri, 'output')) {
  65. this._logService.trace(`[DEBOUNCE: ${this._name}] for ${model.uri.toString()} is ${newValue}ms`);
  66. }
  67. return newValue;
  68. }
  69. _overall() {
  70. const result = new MovingAverage();
  71. for (const [, avg] of this._cache) {
  72. result.update(avg.value);
  73. }
  74. return result.value;
  75. }
  76. default() {
  77. const value = (this._overall() | 0) || this._default;
  78. return clamp(value, this._min, this._max);
  79. }
  80. }
  81. let LanguageFeatureDebounceService = class LanguageFeatureDebounceService {
  82. constructor(_logService) {
  83. this._logService = _logService;
  84. this._data = new Map();
  85. }
  86. for(feature, name, config) {
  87. var _a, _b, _c;
  88. const min = (_a = config === null || config === void 0 ? void 0 : config.min) !== null && _a !== void 0 ? _a : 50;
  89. const max = (_b = config === null || config === void 0 ? void 0 : config.max) !== null && _b !== void 0 ? _b : Math.pow(min, 2);
  90. const extra = (_c = config === null || config === void 0 ? void 0 : config.key) !== null && _c !== void 0 ? _c : undefined;
  91. const key = `${IdentityHash.of(feature)},${min}${extra ? ',' + extra : ''}`;
  92. let info = this._data.get(key);
  93. if (!info) {
  94. info = new FeatureDebounceInformation(this._logService, name, feature, (this._overallAverage() | 0) || (min * 1.5), // default is overall default or derived from min-value
  95. min, max);
  96. this._data.set(key, info);
  97. }
  98. return info;
  99. }
  100. _overallAverage() {
  101. // Average of all language features. Not a great value but an approximation
  102. const result = new MovingAverage();
  103. for (const info of this._data.values()) {
  104. result.update(info.default());
  105. }
  106. return result.value;
  107. }
  108. };
  109. LanguageFeatureDebounceService = __decorate([
  110. __param(0, ILogService)
  111. ], LanguageFeatureDebounceService);
  112. export { LanguageFeatureDebounceService };
  113. registerSingleton(ILanguageFeatureDebounceService, LanguageFeatureDebounceService, true);