Fix Optional null issue - Fixes #3155

Also check class loader cache before locking to speed up cached hits to avoid the lock

wasn't gonna make a unique build just for that but can lump it in here.
This commit is contained in:
Aikar 2020-04-16 03:57:02 -04:00
parent e7f799903e
commit 902942d9e4
2 changed files with 10 additions and 5 deletions

View file

@ -5,7 +5,7 @@ Subject: [PATCH] Make JavaPluginLoader thread-safe
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
index 8ff228ce..c0884f27 100644
index 8ff228ce..ba2c5c6e 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
@@ -0,0 +0,0 @@ public final class JavaPluginLoader implements PluginLoader {
@ -22,16 +22,21 @@ index 8ff228ce..c0884f27 100644
@Nullable
Class<?> getClassByName(final String name) {
+ // Paper start - make MT safe
Class<?> cachedClass = classes.get(name);
+ if (cachedClass != null) {
+ return cachedClass;
+ }
+ java.util.concurrent.locks.ReentrantReadWriteLock lock;
+ synchronized (classLoadLock) {
+ lock = classLoadLock.computeIfAbsent(name, (x) -> new java.util.concurrent.locks.ReentrantReadWriteLock());
+ classLoadLockCount.compute(name, (x, prev) -> prev != null ? prev + 1 : 1);
+ }
+ lock.writeLock().lock();try {
+ cachedClass = classes.get(name);
+ // Paper end
Class<?> cachedClass = classes.get(name);
if (cachedClass != null) {
return cachedClass;
@@ -0,0 +0,0 @@ public final class JavaPluginLoader implements PluginLoader {
}
}

View file

@ -7,7 +7,7 @@ In order to get chunk values, we shouldn't need to create
an optional each time.
diff --git a/src/main/java/com/mojang/datafixers/util/Either.java b/src/main/java/com/mojang/datafixers/util/Either.java
index a90adac7b..efae74b71 100644
index a90adac7b..4bb621d57 100644
--- a/src/main/java/com/mojang/datafixers/util/Either.java
+++ b/src/main/java/com/mojang/datafixers/util/Either.java
@@ -0,0 +0,0 @@ public abstract class Either<L, R> implements App<Either.Mu<R>, L> {
@ -19,7 +19,7 @@ index a90adac7b..efae74b71 100644
public Left(final L value) {
- this.value = value;
+ this.value = value; this.valueOptional = Optional.of(value); // Paper - reduce the optional allocation...
+ this.value = value; this.valueOptional = value != null ? Optional.of(value) : Optional.empty(); // Paper - reduce the optional allocation...
}
@Override
@ -41,7 +41,7 @@ index a90adac7b..efae74b71 100644
public Right(final R value) {
- this.value = value;
+ this.value = value; this.valueOptional = Optional.of(value); // Paper - reduce the optional allocation...
+ this.value = value; this.valueOptional = value != null ? Optional.of(value) : Optional.empty(); // Paper - reduce the optional allocation...
}
@Override