Skip to content

Commit

Permalink
Improve performance of YAML JsonPathMatcher
Browse files Browse the repository at this point in the history
Only apply `ReplaceAliasWithAnchorValueVisitor` to `Yaml.Document` elements.

See: #4650
  • Loading branch information
knutwannheden committed Nov 17, 2024
1 parent 0f76203 commit f71dc0d
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,35 @@ public boolean matches(Cursor cursor) {

private static List<Tree> resolvedAncestors(Cursor cursor) {
ArrayDeque<Tree> deque = new ArrayDeque<>();
Map<Tree, Tree> resolved = new IdentityHashMap<>();
for (Iterator<Object> it = cursor.getPath(Tree.class::isInstance); it.hasNext(); ) {
Tree tree = (Tree) it.next();
tree = tree instanceof Yaml ? new ReplaceAliasWithAnchorValueVisitor<Integer>().visitNonNull(tree, 0) : tree;
if (tree instanceof Yaml.Document) {
tree = new ReplaceAliasWithAnchorValueVisitor<Integer>() {
@Override
public @Nullable Yaml visit(@Nullable Tree tree, Integer p) {
Yaml updated = super.visit(tree, p);
if (tree != null && updated != tree) {
resolved.put(tree, updated);
}
return updated;
}
}.visitNonNull(tree, 0);
deque.addFirst(tree);
break;
}
deque.addFirst(tree);
}
return new ArrayList<>(deque);
ArrayList<Tree> list = new ArrayList<>(deque);
if (!resolved.isEmpty()) {
for (int i = 0; i < list.size(); i++) {
Tree tree = list.get(i);
if (resolved.containsKey(tree)) {
list.set(i, resolved.get(tree));
}
}
}
return list;
}

private JsonPathParser jsonPath() {
Expand Down

0 comments on commit f71dc0d

Please sign in to comment.