diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/Model.java b/smithy-model/src/main/java/software/amazon/smithy/model/Model.java index 72240b67b2d..cd599e3e3c0 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/Model.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/Model.java @@ -18,7 +18,6 @@ import java.util.AbstractSet; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -89,8 +88,7 @@ public final class Model implements ToSmithyBuilder { private final Map, Set> cachedTypes = new ConcurrentHashMap<>(); /** Cache of computed {@link KnowledgeIndex} instances. */ - private final Map, KnowledgeIndex> blackboard - = new ConcurrentSkipListMap<>(Comparator.comparing(Class::getCanonicalName)); + private final Map blackboard = new ConcurrentSkipListMap<>(); /** Lazily computed trait mappings. */ private volatile TraitCache traitCache; @@ -882,7 +880,7 @@ public T getKnowledge(Class type) { */ @SuppressWarnings("unchecked") public T getKnowledge(Class type, Function constructor) { - return (T) blackboard.computeIfAbsent(type, t -> constructor.apply(this)); + return (T) blackboard.computeIfAbsent(type.getName(), t -> constructor.apply(this)); } /** diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/Shape.java b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/Shape.java index b7ee68f2512..dc37df42a5c 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/Shape.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/Shape.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.TreeSet; @@ -55,6 +56,7 @@ public abstract class Shape implements FromSourceLocation, Tagged, ToShapeId, Co private final Map mixins; private final transient SourceLocation source; private transient List memberNames; + private int hash; /** * This class is package-private, which means that all subclasses of this @@ -767,7 +769,14 @@ public final String toString() { @Override public int hashCode() { - return getId().hashCode() + 3 * getType().hashCode(); + int h = hash; + + if (h == 0) { + h = Objects.hash(getType(), getId()); + hash = h; + } + + return h; } @Override @@ -780,6 +789,7 @@ public boolean equals(Object o) { Shape other = (Shape) o; return getType() == other.getType() + && hashCode() == other.hashCode() // take advantage of hashcode caching && getId().equals(other.getId()) && traits.equals(other.traits) && mixins.equals(other.mixins) diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/traits/AbstractTrait.java b/smithy-model/src/main/java/software/amazon/smithy/model/traits/AbstractTrait.java index 82940938021..2317d745601 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/traits/AbstractTrait.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/traits/AbstractTrait.java @@ -87,12 +87,16 @@ public String toString() { @Override public boolean equals(Object other) { - if (!(other instanceof Trait)) { + if (other == this) { + return true; + } else if (!(other instanceof Trait)) { return false; + } else if (hashCode() != other.hashCode()) { // take advantage of hashcode caching + return false; + } else { + Trait b = (Trait) other; + return toShapeId().equals(b.toShapeId()) && toNode().equals(b.toNode()); } - - Trait b = (Trait) other; - return this == other || (toShapeId().equals(b.toShapeId()) && toNode().equals(b.toNode())); } @Override diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/validators/TraitTargetValidator.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/validators/TraitTargetValidator.java index 7c9c7b176b1..2e6eb47850e 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/validation/validators/TraitTargetValidator.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/validators/TraitTargetValidator.java @@ -93,7 +93,6 @@ private void validateTraitTargets( Selector selector, List traits ) { - selector.shapes(model); Set matches = selector.select(model); for (ShapeId traitId : traits) {