@@ -5,6 +5,9 @@ import {parserLogger, tag} from "../Logger";
55import { IndexerWorkerPool } from "./IndexerWorkerPool" ;
66import { type SourceFile } from "@webdoc/types" ;
77import { type Symbol } from "../types/Symbol" ;
8+ import _ from "lodash" ;
9+ import fs from "fs" ;
10+ import os from "os" ;
811import path from "path" ;
912
1013declare var globalThis: any ;
@@ -27,37 +30,52 @@ export function register(lang: LanguageIntegration): void {
2730
2831export 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