diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-10-18 17:28:43 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-10-18 17:28:43 +0200 |
commit | 03a634837a34ee12dd13b1f355a8ee69c3302c15 (patch) | |
tree | 28c6d797052d44e23743841c751db9f847a07c9c /container-core | |
parent | 7a8571355a737aab02934ad5cf9fc8521c429b54 (diff) |
Precompute hashCode as it will be used multiple times.
Diffstat (limited to 'container-core')
4 files changed, 12 insertions, 36 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/core/HandlerMetricContextUtil.java b/container-core/src/main/java/com/yahoo/container/core/HandlerMetricContextUtil.java index 5077ef3e849..7a9266446a1 100644 --- a/container-core/src/main/java/com/yahoo/container/core/HandlerMetricContextUtil.java +++ b/container-core/src/main/java/com/yahoo/container/core/HandlerMetricContextUtil.java @@ -5,7 +5,6 @@ import com.yahoo.jdisc.Metric; import com.yahoo.jdisc.Request; import com.yahoo.jdisc.application.BindingMatch; import com.yahoo.jdisc.application.UriPattern; -import com.yahoo.jdisc.handler.ResponseHandler; import java.net.URI; import java.util.HashMap; diff --git a/container-core/src/main/java/com/yahoo/metrics/simple/Identifier.java b/container-core/src/main/java/com/yahoo/metrics/simple/Identifier.java index ead7d1c88ca..ba0ee582fd6 100644 --- a/container-core/src/main/java/com/yahoo/metrics/simple/Identifier.java +++ b/container-core/src/main/java/com/yahoo/metrics/simple/Identifier.java @@ -12,19 +12,17 @@ public class Identifier { private final String name; private final Point location; + private final int hashCode; public Identifier(String name, Point location) { this.name = (name == null ? "" : name); this.location = (location == null ? Point.emptyPoint() : location); + this.hashCode = this.location.hashCode() * 31 + this.name.hashCode(); } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + location.hashCode(); - result = prime * result + name.hashCode(); - return result; + return hashCode; } @Override @@ -34,13 +32,7 @@ public class Identifier { if (getClass() != obj.getClass()) return false; Identifier other = (Identifier) obj; - if (!location.equals(other.location)) { - return false; - } - if (!name.equals(other.name)) { - return false; - } - return true; + return location.equals(other.location) && name.equals(other.name); } @Override diff --git a/container-core/src/main/java/com/yahoo/metrics/simple/Point.java b/container-core/src/main/java/com/yahoo/metrics/simple/Point.java index f9ea3849ddc..6cfaa1fb0c1 100644 --- a/container-core/src/main/java/com/yahoo/metrics/simple/Point.java +++ b/container-core/src/main/java/com/yahoo/metrics/simple/Point.java @@ -20,6 +20,7 @@ public final class Point implements Context { private final Value[] location; private final String[] dimensions; + private final int hashCode; public Point(Map<String, ?> properties) { this(buildParameters(properties)); @@ -38,6 +39,7 @@ public final class Point implements Context { Point(String[] dimensions, Value[] location) { this.dimensions = dimensions; this.location = location; + this.hashCode = Arrays.hashCode(location) * 31 + Arrays.hashCode(dimensions); } private static final Point theEmptyPoint = new Point(new String[0], new Value[0]); @@ -58,32 +60,16 @@ public final class Point implements Context { @Override public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; Point other = (Point) obj; - if (!Arrays.equals(dimensions, other.dimensions)) { - return false; - } - if (!Arrays.equals(location, other.location)) { - return false; - } - return true; + return Arrays.equals(dimensions, other.dimensions) && Arrays.equals(location, other.location); } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Arrays.hashCode(dimensions); - result = prime * result + Arrays.hashCode(location); - return result; + return hashCode; } @Override diff --git a/container-core/src/main/java/com/yahoo/metrics/simple/jdisc/SimpleMetricConsumer.java b/container-core/src/main/java/com/yahoo/metrics/simple/jdisc/SimpleMetricConsumer.java index bea17bd91bc..3a40958f0b1 100644 --- a/container-core/src/main/java/com/yahoo/metrics/simple/jdisc/SimpleMetricConsumer.java +++ b/container-core/src/main/java/com/yahoo/metrics/simple/jdisc/SimpleMetricConsumer.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.metrics.simple.jdisc; -import java.util.HashMap; import java.util.Map; import com.yahoo.jdisc.Metric.Context; @@ -47,7 +46,7 @@ public class SimpleMetricConsumer implements MetricConsumer { @Override public Context createContext(Map<String, ?> properties) { if (properties == null) - properties = new HashMap<>(); + return Point.emptyPoint(); return new Point(properties); } |