From b9630ceaf08498db9955c50e93bf761e769d3af9 Mon Sep 17 00:00:00 2001 From: brharrington Date: Fri, 2 Aug 2024 17:25:17 -0500 Subject: [PATCH] atlas: reduce amount of tag traversal (#1149) Update QueryIndex.forEachMatch to use the proper position when calling sub-trees. This reduces the need to reprocess earlier positions in the tags list that will not match. --- .../com/netflix/spectator/atlas/impl/QueryIndex.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/impl/QueryIndex.java b/spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/impl/QueryIndex.java index f495870e7..0a25dd3dc 100644 --- a/spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/impl/QueryIndex.java +++ b/spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/impl/QueryIndex.java @@ -383,12 +383,13 @@ private void forEachMatch(Id tags, int i, Consumer consumer) { String v = tags.getValue(j); int cmp = compare(k, keyRef); if (cmp == 0) { + final int nextPos = j + 1; keyPresent = true; // Find exact matches QueryIndex eqIdx = equalChecks.get(v); if (eqIdx != null) { - eqIdx.forEachMatch(tags, i + 1, consumer); + eqIdx.forEachMatch(tags, nextPos, consumer); } // Scan for matches with other conditions @@ -403,7 +404,7 @@ private void forEachMatch(Id tags, int i, Consumer consumer) { QueryIndex idx = otherChecks.get(kq); if (idx != null) { tmp.add(idx); - idx.forEachMatch(tags, i + 1, consumer); + idx.forEachMatch(tags, nextPos, consumer); } } }); @@ -414,14 +415,14 @@ private void forEachMatch(Id tags, int i, Consumer consumer) { // size/get avoids the allocation and has better throughput. int n = otherMatches.size(); for (int p = 0; p < n; ++p) { - otherMatches.get(p).forEachMatch(tags, i + 1, consumer); + otherMatches.get(p).forEachMatch(tags, nextPos, consumer); } } // Check matches for has key final QueryIndex hasKeyIdxRef = hasKeyIdx; if (hasKeyIdxRef != null) { - hasKeyIdxRef.forEachMatch(tags, i, consumer); + hasKeyIdxRef.forEachMatch(tags, j, consumer); } }