Skip to content

Commit

Permalink
No longer set caffeine concurrency level
Browse files Browse the repository at this point in the history
It has no effect since Java 8 due to changes in the impl of ConcurrentHashMap.
Also, it crashes SpongeForge due to JPMS violation.
  • Loading branch information
Yeregorix committed Jan 21, 2025
1 parent 55a83d5 commit 8d8a232
Showing 1 changed file with 0 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -70,7 +69,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -96,30 +94,6 @@ public SpongeEventManager() {
this.handlersByEvent = HashMultimap.create();
this.registeredListeners = new ReferenceOpenHashSet<>();
this.checker = new ListenerChecker(ShouldFire.class);

// Caffeine offers no control over the concurrency level of the
// ConcurrentHashMap which backs the cache. By default this concurrency
// level is 16. We replace the backing map before any use can occur
// a new ConcurrentHashMap with a concurrency level of 1
try {
// Cache impl class is UnboundedLocalLoadingCache which extends
// UnboundedLocalManualCache

// UnboundedLocalManualCache has a field 'cache' with an
// UnboundedLocalCache which contains the actual backing map
final Field innerCache = this.handlersCache.getClass().getSuperclass().getDeclaredField("cache");
innerCache.setAccessible(true);
final Object innerCacheValue = innerCache.get(this.handlersCache);
final Class<?> innerCacheClass = innerCacheValue.getClass(); // UnboundedLocalCache
final Field cacheData = innerCacheClass.getDeclaredField("data");
cacheData.setAccessible(true);
final ConcurrentHashMap<Class<? extends Event>, RegisteredListener.Cache> newBackingData =
new ConcurrentHashMap<>(150, 0.75f, 1);
cacheData.set(innerCacheValue, newBackingData);
} catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
SpongeCommon.logger().warn("Failed to set event cache backing array, type was " + this.handlersCache.getClass().getName());
SpongeCommon.logger().warn(" Caused by: " + e.getClass().getName() + ": " + e.getMessage());
}
}

private static @Nullable String getHandlerErrorOrNull(final ListenerClassVisitor.DiscoveredMethod method) throws
Expand Down

0 comments on commit 8d8a232

Please sign in to comment.