Skip to content

Commit e574663

Browse files
committed
perf:✨ add remappings cache
1 parent c25e776 commit e574663

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/helpers.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ function newWindowBeside(content) {
2424
);
2525
}
2626

27+
let remappings = null; // Cache remappings globally
28+
29+
async function loadRemappings(rootPath) {
30+
const remappingsPath = path.join(rootPath, 'remappings.txt');
31+
try {
32+
const remappingsContent = await vscode.workspace.fs.readFile(vscode.Uri.file(remappingsPath));
33+
return remappingsContent.toString().split('\n').reduce((acc, line) => {
34+
const [key, val] = line.split('=');
35+
if (key && val) {
36+
acc[key.trim()] = val.trim();
37+
}
38+
return acc;
39+
}, {});
40+
} catch (error) {
41+
console.error('Error reading remappings:', error);
42+
return {};
43+
}
44+
}
45+
2746

2847
async function provideCompletionItems(document, position) {
2948
const linePrefix = document.lineAt(position).text.substring(0, position.character);
@@ -36,6 +55,8 @@ async function provideCompletionItems(document, position) {
3655
if (!workspaceFolders) return undefined; // No workspace folder open
3756

3857
const rootPath = workspaceFolders[0].uri.fsPath;
58+
59+
3960
const remappingsPath = path.join(rootPath, 'remappings.txt');
4061
let remappings = {};
4162

@@ -47,6 +68,11 @@ async function provideCompletionItems(document, position) {
4768
});
4869
}
4970

71+
72+
if (remappings === null) { // Load remappings if not already loaded
73+
remappings = await loadRemappings(rootPath);
74+
}
75+
5076
// Step 2: Apply remappings to import paths
5177
const solidityFiles = await findSolidityFiles();
5278
const currentFilePath = document.uri.fsPath;
@@ -65,6 +91,8 @@ async function provideCompletionItems(document, position) {
6591
}
6692
});
6793

94+
95+
6896
// Determine if the path was remapped
6997
const isRemapped = remappedPath !== filePath;
7098

@@ -85,8 +113,8 @@ async function provideCompletionItems(document, position) {
85113
function findSolidityFiles() {
86114
// Define the glob pattern for Solidity files
87115
const solidityFilePattern = '**/*.sol';
88-
// Exclude files from the node_modules folder
89-
const excludePattern = '**/node_modules/**';
116+
// Exclude files from the node_modules and out dirs
117+
const excludePattern = '{**/node_modules/**,**/out/**}';
90118

91119
// Use findFiles to search for files matching the Solidity pattern, excluding undesired paths
92120
return vscode.workspace.findFiles(solidityFilePattern, excludePattern)
@@ -193,3 +221,10 @@ const networkMap = {
193221

194222

195223
module.exports = { newWindowBeside, getContractRootDir, LANGID, networkMap, provideCompletionItems, findSolidityFiles };
224+
225+
// Execution time for optimized: 22.145625114440918 milliseconds
226+
// Execution time for optimized: 11.452915668487549 milliseconds
227+
// Execution time for optimized: 8.146999835968018 milliseconds
228+
// Execution time for optimized: 14.010457992553711 milliseconds
229+
// Execution time for optimized: 8.457749843597412 milliseconds
230+
// Execution time for optimized: 12.96274995803833 milliseconds

0 commit comments

Comments
 (0)