debt - no guessing-caching, better use of gc-signals, command converter using gc-signals, main side heap service

This commit is contained in:
Johannes Rieken
2016-10-21 18:53:05 +02:00
parent c57c31abc9
commit fbacd65e2a
13 changed files with 292 additions and 256 deletions

View File

@@ -5,28 +5,119 @@
'use strict';
import { IDisposable } from 'vs/base/common/lifecycle';
import { TPromise } from 'vs/base/common/winjs.base';
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
import { ExtHostContext } from './extHost.protocol';
import { onDidGarbageCollectSignals, consumeSignals } from 'gc-signals';
import { ExtHostContext, ObjectIdentifier } from './extHost.protocol';
import { consumeSignals, GCSignal } from 'gc-signals';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
export class MainThreadHeapService {
private _subscription: IDisposable;
declare class WeakMap<K, V> {
set(key: K, value?: V): WeakMap<K, V>;
}
declare class Set<E> {
add(e: E): this;
has(e: E): boolean;
delete(e: E): boolean;
}
export const IHeapService = createDecorator<IHeapService>('heapService');
export interface IHeapService {
_serviceBrand: any;
/**
* Track gc-collection for all new objects that
* have the $ident-value set.
*/
trackRecursive<T>(p: TPromise<T>): TPromise<T>;
/**
* Track gc-collection for all new objects that
* have the $ident-value set.
*/
trackRecursive<T>(obj: T): T;
}
export class MainThreadHeapService implements IHeapService {
_serviceBrand: any;
private _activeSignals = new WeakMap<any, GCSignal>();
private _activeIds = new Set<number>();
private _consumeHandle: number;
constructor( @IThreadService threadService: IThreadService) {
const proxy = threadService.get(ExtHostContext.ExtHostHeapService);
this._subscription = onDidGarbageCollectSignals(ids => {
proxy.$onGarbageCollection(ids);
});
this._consumeHandle = setInterval(() => {
const ids = consumeSignals();
this._consumeHandle = setInterval(consumeSignals, 15 * 1000);
if (ids.length > 0) {
// local book-keeping
for (const id of ids) {
this._activeIds.delete(id);
}
// send to ext host
proxy.$onGarbageCollection(ids);
}
}, 15 * 1000);
}
dispose() {
clearInterval(this._consumeHandle);
this._subscription.dispose();
}
}
trackRecursive<T>(p: TPromise<T>): TPromise<T>;
trackRecursive<T>(obj: T): T;
trackRecursive<T>(obj: any): any {
if (TPromise.is(obj)) {
return obj.then(result => this.trackRecursive(result));
} else {
return this._doTrackRecursive(obj);
}
}
private _doTrackRecursive(obj: any): any {
const stack = [obj];
while (stack.length > 0) {
// remove first element
let obj = stack.shift();
if (!obj || typeof obj !== 'object') {
continue;
}
for (let key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
continue;
}
const value = obj[key];
// recurse -> object/array
if (typeof value === 'object') {
stack.push(value);
} else if (key === ObjectIdentifier.name) {
// track new $ident-objects
if (typeof value === 'number' && !this._activeIds.has(value)) {
this._activeIds.add(value);
this._activeSignals.set(obj, new GCSignal(value));
}
}
}
}
return obj;
}
}
registerSingleton(IHeapService, MainThreadHeapService);