Skip to content

Commit eebd7f8

Browse files
Optimize surrounding pair performance (#3190)
1 parent f6ebbfe commit eebd7f8

12 files changed

Lines changed: 236 additions & 124 deletions

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
## Build and Test
1717

18-
- Always run lint and typecheck when making changes:
18+
- Always run lint and compile when making changes:
1919
- `pnpm run lint`
20-
- `pnpm run typecheck`
20+
- `pnpm run compile`
2121
- Tests can be run with:
2222
- `pnpm test`
2323

‎packages/common/src/util/type.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export function isString(arg: unknown): arg is string {
22
return typeof arg === "string" || arg instanceof String;
33
}
4+
5+
export type Mutable<T> = { -readonly [P in keyof T]: T[P] };

‎packages/cursorless-engine/src/languages/TreeSitterQuery/QueryCapture.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface QueryMatch {
4141

4242
/**
4343
* A capture of a query pattern against a syntax tree. This type is used
44-
* internally by the query engine to allow operators to modify the capture.
44+
* internally by the query engine to allow predicates to modify the capture.
4545
*/
4646
export interface MutableQueryCapture extends QueryCapture {
4747
/**
@@ -58,14 +58,11 @@ export interface MutableQueryCapture extends QueryCapture {
5858

5959
/**
6060
* A match of a query pattern against a syntax tree that can be mutated. This
61-
* type is used internally by the query engine to allow operators to modify the
61+
* type is used internally by the query engine to allow predicates to modify the
6262
* match.
6363
*/
6464
export interface MutableQueryMatch extends QueryMatch {
65-
/**
66-
* The index of the pattern that was matched.
67-
*/
68-
readonly patternIdx: number;
69-
7065
readonly captures: MutableQueryCapture[];
7166
}
67+
68+
export type PatternPredicate = (match: MutableQueryMatch) => boolean;

‎packages/cursorless-engine/src/languages/TreeSitterQuery/TreeSitterQuery.ts‎

Lines changed: 112 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { Position, TextDocument, TreeSitter } from "@cursorless/common";
1+
import type {
2+
Mutable,
3+
Position,
4+
TextDocument,
5+
TreeSitter,
6+
} from "@cursorless/common";
27
import type * as treeSitter from "web-tree-sitter";
38
import { ide } from "../../singletons/ide.singleton";
49
import {
@@ -13,10 +18,13 @@ import { parsePredicatesWithErrorHandling } from "./parsePredicatesWithErrorHand
1318
import { positionToPoint } from "./positionToPoint";
1419
import type {
1520
MutableQueryCapture,
16-
MutableQueryMatch,
21+
PatternPredicate,
22+
QueryCapture,
1723
QueryMatch,
1824
} from "./QueryCapture";
1925
import {
26+
createTestQueryCapture,
27+
getStartOfEndOfNodeRange,
2028
getStartOfEndOfRange,
2129
rewriteStartOfEndOf,
2230
} from "./rewriteStartOfEndOf";
@@ -42,7 +50,7 @@ export class TreeSitterQuery {
4250
* array corresponds to a pattern, and each element of the inner array
4351
* corresponds to a predicate for that pattern.
4452
*/
45-
private patternPredicates: ((match: MutableQueryMatch) => boolean)[][],
53+
private patternPredicates: PatternPredicate[][],
4654
) {
4755
this.shouldCheckCaptures = ide().runMode !== "production";
4856
}
@@ -126,30 +134,22 @@ export class TreeSitterQuery {
126134
continue;
127135
}
128136

129-
const hasPatternPredicates =
130-
this.patternPredicates[match.patternIndex].length > 0;
131-
132-
const mutableMatch = this.createMutableQueryMatch(
133-
document,
134-
match,
135-
// If there are pattern predicates, we need to include all captures when
136-
// creating the mutable match, since the predicates may depend on any of
137-
// the captures.
138-
!hasPatternPredicates ? captureNameFilter : undefined,
139-
);
140-
141-
if (!this.runPredicates(mutableMatch)) {
142-
continue;
143-
}
144-
145-
const queryMatch = this.createQueryMatch(
146-
mutableMatch,
147-
// We only need to filter here if we didn't filter in createMutableQueryMatch()
148-
hasPatternPredicates ? captureNameFilter : undefined,
149-
);
150-
151-
if (queryMatch != null) {
152-
results.push(queryMatch);
137+
const patternPredicates = this.patternPredicates[match.patternIndex];
138+
139+
// The split path here is to avoid creating query captures if there are no
140+
// predicates, Solely for performance.
141+
const captures =
142+
patternPredicates.length > 0
143+
? this.createQueryCapturesWithPredicates(
144+
document,
145+
match,
146+
patternPredicates,
147+
captureNameFilter,
148+
)
149+
: this.createQueryCapturesWithoutPredicates(match, captureNameFilter);
150+
151+
if (captures.length > 0) {
152+
results.push({ captures });
153153
}
154154
}
155155

@@ -158,8 +158,8 @@ export class TreeSitterQuery {
158158

159159
private getTreeMatches(
160160
document: TextDocument,
161-
start?: Position,
162-
end?: Position,
161+
start: Position | undefined,
162+
end: Position | undefined,
163163
) {
164164
const { rootNode } = this.treeSitter.getTree(document);
165165
return this.query.matches(rootNode, {
@@ -168,79 +168,62 @@ export class TreeSitterQuery {
168168
});
169169
}
170170

171-
private createMutableQueryMatch(
171+
private createQueryCapturesWithPredicates(
172172
document: TextDocument,
173173
match: treeSitter.QueryMatch,
174+
predicates: PatternPredicate[],
174175
captureNameFilter: Set<number> | undefined,
175-
): MutableQueryMatch {
176+
): QueryCapture[] {
176177
const captures: MutableQueryCapture[] = [];
177178

178-
for (const { name, node } of match.captures) {
179-
if (
180-
captureNameFilter != null &&
181-
!captureNameFilter.has(getNormalizedCaptureIndex(name))
182-
) {
183-
continue;
184-
}
185-
179+
for (const capture of match.captures) {
186180
captures.push({
187-
name,
188-
node,
181+
name: capture.name,
182+
node: capture.node,
189183
document,
190-
range: getNodeRange(node),
191-
insertionDelimiter: undefined,
184+
range: getNodeRange(capture.node),
192185
allowMultiple: false,
193-
hasError: () => isContainedInErrorNode(node),
186+
insertionDelimiter: undefined,
187+
hasError: () => isContainedInErrorNode(capture.node),
194188
});
195189
}
196190

197-
return {
198-
patternIdx: match.patternIndex,
199-
captures,
200-
};
201-
}
202-
203-
private runPredicates(match: MutableQueryMatch): boolean {
204-
for (const predicate of this.patternPredicates[match.patternIdx]) {
205-
if (!predicate(match)) {
206-
return false;
191+
for (const predicate of predicates) {
192+
if (!predicate({ captures })) {
193+
return [];
207194
}
208195
}
209-
return true;
210-
}
211196

212-
private createQueryMatch(
213-
match: MutableQueryMatch,
214-
captureNameFilter: Set<number> | undefined,
215-
): QueryMatch | undefined {
216-
const result: MutableQueryCapture[] = [];
197+
const result: QueryCapture[] = [];
217198
const map = new Map<
218199
string,
219-
{ acc: MutableQueryCapture; captures: MutableQueryCapture[] }
200+
{ acc: Mutable<QueryCapture>; captures: QueryCapture[] }
220201
>();
221202

222203
// Merge the ranges of all captures with the same name into a single
223204
// range and return one capture with that name. We consider captures
224205
// with names `@foo`, `@foo.start`, and `@foo.end` to have the same
225206
// name, for which we'd return a capture with name `foo`.
226207

227-
for (const capture of match.captures) {
208+
for (const capture of captures) {
228209
if (
229210
captureNameFilter != null &&
230211
!captureNameFilter.has(getNormalizedCaptureIndex(capture.name))
231212
) {
232213
continue;
233214
}
215+
234216
const name = getNormalizedCaptureName(capture.name);
235217
const range = getStartOfEndOfRange(capture);
236218
const existing = map.get(name);
237219

238220
if (existing == null) {
239221
const captures = [capture];
240-
const acc = {
241-
...capture,
222+
const acc: QueryCapture = {
242223
name,
243224
range,
225+
allowMultiple: capture.allowMultiple,
226+
insertionDelimiter: capture.insertionDelimiter,
244227
hasError: () => captures.some((c) => c.hasError()),
245228
};
246229
result.push(acc);
@@ -255,26 +238,77 @@ export class TreeSitterQuery {
255238
}
256239
}
257240

258-
if (result.length === 0) {
259-
return undefined;
241+
if (this.shouldCheckCaptures) {
242+
checkCaptures(Array.from(map.values()));
243+
}
244+
245+
return result;
246+
}
247+
248+
private createQueryCapturesWithoutPredicates(
249+
match: treeSitter.QueryMatch,
250+
captureNameFilter: Set<number> | undefined,
251+
): QueryCapture[] {
252+
const result: QueryCapture[] = [];
253+
const map = new Map<
254+
string,
255+
{
256+
acc: Mutable<QueryCapture>;
257+
captures: treeSitter.QueryCapture[];
258+
}
259+
>();
260+
261+
for (const capture of match.captures) {
262+
if (
263+
captureNameFilter != null &&
264+
!captureNameFilter.has(getNormalizedCaptureIndex(capture.name))
265+
) {
266+
continue;
267+
}
268+
269+
const name = getNormalizedCaptureName(capture.name);
270+
const range = getStartOfEndOfNodeRange(capture.name, capture.node);
271+
const existing = map.get(name);
272+
273+
if (existing == null) {
274+
const captures = [capture];
275+
const acc: QueryCapture = {
276+
name,
277+
range,
278+
allowMultiple: false,
279+
insertionDelimiter: undefined,
280+
hasError: () => captures.some((c) => isContainedInErrorNode(c.node)),
281+
};
282+
result.push(acc);
283+
map.set(name, { acc, captures });
284+
} else {
285+
existing.acc.range = existing.acc.range.union(range);
286+
existing.captures.push(capture);
287+
}
260288
}
261289

262290
if (this.shouldCheckCaptures) {
263-
this.checkCaptures(Array.from(map.values()));
291+
checkCaptures(
292+
Array.from(map.values(), (v) => ({
293+
captures: v.captures.map((c) =>
294+
createTestQueryCapture(c.name, getNodeRange(c.node)),
295+
),
296+
})),
297+
);
264298
}
265299

266-
return { captures: result };
300+
return result;
267301
}
302+
}
268303

269-
private checkCaptures(matches: { captures: MutableQueryCapture[] }[]) {
270-
for (const match of matches) {
271-
const capturesAreValid = checkCaptureStartEnd(
272-
rewriteStartOfEndOf(match.captures),
273-
ide().messages,
274-
);
275-
if (!capturesAreValid && ide().runMode === "test") {
276-
throw new Error("Invalid captures");
277-
}
304+
function checkCaptures(matches: { captures: QueryCapture[] }[]) {
305+
for (const match of matches) {
306+
const capturesAreValid = checkCaptureStartEnd(
307+
rewriteStartOfEndOf(match.captures),
308+
ide().messages,
309+
);
310+
if (!capturesAreValid && ide().runMode === "test") {
311+
throw new Error("Invalid captures");
278312
}
279313
}
280314
}

‎packages/cursorless-engine/src/languages/TreeSitterQuery/captureNames.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ function normalizeCaptureName(name: string): string {
9191
}
9292

9393
// eg: for `statement.start.endOf`, returns `statement`
94-
function getScopeName(name: string): string {
95-
return /^(private\.[^.]*|[^.]*)/.exec(name)![0];
94+
function getScopeName(captureName: string): string {
95+
return /^(private\.[^.]*|[^.]*)/.exec(captureName)![0];
9696
}
9797

9898
export function isCaptureAllowed(captureName: string): boolean {

‎packages/cursorless-engine/src/languages/TreeSitterQuery/getNodeRange.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,21 @@ export function getNodeRange(node: Node) {
99
node.endPosition.column,
1010
);
1111
}
12+
13+
export function getNodeStartRange(node: Node) {
14+
return new Range(
15+
node.startPosition.row,
16+
node.startPosition.column,
17+
node.startPosition.row,
18+
node.startPosition.column,
19+
);
20+
}
21+
22+
export function getNodeEndRange(node: Node) {
23+
return new Range(
24+
node.endPosition.row,
25+
node.endPosition.column,
26+
node.endPosition.row,
27+
node.endPosition.column,
28+
);
29+
}

‎packages/cursorless-engine/src/languages/TreeSitterQuery/parsePredicates.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { QueryPredicate } from "web-tree-sitter";
2-
import type { MutableQueryMatch } from "./QueryCapture";
2+
import type { PatternPredicate } from "./QueryCapture";
33
import { queryPredicateOperators } from "./queryPredicateOperators";
44

55
/**
@@ -16,11 +16,11 @@ import { queryPredicateOperators } from "./queryPredicateOperators";
1616
*/
1717
export function parsePredicates(predicateDescriptors: QueryPredicate[][]) {
1818
const errors: PredicateError[] = [];
19-
const predicates: ((match: MutableQueryMatch) => boolean)[][] = [];
19+
const predicates: PatternPredicate[][] = [];
2020

2121
predicateDescriptors.forEach((patternPredicateDescriptors, patternIdx) => {
2222
/** The predicates for a given pattern */
23-
const patternPredicates: ((match: MutableQueryMatch) => boolean)[] = [];
23+
const patternPredicates: PatternPredicate[] = [];
2424

2525
patternPredicateDescriptors.forEach((predicateDescriptor, predicateIdx) => {
2626
const operator = queryPredicateOperators.find(

‎packages/cursorless-engine/src/languages/TreeSitterQuery/parsePredicatesWithErrorHandling.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import type { Query } from "web-tree-sitter";
33
import { ide } from "../../singletons/ide.singleton";
44
import { parsePredicates } from "./parsePredicates";
55
import { predicateToString } from "./predicateToString";
6+
import type { PatternPredicate } from "./QueryCapture";
67

78
export function parsePredicatesWithErrorHandling(
89
languageId: string,
910
query: Query,
10-
) {
11+
): PatternPredicate[][] {
1112
const { errors, predicates } = parsePredicates(query.predicates);
1213

1314
if (errors.length > 0) {

0 commit comments

Comments
 (0)