Skip to content

Commit 13417c8

Browse files
Merge pull request #436 from QueryaHub/issue/424-syntax-highlight-threshold
perf(editor): honor syntax highlight isolate threshold (#424)
2 parents 357b508 + 79e055c commit 13417c8

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎lib/core/editor/syntax_highlight_isolate.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,14 @@ TextStyle? _styleFromSegment(HighlightSegment s, TextStyle? base) {
118118
);
119119
}
120120

121-
/// Runs [syntaxHighlightInIsolate] off the UI thread.
121+
/// Runs highlighting on a worker isolate when [job.code] is large enough;
122+
/// otherwise highlights synchronously on the calling isolate (avoids
123+
/// `compute` overhead for small editors).
122124
Future<List<HighlightSegment>> highlightOffMainThread(
123125
SyntaxHighlightJob job,
124126
) {
127+
if (job.code.length < kSyntaxHighlightIsolateThreshold) {
128+
return Future<List<HighlightSegment>>.value(syntaxHighlightInIsolate(job));
129+
}
125130
return compute(syntaxHighlightInIsolate, job);
126131
}

‎test/core/editor/syntax_highlight_isolate_test.dart‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,27 @@ void main() {
3333
expect(segments, isNotEmpty);
3434
expect(segments.map((s) => s.text).join(), code);
3535
});
36+
37+
test('small SQL buffer highlights inline below isolate threshold', () async {
38+
const code = 'SELECT 1;';
39+
expect(code.length, lessThan(kSyntaxHighlightIsolateThreshold));
40+
41+
final config = buildDefaultEditorHighlighterConfig(
42+
QueryaTheme.darkDefault.editor,
43+
);
44+
final segments = await highlightOffMainThread(
45+
SyntaxHighlightJob(
46+
code: code,
47+
language: 'sql',
48+
themeConfigJson: config,
49+
grammarJson: SyntaxHighlightService.grammarJsonFor(
50+
QueryaCodeLanguage.sql,
51+
),
52+
wrapperArgb: QueryaTheme.darkDefault.editor.foreground.toARGB32(),
53+
),
54+
);
55+
56+
expect(segments, isNotEmpty);
57+
expect(segments.map((s) => s.text).join(), code);
58+
});
3659
}

0 commit comments

Comments
 (0)