From e7f85e67f61acdffd0575df02be3015a40fcf6aa Mon Sep 17 00:00:00 2001 From: Myles Date: Tue, 10 May 2016 10:26:49 +0100 Subject: [PATCH] Change our data structures to use concurrent equivalents, netty is multi-threaded. --- .../ViaVersion/api/protocol/ProtocolPipeline.java | 10 +++++----- .../protocol1_9to1_8/storage/EntityTracker.java | 14 ++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java index dd749e645..fd97e4620 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java @@ -11,12 +11,12 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo; import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.logging.Level; public class ProtocolPipeline extends Protocol { - LinkedList protocolList; + List protocolList; private UserConnection userConnection; public ProtocolPipeline(UserConnection userConnection) { @@ -26,9 +26,9 @@ public class ProtocolPipeline extends Protocol { @Override protected void registerPackets() { - protocolList = new LinkedList<>(); + protocolList = new CopyOnWriteArrayList<>(); // This is a pipeline so we register basic pipes - protocolList.addLast(ProtocolRegistry.BASE_PROTOCOL); + protocolList.add(ProtocolRegistry.BASE_PROTOCOL); } @Override @@ -54,7 +54,7 @@ public class ProtocolPipeline extends Protocol { */ public void add(Protocol protocol) { if (protocolList != null) { - protocolList.addLast(protocol); + protocolList.add(protocol); protocol.init(userConnection); } else { throw new NullPointerException("Tried to add protocol to early"); diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java index 3caf9e55c..65d0d6155 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java @@ -2,6 +2,7 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.storage; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.google.common.collect.Sets; import io.netty.buffer.ByteBuf; import lombok.Getter; import lombok.Setter; @@ -26,16 +27,17 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.chat.GameMode; import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.NewType; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; @Getter public class EntityTracker extends StoredObject { - private final Map uuidMap = new HashMap<>(); - private final Map clientEntityTypes = new HashMap<>(); - private final Map vehicleMap = new HashMap<>(); - private final Map bossBarMap = new HashMap<>(); - private final Set validBlocking = new HashSet<>(); - private final Set knownHolograms = new HashSet<>(); + private final Map uuidMap = new ConcurrentHashMap<>(); + private final Map clientEntityTypes = new ConcurrentHashMap<>(); + private final Map vehicleMap = new ConcurrentHashMap<>(); + private final Map bossBarMap = new ConcurrentHashMap<>(); + private final Set validBlocking = Sets.newConcurrentHashSet(); + private final Set knownHolograms = Sets.newConcurrentHashSet(); private final Cache blockInteractions = CacheBuilder.newBuilder().maximumSize(10).expireAfterAccess(250, TimeUnit.MILLISECONDS).build(); @Setter private boolean blocking = false;