spigot/CraftBukkit-Patches/0084-Remove-DataWatcher-Locking.patch

139 lines
5.4 KiB
Diff
Raw Normal View History

2023-06-10 07:22:56 +10:00
From 3b13a8c5d00d7f3e7d455b948c252dee1ba440f2 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Tue, 9 Jul 2019 02:18:54 -0700
Subject: [PATCH] Remove DataWatcher Locking
The lock in DataWatcher is used to prevent concurrent modifications,
however any modifications to this map only occur on initialization of
an Entity in its constructor.
Every other access is through a readlock, which allows the threads to
pass if there is no thread holding the writelock.
Since the writelock is only obtained in the constructor of the Entity,
the further readlocks are actually useless.
This patch also changes the entries map to be fastutil
int2objectopenhashmap for performance.
2021-03-16 09:00:00 +11:00
diff --git a/src/main/java/net/minecraft/network/syncher/DataWatcher.java b/src/main/java/net/minecraft/network/syncher/DataWatcher.java
2023-06-08 01:30:00 +10:00
index ac10ddf35..78d475649 100644
2021-03-16 09:00:00 +11:00
--- a/src/main/java/net/minecraft/network/syncher/DataWatcher.java
+++ b/src/main/java/net/minecraft/network/syncher/DataWatcher.java
2022-12-08 03:00:00 +11:00
@@ -36,7 +36,7 @@ public class DataWatcher {
2021-06-11 15:00:00 +10:00
private static final int MAX_ID_VALUE = 254;
2019-07-20 09:00:00 +10:00
private final Entity entity;
2021-06-11 15:00:00 +10:00
private final Int2ObjectMap<DataWatcher.Item<?>> itemsById = new Int2ObjectOpenHashMap();
2019-07-20 09:00:00 +10:00
- private final ReadWriteLock lock = new ReentrantReadWriteLock();
+ // private final ReadWriteLock lock = new ReentrantReadWriteLock(); // Spigot - not required
2021-06-11 15:00:00 +10:00
private boolean isDirty;
2022-12-08 03:00:00 +11:00
public DataWatcher(Entity entity) {
@@ -83,7 +83,9 @@ public class DataWatcher {
}
}
2021-03-16 09:00:00 +11:00
+ public boolean registrationLocked; // Spigot
2021-11-22 09:00:00 +11:00
public <T> void define(DataWatcherObject<T> datawatcherobject, T t0) {
+ if (this.registrationLocked) throw new IllegalStateException("Registering datawatcher object after entity initialization"); // Spigot
2021-11-22 09:00:00 +11:00
int i = datawatcherobject.getId();
if (i > 254) {
2023-06-08 01:30:00 +10:00
@@ -102,9 +104,9 @@ public class DataWatcher {
2021-11-22 09:00:00 +11:00
private <T> void createDataItem(DataWatcherObject<T> datawatcherobject, T t0) {
DataWatcher.Item<T> datawatcher_item = new DataWatcher.Item<>(datawatcherobject, t0);
2019-07-20 09:00:00 +10:00
- this.lock.writeLock().lock();
+ // this.lock.writeLock().lock(); // Spigot - not required
2021-11-22 09:00:00 +11:00
this.itemsById.put(datawatcherobject.getId(), datawatcher_item);
2019-07-20 09:00:00 +10:00
- this.lock.writeLock().unlock();
+ // this.lock.writeLock().unlock(); // Spigot - not required
}
2023-06-08 01:30:00 +10:00
public <T> boolean hasItem(DataWatcherObject<T> datawatcherobject) {
@@ -112,6 +114,8 @@ public class DataWatcher {
}
2021-11-22 09:00:00 +11:00
private <T> DataWatcher.Item<T> getItem(DataWatcherObject<T> datawatcherobject) {
+ // Spigot start
+ /*
2019-07-20 09:00:00 +10:00
this.lock.readLock().lock();
DataWatcher.Item datawatcher_item;
2023-06-08 01:30:00 +10:00
@@ -129,6 +133,9 @@ public class DataWatcher {
}
return datawatcher_item;
+ */
2021-11-22 09:00:00 +11:00
+ return (DataWatcher.Item) this.itemsById.get(datawatcherobject.getId());
+ // Spigot end
}
public <T> T get(DataWatcherObject<T> datawatcherobject) {
2023-06-08 01:30:00 +10:00
@@ -167,7 +174,7 @@ public class DataWatcher {
2022-12-08 03:00:00 +11:00
List<DataWatcher.b<?>> list = null;
2021-06-11 15:00:00 +10:00
if (this.isDirty) {
2019-07-20 09:00:00 +10:00
- this.lock.readLock().lock();
+ // this.lock.readLock().lock(); // Spigot - not required
2021-06-11 15:00:00 +10:00
ObjectIterator objectiterator = this.itemsById.values().iterator();
2021-06-11 15:00:00 +10:00
while (objectiterator.hasNext()) {
2023-06-08 01:30:00 +10:00
@@ -183,7 +190,7 @@ public class DataWatcher {
}
}
2019-07-20 09:00:00 +10:00
- this.lock.readLock().unlock();
+ // this.lock.readLock().unlock(); // Spigot - not required
}
2021-06-11 15:00:00 +10:00
this.isDirty = false;
2023-06-08 01:30:00 +10:00
@@ -194,7 +201,7 @@ public class DataWatcher {
2022-12-08 03:00:00 +11:00
public List<DataWatcher.b<?>> getNonDefaultValues() {
List<DataWatcher.b<?>> list = null;
2019-07-20 09:00:00 +10:00
- this.lock.readLock().lock();
+ // this.lock.readLock().lock(); // Spigot - not required
2022-12-08 03:00:00 +11:00
ObjectIterator objectiterator = this.itemsById.values().iterator();
2022-12-08 03:00:00 +11:00
while (objectiterator.hasNext()) {
2023-06-08 01:30:00 +10:00
@@ -209,12 +216,12 @@ public class DataWatcher {
}
}
2019-07-20 09:00:00 +10:00
- this.lock.readLock().unlock();
+ // this.lock.readLock().unlock(); // Spigot - not required
return list;
}
2022-12-08 03:00:00 +11:00
public void assignValues(List<DataWatcher.b<?>> list) {
2021-06-11 15:00:00 +10:00
- this.lock.writeLock().lock();
+ // this.lock.writeLock().lock(); // Spigot - not required
try {
Iterator iterator = list.iterator();
2023-06-08 01:30:00 +10:00
@@ -229,7 +236,7 @@ public class DataWatcher {
2021-06-11 15:00:00 +10:00
}
}
} finally {
- this.lock.writeLock().unlock();
+ // this.lock.writeLock().unlock(); // Spigot - not required
}
2023-03-15 03:30:00 +11:00
this.entity.onSyncedDataUpdated(list);
2021-03-16 09:00:00 +11:00
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
2023-06-08 01:30:00 +10:00
index e83db7454..47b7eaaa0 100644
2021-03-16 09:00:00 +11:00
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
2023-06-08 01:30:00 +10:00
@@ -370,6 +370,7 @@ public abstract class Entity implements INamableTileEntity, EntityAccess, IComma
2021-11-22 09:00:00 +11:00
this.entityData.define(Entity.DATA_POSE, EntityPose.STANDING);
this.entityData.define(Entity.DATA_TICKS_FROZEN, 0);
this.defineSynchedData();
+ this.getEntityData().registrationLocked = true; // Spigot
this.setPos(0.0D, 0.0D, 0.0D);
this.eyeHeight = this.getEyeHeight(EntityPose.STANDING, this.dimensions);
}
--
2023-06-08 01:30:00 +10:00
2.40.1