Skip to content

Commit

Permalink
make MarkerService#read a litte faster, #11976
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Sep 19, 2016
1 parent 7920eda commit 0c4759a
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/vs/platform/markers/common/markerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import arrays = require('vs/base/common/arrays');
import network = require('vs/base/common/network');
import strings = require('vs/base/common/strings');
import collections = require('vs/base/common/collections');
import URI from 'vs/base/common/uri';
import Event, {Emitter, debounceEvent} from 'vs/base/common/event';
Expand All @@ -18,24 +17,36 @@ interface Key {
resource: URI;
}

module Key {
namespace Key {

export function fromValue(value: string): Key {
let regexp = /^(.*)(.*)$/.exec(value);
return {
owner: regexp[1],
resource: URI.parse(regexp[2])
};
const idx = value.indexOf('→');
const owner = value.substring(0, idx);
const resource = URI.parse(value.substring(idx + 1));
return { owner, resource };
}

export function valueOf(k: Key): string {
return k.owner + '→' + k.resource;
}
export function selector(owner?: string, resource?: URI): (input: string)=> boolean {

if (!owner && !resource) {
// anything
return input => true;

let _selectorPattern = '^({0})→({1})$';
} else if (!owner) {
// ends with
const suffix = '→' + resource.toString();
return input => input.lastIndexOf(suffix) === input.length - suffix.length;

export function selector(owner?: string, resource?: URI): RegExp {
return new RegExp(strings.format(_selectorPattern, owner ? strings.escapeRegExpCharacters(owner) : '.*', resource ? strings.escapeRegExpCharacters(resource.toString()) : '.*'));
} else if (!resource) {
// starts with
const prefix = owner + `→`;
return input => input.indexOf(prefix) === 0;

} else {
// exact match
const match = owner + '→' + resource.toString();
return input => input === match;
}
}

export function raw(owner: string, resource: URI): string {
Expand Down Expand Up @@ -129,7 +140,7 @@ export class MarkerService implements IMarkerService {

// remove and record old markers
let oldStats = this._emptyStats();
this._forEach(owner, undefined, undefined, -1, (e, r) => {
this._forEach(owner, undefined, -1, (e, r) => {
let resource = Key.fromValue(e.key).resource;
if (this._isStatRelevant(resource)) {
this._updateStatsPlus(oldStats, this._computeStats(e.value));
Expand All @@ -154,9 +165,9 @@ export class MarkerService implements IMarkerService {
this._onMarkerChanged.fire(collections.values(changedResources));
}

public read(filter: { owner?: string; resource?: URI; selector?: RegExp, take?: number; } = Object.create(null)): IMarker[] {
public read(filter: { owner?: string; resource?: URI; take?: number; } = Object.create(null)): IMarker[] {
let ret: IMarker[] = [];
this._forEach(filter.owner, filter.resource, filter.selector, filter.take, entry => this._fromEntry(entry, ret));
this._forEach(filter.owner, filter.resource, filter.take, entry => this._fromEntry(entry, ret));
return ret;
}

Expand All @@ -165,19 +176,19 @@ export class MarkerService implements IMarkerService {
return resource.scheme !== network.Schemas.inMemory;
}

private _forEach(owner: string, resource: URI, regexp: RegExp, take: number, callback: (entry: { key: string; value: IMarkerData[]; }, remove: Function) => any): void {
private _forEach(owner: string, resource: URI, take: number, callback: (entry: { key: string; value: IMarkerData[]; }, remove: Function) => any): void {
//TODO@Joh: be smart and use an index
let selector = regexp || Key.selector(owner, resource),
took = 0;
const selector = Key.selector(owner, resource);

collections.forEach(this._data, (entry, remove) => {
if (selector.test(entry.key)) {
callback(entry, remove);
let took = 0;
for (let key in this._data) {
if (selector(key)) {
callback({ key, value: this._data[key] }, () => delete this._data[key]);
if (take > 0 && took++ >= take) {
return false;
break;
}
}
});
}
}

private _fromEntry(entry: { key: string; value: IMarkerData[]; }, bucket: IMarker[]): void {
Expand Down

0 comments on commit 0c4759a

Please sign in to comment.