Skip to content
Open
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 @@ -57,6 +57,16 @@ public ApplicableRegionSet queryContains(RegionManager manager, Location locatio
checkNotNull(option);

CacheKey key = new CacheKey(location);

// Fast path: avoid locking if the result is already cached
Map<QueryOption, ApplicableRegionSet> existing = cache.get(key);
if (existing != null) {
ApplicableRegionSet result = existing.get(option);
if (result != null) {
return result;
}
}
Comment on lines +61 to +68

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, can we split the createCache method into two parts, one that returns the appropriate map, and one that fills in the information with computeIfAbsent or whatever is typically used in the != null branch? That way, instead of still having lock contention for every initial query, we get out of the computeIfAbsent quickly and do a follow-up one only on the smaller map if necessary.

It might not be perfectly that clean, but I think we should avoid having expensive logic inside a compute or this fix won't help that much.


return cache.compute(key, (k, v) -> option.createCache(manager, location, v)).get(option);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
import com.sk89q.worldguard.protection.util.RegionCollectionConsumer;

import java.util.Collection;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -513,7 +513,7 @@ public List<ProtectedRegion> constructResult(Set<ProtectedRegion> applicable) {
@Override
Map<QueryOption, ApplicableRegionSet> createCache(RegionManager manager, Location location, Map<QueryOption, ApplicableRegionSet> cache) {
if (cache == null) {
cache = new EnumMap<>(QueryOption.class);
cache = new ConcurrentHashMap<>(QueryOption.values().length);
cache.put(QueryOption.NONE, manager.getApplicableRegions(location.toVector().toBlockPoint(), QueryOption.NONE));
}

Expand All @@ -530,7 +530,7 @@ Map<QueryOption, ApplicableRegionSet> createCache(RegionManager manager, Locatio
@Override
Map<QueryOption, ApplicableRegionSet> createCache(RegionManager manager, Location location, Map<QueryOption, ApplicableRegionSet> cache) {
if (cache == null) {
Map<QueryOption, ApplicableRegionSet> newCache = new EnumMap<>(QueryOption.class);
Map<QueryOption, ApplicableRegionSet> newCache = new ConcurrentHashMap<>(QueryOption.values().length);
ApplicableRegionSet result = manager.getApplicableRegions(location.toVector().toBlockPoint(), QueryOption.SORT);
newCache.put(QueryOption.NONE, result);
newCache.put(QueryOption.SORT, result);
Expand All @@ -551,7 +551,7 @@ Map<QueryOption, ApplicableRegionSet> createCache(RegionManager manager, Locatio
@Override
Map<QueryOption, ApplicableRegionSet> createCache(RegionManager manager, Location location, Map<QueryOption, ApplicableRegionSet> cache) {
if (cache == null) {
Map<QueryOption, ApplicableRegionSet> newCache = new EnumMap<>(QueryOption.class);
Map<QueryOption, ApplicableRegionSet> newCache = new ConcurrentHashMap<>(QueryOption.values().length);
ApplicableRegionSet noParResult = manager.getApplicableRegions(location.toVector().toBlockPoint(), QueryOption.NONE);
Set<ProtectedRegion> noParRegions = noParResult.getRegions();
Set<ProtectedRegion> regions = new HashSet<>();
Expand Down