Abandon TreeSet in ApplicableRegionSet for performance reasons.

This commit is contained in:
sk89q 2014-08-17 19:18:21 -07:00
parent d43eb3bc34
commit c43a24d78f
5 changed files with 34 additions and 16 deletions

View File

@ -19,6 +19,7 @@
package com.sk89q.worldguard.protection;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ObjectArrays;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.protection.association.RegionAssociable;
@ -35,9 +36,8 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldguard.protection.flags.StateFlag.test;
@ -56,7 +56,7 @@ public class ApplicableRegionSet implements Iterable<ProtectedRegion> {
*/
private static final ApplicableRegionSet EMPTY = new ApplicableRegionSet(Collections.<ProtectedRegion>emptyList(), null);
private final SortedSet<ProtectedRegion> applicable;
private final List<ProtectedRegion> applicable;
private final FlagValueCalculator flagValueCalculator;
/**
@ -67,8 +67,8 @@ public class ApplicableRegionSet implements Iterable<ProtectedRegion> {
* @param applicable the regions contained in this set
* @param globalRegion the global region, set aside for special handling.
*/
public ApplicableRegionSet(Collection<ProtectedRegion> applicable, @Nullable ProtectedRegion globalRegion) {
this(new TreeSet<ProtectedRegion>(checkNotNull(applicable)), globalRegion);
public ApplicableRegionSet(List<ProtectedRegion> applicable, @Nullable ProtectedRegion globalRegion) {
this(applicable, globalRegion, false);
}
/**
@ -76,9 +76,13 @@ public ApplicableRegionSet(Collection<ProtectedRegion> applicable, @Nullable Pro
*
* @param applicable the regions contained in this set
* @param globalRegion the global region, set aside for special handling.
* @param sorted true if the list is already sorted
*/
public ApplicableRegionSet(SortedSet<ProtectedRegion> applicable, @Nullable ProtectedRegion globalRegion) {
private ApplicableRegionSet(List<ProtectedRegion> applicable, @Nullable ProtectedRegion globalRegion, boolean sorted) {
checkNotNull(applicable);
if (!sorted) {
Collections.sort(applicable);
}
this.applicable = applicable;
this.flagValueCalculator = new FlagValueCalculator(applicable, globalRegion);
}
@ -342,7 +346,7 @@ public int size() {
* @return a set of regions
*/
public Set<ProtectedRegion> getRegions() {
return Collections.unmodifiableSet(applicable);
return ImmutableSet.copyOf(applicable);
}
@Override
@ -357,4 +361,16 @@ public static ApplicableRegionSet getEmpty() {
return EMPTY;
}
/**
* Create a new instance using a list of regions that is known to
* already be sorted by priority descending.
*
* @param regions a list of regions
* @param globalRegion a global region
* @return
*/
public static ApplicableRegionSet fromSortedList(List<ProtectedRegion> regions, @Nullable ProtectedRegion globalRegion) {
return new ApplicableRegionSet(regions, globalRegion, true);
}
}

View File

@ -34,9 +34,9 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import static com.google.common.base.Preconditions.checkNotNull;
@ -52,17 +52,17 @@
*/
public class FlagValueCalculator {
private final SortedSet<ProtectedRegion> regions;
private final List<ProtectedRegion> regions;
@Nullable
private final ProtectedRegion globalRegion;
/**
* Create a new instance.
*
* @param regions a list of applicable regions
* @param regions a list of applicable regions that <strong>must be sorted by priority descending</strong>
* @param globalRegion an optional global region (null to not use one)
*/
public FlagValueCalculator(SortedSet<ProtectedRegion> regions, @Nullable ProtectedRegion globalRegion) {
public FlagValueCalculator(List<ProtectedRegion> regions, @Nullable ProtectedRegion globalRegion) {
checkNotNull(regions);
this.regions = regions;

View File

@ -43,7 +43,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@ -289,7 +288,7 @@ public Set<ProtectedRegion> removeRegion(String id, RemovalStrategy strategy) {
public ApplicableRegionSet getApplicableRegions(Vector position) {
checkNotNull(position);
TreeSet<ProtectedRegion> regions = new TreeSet<ProtectedRegion>();
List<ProtectedRegion> regions = new ArrayList<ProtectedRegion>();
index.applyContaining(position, new RegionCollectionConsumer(regions, true));
return new ApplicableRegionSet(regions, index.get("__global__"));
}
@ -304,7 +303,7 @@ public ApplicableRegionSet getApplicableRegions(Vector position) {
public ApplicableRegionSet getApplicableRegions(ProtectedRegion region) {
checkNotNull(region);
TreeSet<ProtectedRegion> regions = new TreeSet<ProtectedRegion>();
List<ProtectedRegion> regions = new ArrayList<ProtectedRegion>();
index.applyIntersecting(region, new RegionCollectionConsumer(regions, true));
return new ApplicableRegionSet(regions, index.get("__global__"));
}

View File

@ -294,6 +294,7 @@ public void run() {
position.multiply(16).toVector(0).toBlockVector(),
position.add(1, 1).multiply(16).toVector(Integer.MAX_VALUE).toBlockVector());
index.applyIntersecting(chunkRegion, new RegionCollectionConsumer(regions, false));
Collections.sort(regions);
state.setRegions(Collections.unmodifiableList(regions));

View File

@ -26,12 +26,13 @@
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeSet;
import java.util.List;
public class MockApplicableRegionSet {
private TreeSet<ProtectedRegion> regions = new TreeSet<ProtectedRegion>();
private List<ProtectedRegion> regions = new ArrayList<ProtectedRegion>();
private ProtectedRegion global;
private int id = 0;
private int playerIndex = 0;
@ -85,6 +86,7 @@ public ApplicableRegionSet getApplicableSet() {
}
public FlagValueCalculator getFlagCalculator() {
Collections.sort(regions);
return new FlagValueCalculator(regions, global);
}