From 6cf7021d2b98a5a4aface0e7d4266a09e0659e9c Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 19 Oct 2023 13:32:58 +0200 Subject: [PATCH] ref: better code performance --- .../signals/core/LinkedPositions.java | 2 +- .../signals/handler/NameHandler.java | 25 ++++++++-------- .../signals/handler/SignalStateHandler.java | 29 +++++++++---------- .../signals/tileentitys/SignalTileEntity.java | 1 - 4 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/troblecodings/signals/core/LinkedPositions.java b/src/main/java/com/troblecodings/signals/core/LinkedPositions.java index fb5ac1fda..94ce7a411 100644 --- a/src/main/java/com/troblecodings/signals/core/LinkedPositions.java +++ b/src/main/java/com/troblecodings/signals/core/LinkedPositions.java @@ -39,7 +39,7 @@ public LinkedPositions(final BlockPos thisPos) { } private final SignalStateListener listener = (stateInfo, properties, changed) -> { - if (changed.equals(ChangedState.UPDATED) || changed.equals(ChangedState.ADDED_TO_CACHE)) { + if (changed.equals(ChangedState.ADDED_TO_CACHE)) { loadPossibleSubsidiaires(stateInfo, properties); } else if (changed.equals(ChangedState.REMOVED_FROM_FILE)) { possibleSubsidiaries.remove(stateInfo.pos); diff --git a/src/main/java/com/troblecodings/signals/handler/NameHandler.java b/src/main/java/com/troblecodings/signals/handler/NameHandler.java index 772f730fe..ccf9bda3b 100644 --- a/src/main/java/com/troblecodings/signals/handler/NameHandler.java +++ b/src/main/java/com/troblecodings/signals/handler/NameHandler.java @@ -43,7 +43,8 @@ public final class NameHandler implements INetworkSync { - private static ExecutorService IO_SERVICE = Executors.newFixedThreadPool(6); + private static ExecutorService WRITE_SERVICE = Executors.newFixedThreadPool(4); + private static ExecutorService READ_SERVICE = Executors.newFixedThreadPool(10); private static final Map ALL_NAMES = new HashMap<>(); private static final Map ALL_LEVEL_FILES = new HashMap<>(); private static final Map LOAD_COUNTER = new HashMap<>(); @@ -57,16 +58,16 @@ public static void init() { @EventHandler public static void onServerStop(final FMLServerStoppingEvent event) { - synchronized (ALL_NAMES) { - ALL_NAMES.forEach((info, name) -> createToFile(info, name)); - } - IO_SERVICE.shutdown(); + READ_SERVICE.shutdown(); + WRITE_SERVICE.shutdown(); try { - IO_SERVICE.awaitTermination(10, TimeUnit.MINUTES); + READ_SERVICE.awaitTermination(10, TimeUnit.DAYS); + WRITE_SERVICE.awaitTermination(10, TimeUnit.DAYS); } catch (final InterruptedException e) { e.printStackTrace(); } - IO_SERVICE = Executors.newFixedThreadPool(6); + READ_SERVICE = Executors.newFixedThreadPool(10); + WRITE_SERVICE = Executors.newFixedThreadPool(4); } public static void registerToNetworkChannel(final Object obj) { @@ -159,7 +160,7 @@ public static void onWorldSave(final WorldEvent.Save event) { synchronized (ALL_NAMES) { map = ImmutableMap.copyOf(ALL_NAMES); } - IO_SERVICE.execute(() -> { + WRITE_SERVICE.execute(() -> { map.entrySet().stream().filter(entry -> entry.getKey().world.equals(world)) .forEach(entry -> createToFile(entry.getKey(), entry.getValue())); }); @@ -238,9 +239,9 @@ public static void onPlayerJoin(final PlayerEvent.PlayerLoggedInEvent event) { private static void loadNames(final List infos, final @Nullable EntityPlayer player) { - if (infos == null || infos.isEmpty() || IO_SERVICE.isShutdown()) + if (infos == null || infos.isEmpty() || READ_SERVICE.isShutdown()) return; - IO_SERVICE.execute(() -> { + READ_SERVICE.execute(() -> { infos.forEach(info -> { synchronized (LOAD_COUNTER) { Integer count = LOAD_COUNTER.get(info); @@ -279,9 +280,9 @@ private static void loadNames(final List infos, } private static void unloadNames(final List infos) { - if (infos == null || infos.isEmpty() || IO_SERVICE.isShutdown()) + if (infos == null || infos.isEmpty() || WRITE_SERVICE.isShutdown()) return; - IO_SERVICE.execute(() -> { + WRITE_SERVICE.execute(() -> { infos.forEach(info -> { synchronized (LOAD_COUNTER) { Integer count = LOAD_COUNTER.get(info); diff --git a/src/main/java/com/troblecodings/signals/handler/SignalStateHandler.java b/src/main/java/com/troblecodings/signals/handler/SignalStateHandler.java index 463bfe1df..b8f77fa24 100644 --- a/src/main/java/com/troblecodings/signals/handler/SignalStateHandler.java +++ b/src/main/java/com/troblecodings/signals/handler/SignalStateHandler.java @@ -50,7 +50,8 @@ public final class SignalStateHandler implements INetworkSync { private SignalStateHandler() { } - private static ExecutorService IO_SERVICE = Executors.newFixedThreadPool(6); + private static ExecutorService READ_SERVICE = Executors.newFixedThreadPool(10); + private static ExecutorService WRITE_SERVICE = Executors.newFixedThreadPool(4); private static final Map> CURRENTLY_LOADED_STATES = new HashMap<>(); private static final Map ALL_LEVEL_FILES = new HashMap<>(); private static final Map>> SIGNAL_COUNTER = new HashMap<>(); @@ -67,16 +68,16 @@ public static void registerToNetworkChannel(final Object object) { } public static void onServerStop(final FMLServerStoppingEvent event) { - synchronized (CURRENTLY_LOADED_STATES) { - CURRENTLY_LOADED_STATES.forEach((info, map) -> createToFile(info, map)); - } - IO_SERVICE.shutdown(); + READ_SERVICE.shutdown(); + WRITE_SERVICE.shutdown(); try { - IO_SERVICE.awaitTermination(10, TimeUnit.MINUTES); + READ_SERVICE.awaitTermination(10, TimeUnit.DAYS); + WRITE_SERVICE.awaitTermination(10, TimeUnit.DAYS); } catch (final InterruptedException e) { e.printStackTrace(); } - IO_SERVICE = Executors.newFixedThreadPool(6); + READ_SERVICE = Executors.newFixedThreadPool(10); + WRITE_SERVICE = Executors.newFixedThreadPool(4); } public static void createStates(final SignalStateInfo info, @@ -206,10 +207,6 @@ public static Map getStates(final SignalStateInfo info) { if (info.world.isRemote) { return new HashMap<>(); } - synchronized (SIGNAL_COUNTER) { - if (SIGNAL_COUNTER.containsKey(info)) - return new HashMap<>(); - } return readAndSerialize(info); } } @@ -279,7 +276,7 @@ public static void onWorldSave(final WorldEvent.Save save) { synchronized (CURRENTLY_LOADED_STATES) { maps = ImmutableMap.copyOf(CURRENTLY_LOADED_STATES); } - IO_SERVICE.execute(() -> { + WRITE_SERVICE.execute(() -> { maps.entrySet().stream().filter(entry -> entry.getKey().world.equals(world)) .forEach(entry -> createToFile(entry.getKey(), entry.getValue())); }); @@ -412,9 +409,9 @@ public static void loadSignal(final StateLoadHolder info, final @Nullable Entity public static void loadSignals(final List signals, final @Nullable EntityPlayer player) { - if (signals == null || signals.isEmpty() || IO_SERVICE.isShutdown()) + if (signals == null || signals.isEmpty() || READ_SERVICE.isShutdown()) return; - IO_SERVICE.execute(() -> { + READ_SERVICE.execute(() -> { signals.forEach(info -> { synchronized (ALL_LEVEL_FILES) { if (!ALL_LEVEL_FILES.containsKey(info.info.world)) { @@ -457,9 +454,9 @@ public static void unloadSignal(final StateLoadHolder info) { } public static void unloadSignals(final List signals) { - if (signals == null || signals.isEmpty() || IO_SERVICE.isShutdown()) + if (signals == null || signals.isEmpty() || WRITE_SERVICE.isShutdown()) return; - IO_SERVICE.execute(() -> { + WRITE_SERVICE.execute(() -> { signals.forEach(info -> { synchronized (SIGNAL_COUNTER) { final List> holders = SIGNAL_COUNTER.getOrDefault(info.info, diff --git a/src/main/java/com/troblecodings/signals/tileentitys/SignalTileEntity.java b/src/main/java/com/troblecodings/signals/tileentitys/SignalTileEntity.java index 318d37fbf..3ab184105 100644 --- a/src/main/java/com/troblecodings/signals/tileentitys/SignalTileEntity.java +++ b/src/main/java/com/troblecodings/signals/tileentitys/SignalTileEntity.java @@ -22,7 +22,6 @@ public class SignalTileEntity extends SyncableTileEntity implements NamableWrapp private final SignalStateListener listener = (info, states, changed) -> { switch (changed) { - case ADDED_TO_FILE: case ADDED_TO_CACHE: { properties.clear(); properties.putAll(SignalStateHandler.getStates(info));