Skip to content

Commit 1c77062

Browse files
committed
Add back indexing to fix performance issues
1 parent b4d812a commit 1c77062

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"url": "https://github.com/PhilippeChab/nwscript-ee-language-server"
99
},
1010
"license": "MIT",
11-
"version": "2.0.1",
11+
"version": "2.0.2",
1212
"author": {
1313
"name": "Philippe Chabot"
1414
},
@@ -161,7 +161,7 @@
161161
"vscode:prepublish": "yarn build",
162162
"build": "yarn clean && yarn buildClient && yarn buildServer",
163163
"buildClient": "esbuild extension=./client/src/extension.ts --sourcemap --bundle --external:vscode --platform=node --outdir=./client/out/",
164-
"buildServer": "esbuild server=./server/src/server.ts --sourcemap --bundle --external:vscode --platform=node --outdir=./server/out/",
164+
"buildServer": "esbuild server=./server/src/server.ts indexer=./server/src/Documents/DocumentsIndexer.ts --sourcemap --bundle --external:vscode --platform=node --outdir=./server/out/",
165165
"compile": "tsc -b",
166166
"compile:client": "tsc -b ./client/tsconfig.json",
167167
"compile:server": "tsc -b ./server/tsconfig.json",

server/src/Documents/DocumentsCollection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export default class DocumentsCollection extends Dictionnary<string, Document> {
5353
if (!filePath) return;
5454

5555
const uri = pathToFileURL(filePath).href;
56+
if (this.get(this.getKey(uri, false))) return;
57+
5658
const fileContent = readFileSync(filePath).toString();
5759
this.createDocuments(uri, fileContent, tokenizer, workespaceFilesSystem);
5860
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { readFileSync } from "fs";
2+
import { exit } from "process";
3+
4+
import { Tokenizer } from "../Tokenizer";
5+
import { TokenizedScope } from "../Tokenizer/Tokenizer";
6+
7+
const generateTokens = async (filesPath: string[]) => {
8+
if (filesPath.length === 1 && !Boolean(filesPath[0])) {
9+
exit(0);
10+
}
11+
12+
const tokenizer = await new Tokenizer().loadGrammar();
13+
for (let i = 0; i < filesPath.length; i++) {
14+
const filePath = filesPath[i];
15+
const fileContent = readFileSync(filePath).toString();
16+
const globalScope = tokenizer.tokenizeContent(fileContent, TokenizedScope.global);
17+
18+
process?.send!(JSON.stringify({ filePath, globalScope }));
19+
}
20+
21+
exit(0);
22+
};
23+
24+
process.on("message", (filesPath: string) => {
25+
generateTokens(filesPath.split(","));
26+
});

server/src/ServerManager/ServerManager.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { cpus } from "os";
2+
import { join } from "path";
3+
import { pathToFileURL } from "url";
4+
import * as clustering from "cluster";
15
import type { Connection, InitializeParams } from "vscode-languageserver";
26

37
import {
@@ -67,8 +71,42 @@ export default class ServerManger {
6771
}
6872

6973
await this.loadConfig();
70-
this.configLoaded = true;
71-
this.diagnosticsProvider?.processDocumentsWaitingForPublish();
74+
75+
const numCPUs = cpus().length;
76+
const cluster = clustering.default;
77+
if (cluster.isPrimary) {
78+
cluster.setupPrimary({
79+
exec: join(__dirname, "indexer.js"),
80+
});
81+
}
82+
83+
let filesIndexedCount = 0;
84+
const filesPath = this.workspaceFilesSystem.getAllFilesPath();
85+
const progressReporter = await this.connection.window.createWorkDoneProgress();
86+
const filesCount = filesPath.length;
87+
this.logger.info(`Indexing files ...`);
88+
89+
progressReporter.begin("Indexing files for NWScript: EE Language Server ...", 0);
90+
const partCount = Math.ceil(filesCount / numCPUs);
91+
for (let i = 0; i < Math.min(numCPUs, filesCount); i++) {
92+
const worker = cluster.fork();
93+
worker.send(filesPath.slice(i * partCount, Math.min((i + 1) * partCount, filesCount - 1)).join(","));
94+
worker.on("message", (message: string) => {
95+
const { filePath, globalScope } = JSON.parse(message);
96+
this.documentsCollection?.createDocument(pathToFileURL(filePath).href, globalScope);
97+
filesIndexedCount++;
98+
progressReporter?.report(filesIndexedCount / filesCount);
99+
});
100+
}
101+
102+
cluster.on("exit", () => {
103+
if (Object.keys(cluster.workers || {}).length === 0) {
104+
progressReporter?.done();
105+
this.logger.info(`Indexed ${filesIndexedCount} files.`);
106+
this.configLoaded = true;
107+
this.diagnosticsProvider?.processDocumentsWaitingForPublish();
108+
}
109+
});
72110
}
73111

74112
public down() {}

server/src/WorkspaceFilesSystem/WorkspaceFilesSystem.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export default class WorkspaceFilesSystem {
1111
return normalize(join(this.rootPath, ...parts));
1212
}
1313

14+
public getAllFilesPath() {
15+
return new GlobSync(`**/*${FILES_EXTENSION}`).found.map((filename) => this.normalizedAbsolutePath(filename));
16+
}
17+
1418
public getFilePath(filename: string) {
1519
const path = new GlobSync(`**/${filename}${FILES_EXTENSION}`).found[0];
1620
if (path) {

0 commit comments

Comments
 (0)