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;
}
}

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