From 4bf54bc2985154718c44c29728d0a061fb6e1591 Mon Sep 17 00:00:00 2001 From: David Mollitor Date: Mon, 3 Nov 2025 10:49:39 -0500 Subject: [PATCH] Remove redundant sorted check before Arrays.sort() Arrays.sort() uses TimSort which is already optimized for sorted arrays with O(n) best-case complexity. The pre-sort check adds unnecessary overhead for unsorted data (common case) without significant benefit. Simplified toKeyValues() and toTags() by removing isSortedSet() method and relying on TimSort's adaptive sorting behavior. Signed-off-by: David Mollitor --- .../java/io/micrometer/common/KeyValues.java | 28 ++----------------- .../io/micrometer/core/instrument/Tags.java | 28 ++----------------- 2 files changed, 4 insertions(+), 52 deletions(-) diff --git a/micrometer-commons/src/main/java/io/micrometer/common/KeyValues.java b/micrometer-commons/src/main/java/io/micrometer/common/KeyValues.java index 2d0eda45c9..fe9154af53 100644 --- a/micrometer-commons/src/main/java/io/micrometer/common/KeyValues.java +++ b/micrometer-commons/src/main/java/io/micrometer/common/KeyValues.java @@ -63,27 +63,6 @@ private KeyValues(KeyValue[] sortedSet, int length) { this.length = length; } - /** - * Checks if the first {@code length} elements of the {@code keyValues} array form an - * ordered set of key-values. - * @param keyValues an array of key-values. - * @param length the number of elements to check. - * @return {@code true} if the first {@code length} elements of {@code keyValues} form - * an ordered set; otherwise {@code false}. - */ - private static boolean isSortedSet(KeyValue[] keyValues, int length) { - if (length > keyValues.length) { - return false; - } - for (int i = 0; i < length - 1; i++) { - int cmp = keyValues[i].compareTo(keyValues[i + 1]); - if (cmp >= 0) { - return false; - } - } - return true; - } - /** * Constructs a {@code KeyValues} collection from the provided array of key-values. * @param keyValues an array of {@code KeyValue} objects, possibly unordered and/or @@ -92,11 +71,8 @@ private static boolean isSortedSet(KeyValue[] keyValues, int length) { * key-values. */ private static KeyValues toKeyValues(KeyValue[] keyValues) { - int len = keyValues.length; - if (!isSortedSet(keyValues, len)) { - Arrays.sort(keyValues); - len = dedup(keyValues); - } + Arrays.sort(keyValues); + int len = dedup(keyValues); return new KeyValues(keyValues, len); } diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/Tags.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/Tags.java index d84971e0de..84d6d30b14 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/Tags.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/Tags.java @@ -60,27 +60,6 @@ private Tags(Tag[] sortedSet, int length) { this.length = length; } - /** - * Checks if the first {@code length} elements of the {@code tags} array form an - * ordered set of tags. - * @param tags an array of tags. - * @param length the number of elements to check. - * @return {@code true} if the first {@code length} elements of {@code tags} form an - * ordered set; otherwise {@code false}. - */ - private static boolean isSortedSet(Tag[] tags, int length) { - if (length > tags.length) { - return false; - } - for (int i = 0; i < length - 1; i++) { - int cmp = tags[i].compareTo(tags[i + 1]); - if (cmp >= 0) { - return false; - } - } - return true; - } - /** * Constructs a {@code Tags} collection from the provided array of tags. * @param tags an array of {@code Tag} objects, possibly unordered and/or containing @@ -88,11 +67,8 @@ private static boolean isSortedSet(Tag[] tags, int length) { * @return a {@code Tags} instance with a deduplicated and ordered set of tags. */ private static Tags toTags(Tag[] tags) { - int len = tags.length; - if (!isSortedSet(tags, len)) { - Arrays.sort(tags); - len = dedup(tags); - } + Arrays.sort(tags); + int len = dedup(tags); return new Tags(tags, len); }