Skip to content

Commit 087d63c

Browse files
committed
fix: remove string interpolation
Instead of using string interpolation, which is always executed, use `debug`'s support for argument formatting, which only evaluates arguments when debugging is enabled. This speeds up FileIndex.indexFile considerably for files with many keywords.
1 parent c70587d commit 087d63c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

packages/cli/src/fulltext/FileIndex.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ import listGitProjectFiles from './listGitProjectFIles';
1313
import querySymbols from './querySymbols';
1414
import { fileNameMatchesFilterPatterns } from './fileNameMatchesFilterPatterns';
1515

16+
makeDebug.formatters.a = (v) => {
17+
return v.join(' ');
18+
};
19+
1620
const debug = makeDebug('appmap:file-index');
1721

22+
// enable to see all indexing terms for each file
23+
const debugTerms = makeDebug('appmap:file-index:terms');
24+
1825
export type FileIndexMatch = {
1926
directory: string;
2027
fileName: string;
@@ -49,7 +56,7 @@ export class FileIndex {
4956
const rows = this.database.prepare(query).all(searchExpr, limit);
5057
if (debug.enabled)
5158
for (const row of rows) {
52-
debug(`Found row ${(row as { file_name: string }).file_name}`);
59+
debug('Found row %s', (row as { file_name: string }).file_name);
5360
}
5461
return rows.map((row: any) => ({
5562
directory: row.directory,
@@ -88,7 +95,8 @@ export class FileIndex {
8895
if (options.allowSymbols) {
8996
debug('Symbol parsing is enabled.');
9097
debug(
91-
`Generic symbol parsing is ${options.allowGenericParsing ? 'enabled.' : 'disabled.'}`
98+
'Generic symbol parsing is %s',
99+
options.allowGenericParsing ? 'enabled.' : 'disabled.'
92100
);
93101
} else {
94102
debug('Symbol parsing is disabled.');
@@ -135,16 +143,17 @@ export class FileIndex {
135143
const fileNameTokens = filePath.split(path.sep);
136144

137145
try {
138-
let terms = queryKeywords(fileNameTokens).join(' ');
146+
let terms = queryKeywords(fileNameTokens);
139147

140148
if (allowSymbols) {
141149
const symbols = querySymbols(path.join(directory, filePath), allowGenericParsing);
142-
terms += ` ${queryKeywords(symbols).sort().join(' ')}`;
150+
terms = terms.concat(queryKeywords(symbols).sort());
143151
}
144152

145-
debug(`Indexing file path ${filePath} with terms ${terms}`);
153+
debug('Indexing file path %s with %d terms', filePath, terms.length);
154+
debugTerms('Terms for path %s: %a', filePath, terms);
146155

147-
this.#insert.run(directory, filePath, terms);
156+
this.#insert.run(directory, filePath, terms.join(' '));
148157
} catch (error) {
149158
console.warn(`Error indexing document ${filePath}`);
150159
console.warn(error);
@@ -273,10 +282,10 @@ export async function filterFiles(
273282
appendFile = true;
274283
if (stats.size > 50_000) {
275284
if (isDataFile(fileName)) {
276-
debug(`Skipping large data file ${fileName} with size ${stats.size}`);
285+
debug('Skipping large data file %s with size %d', fileName, stats.size);
277286
appendFile = false;
278287
} else {
279-
debug(`WARNING Large file ${fileName} with size ${stats.size}`);
288+
debug('WARNING Large file %s with size %d', fileName, stats.size);
280289
}
281290
}
282291
}

0 commit comments

Comments
 (0)