From 47677592a0ddebca92defd8c63fc1be8ce620f74 Mon Sep 17 00:00:00 2001 From: Cedric Date: Wed, 18 Oct 2023 21:29:25 +0200 Subject: [PATCH] ref: better code performance --- .../signals/handler/NameHandler.java | 37 ++++++++---------- .../signals/handler/SignalStateHandler.java | 39 ++++++++++--------- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/troblecodings/signals/handler/NameHandler.java b/src/main/java/com/troblecodings/signals/handler/NameHandler.java index 61ed3d898..75bd39317 100644 --- a/src/main/java/com/troblecodings/signals/handler/NameHandler.java +++ b/src/main/java/com/troblecodings/signals/handler/NameHandler.java @@ -159,12 +159,10 @@ public static void onWorldSave(final WorldEvent.Save event) { synchronized (ALL_NAMES) { map = ImmutableMap.copyOf(ALL_NAMES); } - new Thread(() -> { - synchronized (ALL_LEVEL_FILES) { - map.entrySet().stream().filter(entry -> entry.getKey().world.equals(world)) - .forEach(entry -> createToFile(entry.getKey(), entry.getValue())); - } - }, "OSNameHandler:Save").start(); + IO_SERVICE.execute(() -> { + map.entrySet().stream().filter(entry -> entry.getKey().world.equals(world)) + .forEach(entry -> createToFile(entry.getKey(), entry.getValue())); + }); } @SubscribeEvent @@ -201,16 +199,6 @@ public static void onChunkWatch(final ChunkWatchEvent.Watch event) { return; final EntityPlayer player = event.getPlayer(); final List states = new ArrayList<>(); - synchronized (ALL_LEVEL_FILES) { - if (!ALL_LEVEL_FILES.containsKey(world)) { - ALL_LEVEL_FILES.put(world, - new NameHandlerFile(Paths.get("osfiles/namefiles/" - + ((WorldServer) world).getMinecraftServer().getName() - .replace(":", "").replace("/", "").replace("\\", "") - + "/" + ((WorldServer) world).provider.getDimensionType().getName() - .replace(":", "")))); - } - } chunk.getTileEntityMap().forEach((pos, tile) -> { if (tile instanceof SignalTileEntity || tile instanceof RedstoneIOTileEntity) { final StateInfo info = new StateInfo(world, pos); @@ -262,14 +250,21 @@ private static void loadNames(final List infos, } LOAD_COUNTER.put(info, 1); String name; + NameHandlerFile file; synchronized (ALL_LEVEL_FILES) { - final NameHandlerFile file = ALL_LEVEL_FILES.get(info.world); - if (file == null) - return; - synchronized (file) { - name = file.getString(info.pos); + file = ALL_LEVEL_FILES.get(info.world); + if (file == null) { + file = new NameHandlerFile(Paths.get("osfiles/namefiles/" + + ((WorldServer) info.world).getMinecraftServer().getName() + .replace(":", "").replace("/", "").replace("\\", "") + + "/" + ((WorldServer) info.world).provider.getDimensionType() + .getName().replace(":", ""))); + ALL_LEVEL_FILES.put(info.world, file); } } + synchronized (file) { + name = file.getString(info.pos); + } synchronized (ALL_NAMES) { ALL_NAMES.put(info, name); } diff --git a/src/main/java/com/troblecodings/signals/handler/SignalStateHandler.java b/src/main/java/com/troblecodings/signals/handler/SignalStateHandler.java index 1ad469130..29ab10c2a 100644 --- a/src/main/java/com/troblecodings/signals/handler/SignalStateHandler.java +++ b/src/main/java/com/troblecodings/signals/handler/SignalStateHandler.java @@ -145,12 +145,17 @@ private static void createToFile(final SignalStateInfo info, final Map states) { if (states == null) return; - final SignalStateFile file; + SignalStateFile file; synchronized (ALL_LEVEL_FILES) { file = ALL_LEVEL_FILES.get(info.world); if (file == null) { - return; + file = new SignalStateFile(Paths.get("osfiles/signalfiles/" + + ((WorldServer) info.world).getMinecraftServer().getName().replace(":", "") + .replace("/", "").replace("\\", "") + + "/" + ((WorldServer) info.world).provider.getDimensionType().getName() + .replace(":", ""))); } + ALL_LEVEL_FILES.put(info.world, file); } SignalStatePos pos = file.find(info.pos); if (pos == null) { @@ -200,6 +205,10 @@ 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); } } @@ -225,9 +234,15 @@ private static Map readAndSerialize(final SignalStateInfo st SignalStateFile file; synchronized (ALL_LEVEL_FILES) { file = ALL_LEVEL_FILES.get(stateInfo.world); + if (file == null) { + file = new SignalStateFile(Paths.get("osfiles/signalfiles/" + + ((WorldServer) stateInfo.world).getMinecraftServer().getName() + .replace(":", "").replace("/", "").replace("\\", "") + + "/" + ((WorldServer) stateInfo.world).provider.getDimensionType() + .getName().replace(":", ""))); + } + ALL_LEVEL_FILES.put(stateInfo.world, file); } - if (file == null) - return new HashMap<>(); SignalStatePos pos = file.find(stateInfo.pos); if (pos == null) { if (stateInfo.world.isRemote) { @@ -264,10 +279,8 @@ public static void onWorldSave(final WorldEvent.Save save) { maps = ImmutableMap.copyOf(CURRENTLY_LOADED_STATES); } IO_SERVICE.execute(() -> { - synchronized (ALL_LEVEL_FILES) { - maps.entrySet().stream().filter(entry -> entry.getKey().world.equals(world)) - .forEach(entry -> createToFile(entry.getKey(), entry.getValue())); - } + maps.entrySet().stream().filter(entry -> entry.getKey().world.equals(world)) + .forEach(entry -> createToFile(entry.getKey(), entry.getValue())); }); } @@ -350,16 +363,6 @@ public static void onChunkWatch(final ChunkWatchEvent.Watch event) { final EntityPlayer player = event.getPlayer(); final List states = new ArrayList<>(); - synchronized (ALL_LEVEL_FILES) { - if (!ALL_LEVEL_FILES.containsKey(world)) { - ALL_LEVEL_FILES.put(world, - new SignalStateFile(Paths.get("osfiles/signalfiles/" - + ((WorldServer) world).getMinecraftServer().getName() - .replace(":", "").replace("/", "").replace("\\", "") - + "/" + ((WorldServer) world).provider.getDimensionType().getName() - .replace(":", "")))); - } - } chunk.getTileEntityMap().forEach((pos, tile) -> { if (tile instanceof SignalTileEntity) { final SignalTileEntity signalTile = (SignalTileEntity) tile;