Skip to content

Commit

Permalink
cache hash code for Query.In (#1106)
Browse files Browse the repository at this point in the history
In some cases it can be a big set of values and recomputing
in the query index to do map looks has a high overhead.
  • Loading branch information
brharrington authored Jan 4, 2024
1 parent 4c78bf8 commit 8061303
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,14 @@ final class In implements KeyQuery {
private final String k;
private final Set<String> vs;

private final int cachedHashCode;

/** Create a new instance. */
In(String k, Set<String> vs) {
Preconditions.checkArg(!vs.isEmpty(), "list of values for :in cannot be empty");
this.k = Preconditions.checkNotNull(k, "k");
this.vs = Preconditions.checkNotNull(vs, "vs");
this.cachedHashCode = calculateHashCode();
}

@Override public String key() {
Expand Down Expand Up @@ -618,6 +621,10 @@ public Set<String> values() {
}

@Override public int hashCode() {
return cachedHashCode;
}

private int calculateHashCode() {
int result = k.hashCode();
result = 31 * result + vs.hashCode();
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public void inEqualsContract() {
EqualsVerifier
.forClass(Query.In.class)
.suppress(Warning.NULL_FIELDS)
.withCachedHashCode(
"cachedHashCode",
"calculateHashCode",
new Query.In("k", Collections.singleton("v")))
.verify();
}

Expand Down

0 comments on commit 8061303

Please sign in to comment.