Skip to content

Commit

Permalink
BattleSearch: Replace lookahead with union (#10679)
Browse files Browse the repository at this point in the history
PCRE lookahead was causing the regex to be pathologically slow.
  • Loading branch information
larry-the-table-guy authored Dec 17, 2024
1 parent 602e1d8 commit ab5f713
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions server/chat-plugins/battlesearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export async function runBattleSearch(userids: ID[], month: string, tierid: ID,
if (useRipgrep) {
// Matches non-word (including _ which counts as a word) characters between letters/numbers
// in a user's name so the userid can case-insensitively be matched to the name.
const regexString = userids.map(id => `(?=.*?("p(1|2)":"${[...id].join('[^a-zA-Z0-9]*')}[^a-zA-Z0-9]*"))`).join('');
// We want to be order-insensitive for the player IDs. This union is much cheaper than using PCRE.
const userUnion = userids.map(id => `${[...id].join('[^a-zA-Z0-9]*')}[^a-zA-Z0-9]*`).join('|');
const regexString = userids.map(id => `(.*?("p(1|2)":"(${userUnion})"))`).join('');
let output;
try {
output = await ProcessManager.exec(['rg', '-i', regexString, '--no-line-number', '-P', '-tjson', ...files]);
output = await ProcessManager.exec(['rg', '-i', regexString, '--no-line-number', '-tjson', ...files]);
} catch {
return results;
}
Expand Down

0 comments on commit ab5f713

Please sign in to comment.