Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minor perf improvements #1318

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,8 +88,7 @@ public final class Model implements ToSmithyBuilder<Model> {
private final Map<Class<? extends Shape>, Set<? extends Shape>> cachedTypes = new ConcurrentHashMap<>();

/** Cache of computed {@link KnowledgeIndex} instances. */
private final Map<Class<? extends KnowledgeIndex>, KnowledgeIndex> blackboard
= new ConcurrentSkipListMap<>(Comparator.comparing(Class::getCanonicalName));
private final Map<String, KnowledgeIndex> blackboard = new ConcurrentSkipListMap<>();

/** Lazily computed trait mappings. */
private volatile TraitCache traitCache;
Expand Down Expand Up @@ -882,7 +880,7 @@ public <T extends KnowledgeIndex> T getKnowledge(Class<T> type) {
*/
@SuppressWarnings("unchecked")
public <T extends KnowledgeIndex> T getKnowledge(Class<T> type, Function<Model, T> constructor) {
return (T) blackboard.computeIfAbsent(type, t -> constructor.apply(this));
return (T) blackboard.computeIfAbsent(type.getName(), t -> constructor.apply(this));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +56,7 @@ public abstract class Shape implements FromSourceLocation, Tagged, ToShapeId, Co
private final Map<ShapeId, Shape> mixins;
private final transient SourceLocation source;
private transient List<String> memberNames;
private int hash;

/**
* This class is package-private, which means that all subclasses of this
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ private void validateTraitTargets(
Selector selector,
List<ShapeId> traits
) {
selector.shapes(model);
Set<Shape> matches = selector.select(model);

for (ShapeId traitId : traits) {
Expand Down