Skip to content

Commit

Permalink
feat: implement caching for configuration retrieval to improve perfor…
Browse files Browse the repository at this point in the history
…mance
  • Loading branch information
maxisam committed Nov 7, 2024
1 parent b2bba54 commit c34ec05
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/DirectoryOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class DirectoryOperator {

private async saveBookmarksAsync() {
// in case users running 2 instances of the vscode.
const config = await getConfigurationAsync(this.configDirUri);
const config = await getConfigurationAsync(this.configDirUri, true);
config.bookmarkedDirectories = this.bookmarkedDirectories;
await updateConfigurationAsync(config, this.configDirUri);
}
Expand Down
14 changes: 12 additions & 2 deletions src/utils/configUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CONFIG_FILE_NAME, DEFAULT_WS_SETTING_PATH } from '../core/constants';
import type { ExcludeObject, IConfiguration } from '../types/Configuration';
import { defaultConfiguration } from '../types/Configuration';

const myFileConfigs = new Map<string, IConfiguration>();
export async function updateConfigurationAsync(config: IConfiguration, configDirUri: vscode.Uri) {
try {
await vscode.workspace.fs.stat(configDirUri);
Expand All @@ -23,7 +24,13 @@ export function getConfigurationDirUri(
return vscode.Uri.joinPath(workspaceRootPath, '.vscode');
}

export async function getConfigurationAsync(configDirUri: vscode.Uri): Promise<IConfiguration> {
export async function getConfigurationAsync(
configDirUri: vscode.Uri,
isNoCache = false,
): Promise<IConfiguration> {
if (myFileConfigs.has(configDirUri.path) && !isNoCache) {
return myFileConfigs.get(configDirUri.path)!;
}
const configPath = vscode.Uri.joinPath(configDirUri, CONFIG_FILE_NAME);
try {
const configData = await vscode.workspace.fs.readFile(configPath);
Expand All @@ -32,7 +39,10 @@ export async function getConfigurationAsync(configDirUri: vscode.Uri): Promise<I
origConfig.privateSettingsPath = origConfig.privateSettingsPath?.trim()
? origConfig.privateSettingsPath
: DEFAULT_WS_SETTING_PATH;
return { ...defaultConfiguration, ...origConfig };
const newconfig = { ...defaultConfiguration, ...origConfig };
// cache the config
myFileConfigs.set(configPath.path, newconfig);
return newconfig;
} catch {
return defaultConfiguration;
}
Expand Down

0 comments on commit c34ec05

Please sign in to comment.