From bffc0dc3c17f76c295202a4d1a729fe2ec8e982f Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Tue, 23 Oct 2018 17:41:03 +0200 Subject: [PATCH] perf - use IdleValue for Collator creation to save ~25ms when starting without or with an empty explorer. fyi @bpasero --- src/vs/base/common/comparers.ts | 21 ++++++++++----------- src/vs/workbench/electron-browser/main.ts | 11 +++++++++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/vs/base/common/comparers.ts b/src/vs/base/common/comparers.ts index a3f3ca3582f..3a73fa7d4f4 100644 --- a/src/vs/base/common/comparers.ts +++ b/src/vs/base/common/comparers.ts @@ -5,24 +5,23 @@ import * as strings from 'vs/base/common/strings'; import * as paths from 'vs/base/common/paths'; +import { IdleValue } from 'vs/base/common/async'; -let intlFileNameCollator: Intl.Collator; -let intlFileNameCollatorIsNumeric: boolean; +let intlFileNameCollator: IdleValue<{ collator: Intl.Collator, collatorIsNumeric: boolean }>; -export function setFileNameComparer(collator: Intl.Collator): void { +export function setFileNameComparer(collator: IdleValue<{ collator: Intl.Collator, collatorIsNumeric: boolean }>): void { intlFileNameCollator = collator; - intlFileNameCollatorIsNumeric = collator.resolvedOptions().numeric; } export function compareFileNames(one: string, other: string, caseSensitive = false): number { if (intlFileNameCollator) { const a = one || ''; const b = other || ''; - const result = intlFileNameCollator.compare(a, b); + const result = intlFileNameCollator.getValue().collator.compare(a, b); // Using the numeric option in the collator will // make compare(`foo1`, `foo01`) === 0. We must disambiguate. - if (intlFileNameCollatorIsNumeric && result === 0 && a !== b) { + if (intlFileNameCollator.getValue().collatorIsNumeric && result === 0 && a !== b) { return a < b ? -1 : 1; } @@ -59,19 +58,19 @@ export function compareFileExtensions(one: string, other: string): number { const [oneName, oneExtension] = extractNameAndExtension(one); const [otherName, otherExtension] = extractNameAndExtension(other); - let result = intlFileNameCollator.compare(oneExtension, otherExtension); + let result = intlFileNameCollator.getValue().collator.compare(oneExtension, otherExtension); if (result === 0) { // Using the numeric option in the collator will // make compare(`foo1`, `foo01`) === 0. We must disambiguate. - if (intlFileNameCollatorIsNumeric && oneExtension !== otherExtension) { + if (intlFileNameCollator.getValue().collatorIsNumeric && oneExtension !== otherExtension) { return oneExtension < otherExtension ? -1 : 1; } // Extensions are equal, compare filenames - result = intlFileNameCollator.compare(oneName, otherName); + result = intlFileNameCollator.getValue().collator.compare(oneName, otherName); - if (intlFileNameCollatorIsNumeric && result === 0 && oneName !== otherName) { + if (intlFileNameCollator.getValue().collatorIsNumeric && result === 0 && oneName !== otherName) { return oneName < otherName ? -1 : 1; } } @@ -194,4 +193,4 @@ export function compareByPrefix(one: string, other: string, lookFor: string): nu } return 0; -} \ No newline at end of file +} diff --git a/src/vs/workbench/electron-browser/main.ts b/src/vs/workbench/electron-browser/main.ts index dd96e4ae00d..d1e5d78f711 100644 --- a/src/vs/workbench/electron-browser/main.ts +++ b/src/vs/workbench/electron-browser/main.ts @@ -49,6 +49,7 @@ import { createHash } from 'crypto'; import { parseStorage, StorageObject } from 'vs/platform/storage/common/storageLegacyMigration'; import { StorageScope } from 'vs/platform/storage/common/storage'; import { endsWith } from 'vs/base/common/strings'; +import { IdleValue } from 'vs/base/common/async'; gracefulFs.gracefulify(fs); // enable gracefulFs @@ -70,7 +71,13 @@ export function startup(configuration: IWindowConfiguration): Promise { KeyboardMapperFactory.INSTANCE._onKeyboardLayoutChanged(); // Setup Intl for comparers - comparer.setFileNameComparer(new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' })); + comparer.setFileNameComparer(new IdleValue(() => { + const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }); + return { + collator: collator, + collatorIsNumeric: collator.resolvedOptions().numeric + }; + })); // Open workbench return openWorkbench(configuration); @@ -463,4 +470,4 @@ function createMainProcessServices(mainProcessClient: ElectronIPCClient, configu serviceCollection.set(IWorkspacesService, new WorkspacesChannelClient(workspacesChannel)); return serviceCollection; -} \ No newline at end of file +} -- GitLab