Skip to content

Commit 32d16d3

Browse files
committed
debt - use Map for service collection
1 parent 5819ecc commit 32d16d3

2 files changed

Lines changed: 19 additions & 45 deletions

File tree

src/vs/platform/instantiation/common/instantiation.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import * as descriptors from './descriptors';
1212

1313
export namespace _util {
1414

15+
export const serviceIds = new Map<string, ServiceIdentifier<any>>();
16+
1517
export const DI_TARGET = '$di$target';
1618
export const DI_DEPENDENCIES = '$di$dependencies';
1719

@@ -98,7 +100,7 @@ export interface IFunctionSignature8<A1, A2, A3, A4, A5, A6, A7, A8, R> {
98100
(accessor: ServicesAccessor, first: A1, second: A2, third: A3, forth: A4, fifth: A5, sixth: A6, seventh: A7, eigth: A8): R;
99101
}
100102

101-
export var IInstantiationService = createDecorator<IInstantiationService>('instantiationService');
103+
export const IInstantiationService = createDecorator<IInstantiationService>('instantiationService');
102104

103105
export interface IInstantiationService {
104106

@@ -185,7 +187,11 @@ function storeServiceDependency(id: Function, target: Function, index: number, o
185187
*/
186188
export function createDecorator<T>(serviceId: string): { (...args: any[]): void; type: T; } {
187189

188-
let id = function (target: Function, key: string, index: number): any {
190+
if (_util.serviceIds.has(serviceId)) {
191+
return _util.serviceIds.get(serviceId);
192+
}
193+
194+
const id = <any>function (target: Function, key: string, index: number): any {
189195
if (arguments.length !== 3) {
190196
throw new Error('@IServiceName-decorator can only be used to decorate a parameter');
191197
}
@@ -194,7 +200,8 @@ export function createDecorator<T>(serviceId: string): { (...args: any[]): void;
194200

195201
id.toString = () => serviceId;
196202

197-
return <any>id;
203+
_util.serviceIds.set(serviceId, id);
204+
return id;
198205
}
199206

200207
/**

src/vs/platform/instantiation/common/serviceCollection.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,36 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { binarySearch } from 'vs/base/common/arrays';
87
import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
98
import { SyncDescriptor } from './descriptors';
109

1110
type Entry = [ServiceIdentifier<any>, any];
1211

1312
export class ServiceCollection {
1413

15-
private _entries: Entry[] = [];
14+
private _entries = new Map<ServiceIdentifier<any>, any>();
1615

1716
constructor(...entries: [ServiceIdentifier<any>, any][]) {
18-
for (let entry of entries) {
19-
this.set(entry[0], entry[1]);
17+
for (let [id, service] of entries) {
18+
this.set(id, service);
2019
}
2120
}
2221

2322
set<T>(id: ServiceIdentifier<T>, instanceOrDescriptor: T | SyncDescriptor<T>): T | SyncDescriptor<T> {
24-
const entry: Entry = [id, instanceOrDescriptor];
25-
const idx = binarySearch(this._entries, entry, ServiceCollection._entryCompare);
26-
if (idx < 0) {
27-
// new element
28-
this._entries.splice(~idx, 0, entry);
29-
} else {
30-
const old = this._entries[idx];
31-
this._entries[idx] = entry;
32-
return old[1];
33-
}
23+
const result = this._entries.get(id);
24+
this._entries.set(id, instanceOrDescriptor);
25+
return result;
3426
}
3527

3628
forEach(callback: (id: ServiceIdentifier<any>, instanceOrDescriptor: any) => any): void {
37-
for (let entry of this._entries) {
38-
let [id, instanceOrDescriptor] = entry;
39-
callback(id, instanceOrDescriptor);
40-
}
29+
this._entries.forEach((value, key) => callback(key, value));
4130
}
4231

4332
has(id: ServiceIdentifier<any>): boolean {
44-
return binarySearch(this._entries, ServiceCollection._searchEntry(id), ServiceCollection._entryCompare) >= 0;
33+
return this._entries.has(id);
4534
}
4635

4736
get<T>(id: ServiceIdentifier<T>): T | SyncDescriptor<T> {
48-
const idx = binarySearch(this._entries, ServiceCollection._searchEntry(id), ServiceCollection._entryCompare);
49-
if (idx >= 0) {
50-
return this._entries[idx][1];
51-
}
52-
}
53-
54-
private static _dummy: Entry = [undefined, undefined];
55-
56-
private static _searchEntry(id: ServiceIdentifier<any>): Entry {
57-
ServiceCollection._dummy[0] = id;
58-
return ServiceCollection._dummy;
59-
}
60-
61-
private static _entryCompare(a: Entry, b: Entry): number {
62-
const _a = a[0].toString();
63-
const _b = b[0].toString();
64-
if (_a < _b) {
65-
return -1;
66-
} else if (_a > _b) {
67-
return 1;
68-
} else {
69-
return 0;
70-
}
37+
return this._entries.get(id);
7138
}
7239
}

0 commit comments

Comments
 (0)