From e074c110de6e352c82b8e432fac94793fbd623fe Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Wed, 20 Nov 2024 16:23:03 +0900 Subject: [PATCH 1/2] Polish gh-4959 (#5692) --- .../core/KeyValuesMergeBenchmark.java | 2 +- .../benchmark/core/TagsMergeBenchmark.java | 4 +- .../java/io/micrometer/common/KeyValues.java | 113 ++++++++++-------- .../io/micrometer/core/instrument/Tags.java | 72 ++++++----- 4 files changed, 104 insertions(+), 87 deletions(-) diff --git a/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/KeyValuesMergeBenchmark.java b/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/KeyValuesMergeBenchmark.java index 29a31cb85e..bc443d8aea 100644 --- a/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/KeyValuesMergeBenchmark.java +++ b/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/KeyValuesMergeBenchmark.java @@ -34,7 +34,7 @@ public class KeyValuesMergeBenchmark { static final KeyValues left = KeyValues.of("key", "value", "key2", "value2", "key6", "value6", "key7", "value7", "key8", "value8", "keyA", "valueA", "keyC", "valueC", "keyE", "valueE", "keyF", "valueF", "keyG", "valueG", - "keyG", "valueG", "keyG", "valueG", "keyH", "valueH"); + "keyH", "valueH"); static final KeyValues right = KeyValues.of("key", "value", "key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5", "keyA", "valueA", "keyB", "valueB", "keyD", "valueD"); diff --git a/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/TagsMergeBenchmark.java b/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/TagsMergeBenchmark.java index 3e0774c522..3d1c1f53c8 100644 --- a/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/TagsMergeBenchmark.java +++ b/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/TagsMergeBenchmark.java @@ -33,8 +33,8 @@ public class TagsMergeBenchmark { static final Tags left = Tags.of("key", "value", "key2", "value2", "key6", "value6", "key7", "value7", "key8", - "value8", "keyA", "valueA", "keyC", "valueC", "keyE", "valueE", "keyF", "valueF", "keyG", "valueG", "keyG", - "valueG", "keyG", "valueG", "keyH", "valueH"); + "value8", "keyA", "valueA", "keyC", "valueC", "keyE", "valueE", "keyF", "valueF", "keyG", "valueG", "keyH", + "valueH"); static final Tags right = Tags.of("key", "value", "key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5", "keyA", "valueA", "keyB", "valueB", "keyD", "valueD"); 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 8a2730e729..6c32e8f878 100644 --- a/micrometer-commons/src/main/java/io/micrometer/common/KeyValues.java +++ b/micrometer-commons/src/main/java/io/micrometer/common/KeyValues.java @@ -36,24 +36,26 @@ */ public final class KeyValues implements Iterable { - private static final KeyValues EMPTY = new KeyValues(new KeyValue[] {}, 0); + private static final KeyValue[] EMPTY_KEY_VALUE_ARRAY = new KeyValue[0]; + + private static final KeyValues EMPTY = new KeyValues(EMPTY_KEY_VALUE_ARRAY, 0); /** - * A private array of {@code KeyValue} objects containing the sorted and deduplicated - * tags. + * An array of {@code KeyValue} objects containing the sorted and deduplicated + * key-values. */ private final KeyValue[] sortedSet; /** - * The number of valid tags present in the {@link #sortedSet} array. + * The number of valid key-values present in the {@link #sortedSet} array. */ private final int length; /** - * A private constructor that initializes a {@code KeyValues} object with a sorted set - * of keyvalues and its length. - * @param sortedSet an ordered set of unique keyvalues by key - * @param length the number of valid tags in the {@code sortedSet} + * A constructor that initializes a {@code KeyValues} object with a sorted set of + * key-values and its length. + * @param sortedSet an ordered set of unique key-values by key + * @param length the number of valid key-values in the {@code sortedSet} */ private KeyValues(KeyValue[] sortedSet, int length) { this.sortedSet = sortedSet; @@ -61,12 +63,12 @@ private KeyValues(KeyValue[] sortedSet, int length) { } /** - * Checks if the first {@code length} elements of the {@code keyvalues} array form an - * ordered set of keyvalues. - * @param keyValues an array of keyvalues. - * @param length the number of items to check. - * @return {@code true} if the first {@code length} items of {@code keyvalues} form an - * ordered set; otherwise {@code false}. + * 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) { @@ -82,12 +84,13 @@ private static boolean isSortedSet(KeyValue[] keyValues, int length) { } /** - * Constructs a {@code Tags} collection from the provided array of tags. - * @param keyValues an array of {@code Tag} objects, possibly unordered and/or + * Constructs a {@code KeyValues} collection from the provided array of key-values. + * @param keyValues an array of {@code KeyValue} objects, possibly unordered and/or * containing duplicates. - * @return a {@code Tags} instance with a deduplicated and ordered set of tags. + * @return a {@code KeyValues} instance with a deduplicated and ordered set of + * key-values. */ - private static KeyValues make(KeyValue[] keyValues) { + private static KeyValues toKeyValues(KeyValue[] keyValues) { int len = keyValues.length; if (!isSortedSet(keyValues, len)) { Arrays.sort(keyValues); @@ -97,10 +100,10 @@ private static KeyValues make(KeyValue[] keyValues) { } /** - * Removes duplicate tags from an ordered array of tags. - * @param keyValues an ordered array of {@code Tag} objects. - * @return the number of unique tags in the {@code tags} array after removing - * duplicates. + * Removes duplicate key-values from an ordered array of key-values. + * @param keyValues an ordered array of {@code KeyValue} objects. + * @return the number of unique key-values in the {@code keyValues} array after + * removing duplicates. */ private static int dedup(KeyValue[] keyValues) { int n = keyValues.length; @@ -121,12 +124,12 @@ private static int dedup(KeyValue[] keyValues) { } /** - * Constructs a {@code Tags} instance by merging two sets of tags in time proportional - * to the sum of their sizes. - * @param other the set of tags to merge with this one. - * @return a {@code Tags} instance with the merged sets of tags. + * Constructs a {@code KeyValues} instance by merging two sets of key-values in time + * proportional to the sum of their sizes. + * @param other the set of key-values to merge with this one. + * @return a {@code KeyValues} instance with the merged sets of key-values. */ - private KeyValues merged(KeyValues other) { + private KeyValues merge(KeyValues other) { if (other.length == 0) { return this; } @@ -134,36 +137,42 @@ private KeyValues merged(KeyValues other) { return this; } KeyValue[] sortedSet = new KeyValue[this.length + other.length]; - int sortedIdx = 0, thisIdx = 0, otherIdx = 0; - while (thisIdx < this.length && otherIdx < other.length) { - int cmp = this.sortedSet[thisIdx].compareTo(other.sortedSet[otherIdx]); + int sortedIndex = 0; + int thisIndex = 0; + int otherIndex = 0; + while (thisIndex < this.length && otherIndex < other.length) { + KeyValue thisKeyValue = this.sortedSet[thisIndex]; + KeyValue otherKeyValue = other.sortedSet[otherIndex]; + int cmp = thisKeyValue.compareTo(otherKeyValue); if (cmp > 0) { - sortedSet[sortedIdx] = other.sortedSet[otherIdx]; - otherIdx++; + sortedSet[sortedIndex] = otherKeyValue; + otherIndex++; } else if (cmp < 0) { - sortedSet[sortedIdx] = this.sortedSet[thisIdx]; - thisIdx++; + sortedSet[sortedIndex] = thisKeyValue; + thisIndex++; } else { - // In case of key conflict prefer tag from other set - sortedSet[sortedIdx] = other.sortedSet[otherIdx]; - thisIdx++; - otherIdx++; + // In case of key conflict prefer key-value from other set + sortedSet[sortedIndex] = otherKeyValue; + thisIndex++; + otherIndex++; } - sortedIdx++; + sortedIndex++; } - int thisRemaining = this.length - thisIdx; + int thisRemaining = this.length - thisIndex; if (thisRemaining > 0) { - System.arraycopy(this.sortedSet, thisIdx, sortedSet, sortedIdx, thisRemaining); - sortedIdx += thisRemaining; + System.arraycopy(this.sortedSet, thisIndex, sortedSet, sortedIndex, thisRemaining); + sortedIndex += thisRemaining; } - int otherRemaining = other.length - otherIdx; - if (otherIdx < other.sortedSet.length) { - System.arraycopy(other.sortedSet, otherIdx, sortedSet, sortedIdx, otherRemaining); - sortedIdx += otherRemaining; + else { + int otherRemaining = other.length - otherIndex; + if (otherRemaining > 0) { + System.arraycopy(other.sortedSet, otherIndex, sortedSet, sortedIndex, otherRemaining); + sortedIndex += otherRemaining; + } } - return new KeyValues(sortedSet, sortedIdx); + return new KeyValues(sortedSet, sortedIndex); } /** @@ -200,7 +209,7 @@ public KeyValues and(@Nullable KeyValue... keyValues) { if (blankVarargs(keyValues)) { return this; } - return and(make(keyValues)); + return and(toKeyValues(keyValues)); } /** @@ -237,7 +246,7 @@ public KeyValues and(@Nullable Iterable keyValues) { return KeyValues.of(keyValues); } - return merged(KeyValues.of(keyValues)); + return merge(KeyValues.of(keyValues)); } @Override @@ -362,10 +371,10 @@ else if (keyValues instanceof KeyValues) { } else if (keyValues instanceof Collection) { Collection keyValuesCollection = (Collection) keyValues; - return make(keyValuesCollection.toArray(new KeyValue[0])); + return toKeyValues(keyValuesCollection.toArray(EMPTY_KEY_VALUE_ARRAY)); } else { - return make(StreamSupport.stream(keyValues.spliterator(), false).toArray(KeyValue[]::new)); + return toKeyValues(StreamSupport.stream(keyValues.spliterator(), false).toArray(KeyValue[]::new)); } } @@ -397,7 +406,7 @@ public static KeyValues of(@Nullable String... keyValues) { for (int i = 0; i < keyValues.length; i += 2) { keyValueArray[i / 2] = KeyValue.of(keyValues[i], keyValues[i + 1]); } - return make(keyValueArray); + return toKeyValues(keyValueArray); } private static boolean blankVarargs(@Nullable Object[] args) { 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 523f274c1d..1c696066e8 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 @@ -34,10 +34,12 @@ */ public final class Tags implements Iterable { - private static final Tags EMPTY = new Tags(new Tag[] {}, 0); + private static final Tag[] EMPTY_TAG_ARRAY = new Tag[0]; + + private static final Tags EMPTY = new Tags(EMPTY_TAG_ARRAY, 0); /** - * A private array of {@code Tag} objects containing the sorted and deduplicated tags. + * An array of {@code Tag} objects containing the sorted and deduplicated tags. */ private final Tag[] sortedSet; @@ -47,8 +49,8 @@ public final class Tags implements Iterable { private final int length; /** - * A private constructor that initializes a {@code Tags} object with a sorted set of - * tags and its length. + * A constructor that initializes a {@code Tags} object with a sorted set of tags and + * its length. * @param sortedSet an ordered set of unique tags by key * @param length the number of valid tags in the {@code sortedSet} */ @@ -61,8 +63,8 @@ private Tags(Tag[] sortedSet, int 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 items to check. - * @return {@code true} if the first {@code length} items of {@code tags} form an + * @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) { @@ -84,7 +86,7 @@ private static boolean isSortedSet(Tag[] tags, int length) { * duplicates. * @return a {@code Tags} instance with a deduplicated and ordered set of tags. */ - private static Tags make(Tag[] tags) { + private static Tags toTags(Tag[] tags) { int len = tags.length; if (!isSortedSet(tags, len)) { Arrays.sort(tags); @@ -123,7 +125,7 @@ private static int dedup(Tag[] tags) { * @param other the set of tags to merge with this one. * @return a {@code Tags} instance with the merged sets of tags. */ - private Tags merged(Tags other) { + private Tags merge(Tags other) { if (other.length == 0) { return this; } @@ -131,36 +133,42 @@ private Tags merged(Tags other) { return this; } Tag[] sortedSet = new Tag[this.length + other.length]; - int sortedIdx = 0, thisIdx = 0, otherIdx = 0; - while (thisIdx < this.length && otherIdx < other.length) { - int cmp = this.sortedSet[thisIdx].compareTo(other.sortedSet[otherIdx]); + int sortedIndex = 0; + int thisIndex = 0; + int otherIndex = 0; + while (thisIndex < this.length && otherIndex < other.length) { + Tag thisTag = this.sortedSet[thisIndex]; + Tag otherTag = other.sortedSet[otherIndex]; + int cmp = thisTag.compareTo(otherTag); if (cmp > 0) { - sortedSet[sortedIdx] = other.sortedSet[otherIdx]; - otherIdx++; + sortedSet[sortedIndex] = otherTag; + otherIndex++; } else if (cmp < 0) { - sortedSet[sortedIdx] = this.sortedSet[thisIdx]; - thisIdx++; + sortedSet[sortedIndex] = thisTag; + thisIndex++; } else { // In case of key conflict prefer tag from other set - sortedSet[sortedIdx] = other.sortedSet[otherIdx]; - thisIdx++; - otherIdx++; + sortedSet[sortedIndex] = otherTag; + thisIndex++; + otherIndex++; } - sortedIdx++; + sortedIndex++; } - int thisRemaining = this.length - thisIdx; + int thisRemaining = this.length - thisIndex; if (thisRemaining > 0) { - System.arraycopy(this.sortedSet, thisIdx, sortedSet, sortedIdx, thisRemaining); - sortedIdx += thisRemaining; + System.arraycopy(this.sortedSet, thisIndex, sortedSet, sortedIndex, thisRemaining); + sortedIndex += thisRemaining; } - int otherRemaining = other.length - otherIdx; - if (otherIdx < other.sortedSet.length) { - System.arraycopy(other.sortedSet, otherIdx, sortedSet, sortedIdx, otherRemaining); - sortedIdx += otherRemaining; + else { + int otherRemaining = other.length - otherIndex; + if (otherRemaining > 0) { + System.arraycopy(other.sortedSet, otherIndex, sortedSet, sortedIndex, otherRemaining); + sortedIndex += otherRemaining; + } } - return new Tags(sortedSet, sortedIdx); + return new Tags(sortedSet, sortedIndex); } /** @@ -197,7 +205,7 @@ public Tags and(@Nullable Tag... tags) { if (blankVarargs(tags)) { return this; } - return and(make(tags)); + return and(toTags(tags)); } /** @@ -214,7 +222,7 @@ public Tags and(@Nullable Iterable tags) { if (this.length == 0) { return Tags.of(tags); } - return merged(Tags.of(tags)); + return merge(Tags.of(tags)); } @Override @@ -323,10 +331,10 @@ else if (tags instanceof Tags) { } else if (tags instanceof Collection) { Collection tagsCollection = (Collection) tags; - return make(tagsCollection.toArray(new Tag[0])); + return toTags(tagsCollection.toArray(EMPTY_TAG_ARRAY)); } else { - return make(StreamSupport.stream(tags.spliterator(), false).toArray(Tag[]::new)); + return toTags(StreamSupport.stream(tags.spliterator(), false).toArray(Tag[]::new)); } } @@ -358,7 +366,7 @@ public static Tags of(@Nullable String... keyValues) { for (int i = 0; i < keyValues.length; i += 2) { tags[i / 2] = Tag.of(keyValues[i], keyValues[i + 1]); } - return make(tags); + return toTags(tags); } private static boolean blankVarargs(@Nullable Object[] args) { From e545f6c809461c01166218ef304c7d33a6e701d7 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 21 Nov 2024 08:59:22 +0900 Subject: [PATCH 2/2] Upgrade to Gradle Wrapper 8.11.1 (#5695) --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 65102038b1..d309848245 100644 --- a/build.gradle +++ b/build.gradle @@ -363,7 +363,7 @@ nexusPublishing { } wrapper { - gradleVersion = '8.11' + gradleVersion = '8.11.1' } defaultTasks 'build' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 94113f200e..e2847c8200 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME