Skip to content

Commit

Permalink
ref: better code performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Oct 19, 2023
1 parent bb2d6ac commit 6cf7021
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/troblecodings/signals/handler/NameHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<StateInfo, String> ALL_NAMES = new HashMap<>();
private static final Map<World, NameHandlerFile> ALL_LEVEL_FILES = new HashMap<>();
private static final Map<StateInfo, Integer> LOAD_COUNTER = new HashMap<>();
Expand All @@ -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) {
Expand Down Expand Up @@ -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()));
});
Expand Down Expand Up @@ -238,9 +239,9 @@ public static void onPlayerJoin(final PlayerEvent.PlayerLoggedInEvent event) {

private static void loadNames(final List<StateInfo> 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);
Expand Down Expand Up @@ -279,9 +280,9 @@ private static void loadNames(final List<StateInfo> infos,
}

private static void unloadNames(final List<StateInfo> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SignalStateInfo, Map<SEProperty, String>> CURRENTLY_LOADED_STATES = new HashMap<>();
private static final Map<World, SignalStateFile> ALL_LEVEL_FILES = new HashMap<>();
private static final Map<SignalStateInfo, List<LoadHolder<?>>> SIGNAL_COUNTER = new HashMap<>();
Expand All @@ -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,
Expand Down Expand Up @@ -206,10 +207,6 @@ public static Map<SEProperty, String> 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);
}
}
Expand Down Expand Up @@ -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()));
});
Expand Down Expand Up @@ -412,9 +409,9 @@ public static void loadSignal(final StateLoadHolder info, final @Nullable Entity

public static void loadSignals(final List<StateLoadHolder> 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)) {
Expand Down Expand Up @@ -457,9 +454,9 @@ public static void unloadSignal(final StateLoadHolder info) {
}

public static void unloadSignals(final List<StateLoadHolder> 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<LoadHolder<?>> holders = SIGNAL_COUNTER.getOrDefault(info.info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 6cf7021

Please sign in to comment.