From 6b96b4d133528198fcaab6bdf91464b967186dba Mon Sep 17 00:00:00 2001 From: Yury Yarashevich Date: Thu, 23 May 2024 14:14:15 +0200 Subject: [PATCH] Check if tags array is already sorted set before sorting. --- .../benchmark/core/TagsBenchmark.java | 55 +++++++++++++++++++ .../benchmark/core/TagsMergeBenchmark.java | 12 ++-- .../io/micrometer/core/instrument/Tags.java | 7 ++- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/TagsBenchmark.java b/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/TagsBenchmark.java index b930006011..813f0da21c 100644 --- a/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/TagsBenchmark.java +++ b/benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/TagsBenchmark.java @@ -15,6 +15,7 @@ */ package io.micrometer.benchmark.core; +import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tags; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.runner.Runner; @@ -31,6 +32,60 @@ @OutputTimeUnit(TimeUnit.NANOSECONDS) public class TagsBenchmark { + static final Tag[] orderedTagsSet10 = new Tag[] { Tag.of("key0", "value"), Tag.of("key1", "value"), + Tag.of("key2", "value"), Tag.of("key3", "value"), Tag.of("key4", "value"), Tag.of("key5", "value"), + Tag.of("key6", "value"), Tag.of("key7", "value"), Tag.of("key8", "value"), Tag.of("key9", "value") }; + + static final Tag[] orderedTagsSet4 = new Tag[] { Tag.of("key0", "value"), Tag.of("key1", "value"), + Tag.of("key2", "value"), Tag.of("key3", "value"), }; + + static final Tag[] orderedTagsSet2 = new Tag[] { Tag.of("key0", "value"), Tag.of("key1", "value"), }; + + static final Tag[] unorderedTagsSet10 = new Tag[] { Tag.of("key1", "value"), Tag.of("key2", "value"), + Tag.of("key3", "value"), Tag.of("key4", "value"), Tag.of("key5", "value"), Tag.of("key6", "value"), + Tag.of("key7", "value"), Tag.of("key8", "value"), Tag.of("key9", "value"), Tag.of("key0", "value") }; + + static final Tag[] unorderedTagsSet4 = new Tag[] { Tag.of("key1", "value"), Tag.of("key2", "value"), + Tag.of("key3", "value"), Tag.of("key0", "value"), }; + + static final Tag[] unorderedTagsSet2 = new Tag[] { Tag.of("key1", "value"), Tag.of("key0", "value") }; + + @Threads(16) + @Benchmark + public Tags tagsOfOrderedTagsSet10() { + return Tags.of(orderedTagsSet10); + } + + @Threads(16) + @Benchmark + public Tags tagsOfOrderedTagsSet4() { + return Tags.of(orderedTagsSet4); + } + + @Threads(16) + @Benchmark + public Tags tagsOfOrderedTagsSet2() { + return Tags.of(orderedTagsSet2); + } + + @Threads(16) + @Benchmark + public Tags tagsOfUnorderedTagsSet10() { + return Tags.of(unorderedTagsSet10); + } + + @Threads(16) + @Benchmark + public Tags tagsOfUnorderedTagsSet4() { + return Tags.of(unorderedTagsSet4); + } + + @Threads(16) + @Benchmark + public Tags tagsOfUnorderedTagsSet2() { + return Tags.of(unorderedTagsSet2); + } + @Threads(16) @Benchmark public void of() { 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 d592f2bb3f..59a24cf89f 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018 VMware, Inc. + * Copyright 2024 VMware, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,12 +32,12 @@ @State(Scope.Thread) public class TagsMergeBenchmark { - 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"); + 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"); - Tags right = Tags.of("key", "value", "key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4", "key5", - "value5", "keyA", "valueA", "keyB", "valueB", "keyD", "valueD"); + static final Tags right = Tags.of("key", "value", "key1", "value1", "key2", "value2", "key3", "value3", "key4", + "value4", "key5", "value5", "keyA", "valueA", "keyB", "valueB", "keyD", "valueD"); @Threads(16) @Benchmark 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 c91c0a90d2..4a79706b83 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,8 +60,11 @@ private static boolean isSortedSet(Tag[] sortedSet, int length) { } private static Tags make(Tag[] tags) { - Arrays.sort(tags); - int len = dedup(tags); + int len = tags.length; + if (!isSortedSet(tags, len)) { + Arrays.sort(tags); + len = dedup(tags); + } return new Tags(tags, len); }