Optimize ImmutableContextSet#equals by comparing the hashcodes (constant time lookup)

This commit is contained in:
Luck 2018-11-24 01:25:02 +00:00
parent e8c4a55dc3
commit c19ef84116
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 38 additions and 18 deletions

View File

@ -26,7 +26,6 @@
package me.lucko.luckperms.api.context;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.SetMultimap;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -67,23 +66,6 @@ abstract class AbstractContextSet implements ContextSet {
return backing().size();
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof ContextSet)) return false;
final ContextSet that = (ContextSet) o;
final Multimap<String, String> otherContexts;
if (that instanceof AbstractContextSet) {
otherContexts = ((AbstractContextSet) that).backing();
} else {
otherContexts = that.toMultimap();
}
return backing().equals(otherContexts);
}
@Override
public int hashCode() {
return backing().hashCode();

View File

@ -213,6 +213,28 @@ public final class ImmutableContextSet extends AbstractContextSet implements Con
return this.map.entries().spliterator();
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof ContextSet)) return false;
final ContextSet that = (ContextSet) o;
// fast(er) path for ImmutableContextSet comparisons
if (that instanceof ImmutableContextSet) {
ImmutableContextSet immutableThat = (ImmutableContextSet) that;
if (this.hashCode != immutableThat.hashCode) return false;
}
final Multimap<String, String> thatBacking;
if (that instanceof AbstractContextSet) {
thatBacking = ((AbstractContextSet) that).backing();
} else {
thatBacking = that.toMultimap();
}
return backing().equals(thatBacking);
}
@Override
public int hashCode() {
return this.hashCode;

View File

@ -345,6 +345,22 @@ public final class MutableContextSet extends AbstractContextSet implements Conte
this.map.clear();
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof ContextSet)) return false;
final ContextSet that = (ContextSet) o;
final Multimap<String, String> thatBacking;
if (that instanceof AbstractContextSet) {
thatBacking = ((AbstractContextSet) that).backing();
} else {
thatBacking = that.toMultimap();
}
return backing().equals(thatBacking);
}
@Override
public String toString() {
return "MutableContextSet(contexts=" + this.map + ")";