Skip to content

Commit 0b21fbd

Browse files
committed
Run indexer on main thread for smaller projects. Repair symbol loc.file.package when copied from worker.
1 parent f467b5c commit 0b21fbd

File tree

3 files changed

+67
-27
lines changed

3 files changed

+67
-27
lines changed

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"build-next": "cd .. && webdoc && cd example",
2828
"build-pixi-api": "cd ../../pixi-api && webdoc --verbose && cd ../webdoc/example",
2929
"build-pixi-api-prod": "cd ../../pixi-api && webdoc --site-root pixi-api && cd ../webdoc/example",
30-
"build-pixi-api-gcp": "cd ../../pixi-api && webdoc --tutorials ./projects/guides --verbose && cd ../webdoc/example",
30+
"build-pixi-api-gcp": "cd ../../pixi-api && node --inspect-brk ../webdoc/example/node_modules/@webdoc/cli/cli --tutorials ./projects/guides --verbose && cd ../webdoc/example",
3131
"build-pixi-guides": "cd ../../guides && webdoc --tutorials docs && cd ../webdoc/example",
3232
"build-mongodb": "cd ../../node-mongodb-native && webdoc --verbose && cd ../webdoc/example"
3333
},

packages/webdoc-parser/src/indexer/IndexerWorkerPool.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @flow
22

3+
import {type PackageDoc, type SourceFile} from "@webdoc/types";
34
import {parserLogger, tag} from "../Logger";
45
import EventEmitter from "events";
56
import type {LanguageConfig} from "../types/LanguageIntegration";
6-
import {type SourceFile} from "@webdoc/types";
77
import {type Symbol} from "../types/Symbol";
88
import {Worker} from "worker_threads";
99
import os from "os";
@@ -81,15 +81,37 @@ export class IndexerWorkerPool {
8181
this.workers = workers;
8282
}
8383

84-
index(langIntegrationModule: string, file: SourceFile, config: LanguageConfig): Promise<Symbol> {
84+
repair(symbol: Symbol, packages: { [id: string]: PackageDoc }): Symbol {
85+
const file = symbol.loc.file;
86+
const pkgId = file?.package.id;
87+
88+
if (file && pkgId && packages[pkgId]) {
89+
file.package = packages[pkgId];
90+
}
91+
for (const child of symbol.members) {
92+
this.repair(child, packages);
93+
}
94+
95+
return symbol;
96+
}
97+
98+
index(
99+
langIntegrationModule: string,
100+
file: SourceFile,
101+
config: LanguageConfig,
102+
packages: { [id: string]: PackageDoc },
103+
): Promise<Symbol> {
85104
return new Promise<Symbol>((resolve, reject) => {
86105
const jobId = this.ptr++;
87106
const worker = this.workers[jobId % this.workers.length];
88107

89108
worker.send(jobId, langIntegrationModule, file, config);
90109
worker.on(`response-${jobId}`, (jobResult: Symbol, jobError: any): void => {
91-
if (jobResult) resolve(jobResult);
92-
else reject(jobError);
110+
if (jobResult) {
111+
resolve(this.repair(jobResult, packages));
112+
} else {
113+
reject(jobError);
114+
}
93115
});
94116
});
95117
}

packages/webdoc-parser/src/indexer/index.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {parserLogger, tag} from "../Logger";
55
import {IndexerWorkerPool} from "./IndexerWorkerPool";
66
import {type SourceFile} from "@webdoc/types";
77
import {type Symbol} from "../types/Symbol";
8+
import _ from "lodash";
9+
import fs from "fs";
10+
import os from "os";
811
import path from "path";
912

1013
declare var globalThis: any;
@@ -27,37 +30,52 @@ export function register(lang: LanguageIntegration): void {
2730

2831
export async function run(files: SourceFile[], config: LanguageConfig): Promise<Symbol[]> {
2932
const startTime = Date.now();
30-
const symbolTrees: Array<Symbol> = new Array(files.length);
31-
const symbolIndexingOperations: Array<Promise<void>> = new Array(files.length);
32-
const workerPool = new IndexerWorkerPool(Math.floor(files.length / 125));
33+
const maxThreads = Math.min(os.cpus().length, 1 + Math.floor(files.length / 125));
3334

3435
parserLogger.info(tag.Indexer, "Indexing " + files.length + " files");
3536

36-
for (let i = 0; i < files.length; i++) {
37-
const fileName = files[i].path;
38-
const extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length);
37+
const symbolTrees: Array<Symbol> = new Array(files.length);
3938

40-
if (!(extension in languages)) {
41-
throw new Error(`.${extension} language is not registered with the Indexer!`);
39+
if (maxThreads > 1) {
40+
const packages = _.keyBy(
41+
files.map((file) => file.package),
42+
(pkg) => pkg.id,
43+
);
44+
const symbolIndexingOperations: Array<Promise<void>> = new Array(files.length);
45+
const workerPool = new IndexerWorkerPool(maxThreads);
46+
47+
for (let i = 0; i < files.length; i++) {
48+
const fileName = files[i].path;
49+
const extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length);
50+
51+
if (!(extension in languages)) {
52+
throw new Error(`.${extension} language is not registered with the Indexer!`);
53+
}
54+
55+
const file = {
56+
...files[i],
57+
path: path.resolve(globalThis.process.cwd(), files[i].path),
58+
};
59+
const lang = languages[extension].module;
60+
61+
symbolIndexingOperations[i] = workerPool.index(lang, file, config, packages).then(
62+
(symbolTree: Symbol): void => {
63+
symbolTrees[i] = symbolTree;
64+
},
65+
);
4266
}
4367

44-
const file = {
45-
...files[i],
46-
path: path.resolve(globalThis.process.cwd(), files[i].path),
47-
};
48-
const lang = languages[extension].module;
68+
await Promise.all(symbolIndexingOperations);
69+
workerPool.destroy();
70+
} else {
71+
for (let i = 0; i < files.length; i++) {
72+
const filePath = path.resolve(globalThis.process.cwd(), files[i].path);
73+
const fileContent = files[i].content || fs.readFileSync(filePath, "utf8");
4974

50-
symbolIndexingOperations[i] = workerPool.index(lang, file, config).then(
51-
(symbolTree: Symbol): void => {
52-
symbolTrees[i] = symbolTree;
53-
},
54-
);
75+
symbolTrees[i] = process(fileContent, files[i], config);
76+
}
5577
}
5678

57-
await Promise.all(symbolIndexingOperations);
58-
59-
workerPool.destroy();
60-
6179
const endTime = Date.now();
6280

6381
parserLogger.info(tag.Indexer, "Indexing took " + (endTime - startTime) + "ms");

0 commit comments

Comments
 (0)