mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-28 05:35:26 +01:00
Optimize ImmutableContextSet#equals by comparing the hashcodes (constant time lookup)
This commit is contained in:
parent
e8c4a55dc3
commit
c19ef84116
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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 + ")";
|
||||
|
Loading…
Reference in New Issue
Block a user