Skip to content

Commit 921f67e

Browse files
perf: avoid unnecessary String construction
1 parent 5f23c29 commit 921f67e

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

src/main/java/io/github/treesitter/jtreesitter/CapturesIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public CapturesIterator(
3838

3939
@Override
4040
public boolean tryAdvance(Consumer<? super SimpleImmutableEntry<Integer, QueryMatch>> action) {
41-
var hasNoText = tree.getText() == null;
41+
var hasNoText = !tree.hasText();
4242
MemorySegment match = allocator.allocate(TSQueryMatch.layout());
4343
MemorySegment index = allocator.allocate(C_INT);
4444
var captureNames = query.getCaptureNames();

src/main/java/io/github/treesitter/jtreesitter/MatchesIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public MatchesIterator(
3636

3737
@Override
3838
public boolean tryAdvance(Consumer<? super QueryMatch> action) {
39-
var hasNoText = tree.getText() == null;
39+
var hasNoText = !tree.hasText();
4040
MemorySegment match = allocator.allocate(TSQueryMatch.layout());
4141
var captureNames = query.getCaptureNames();
4242
while (ts_query_cursor_next_match(cursor, match)) {

src/main/java/io/github/treesitter/jtreesitter/Tree.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ MemorySegment segment() {
4444
return self;
4545
}
4646

47+
boolean hasText() {
48+
return charset != null;
49+
}
50+
4751
@Nullable
4852
String getRegion(@Unsigned int start, @Unsigned int end) {
4953
var length = Math.min(end, source.length) - start;
@@ -57,7 +61,7 @@ public Language getLanguage() {
5761

5862
/** Get the source code of the syntax tree, if available. */
5963
public @Nullable String getText() {
60-
return charset != null ? new String(source, charset) : null;
64+
return hasText() ? new String(source, charset) : null;
6165
}
6266

6367
/** Get the root node of the syntax tree. */
@@ -161,6 +165,6 @@ public void close() throws RuntimeException {
161165

162166
@Override
163167
public String toString() {
164-
return "Tree{language=%s, source=%s}".formatted(language, source);
168+
return "Tree{language=%s, source=%s}".formatted(language, getText());
165169
}
166170
}

src/test/java/io/github/treesitter/jtreesitter/QueryTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,13 @@ void getPatternAssertions() {
181181

182182
@Test
183183
void queryWithTwoPredicates() {
184-
var source = """
184+
var source =
185+
"""
185186
((identifier) @foo
186187
(#eq? @foo "foo")
187188
(#not-eq? @foo "bar"))
188189
"""
189-
.stripIndent();
190+
.stripIndent();
190191
assertQuery(source, query -> {
191192
assertEquals(1, query.getPatternCount());
192193
assertIterableEquals(List.of("foo"), query.getCaptureNames());

0 commit comments

Comments
 (0)