aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0744-Custom-table-implementation-for-blockstate-state-loo.patch
blob: 0feb55739cefbf0cdc99c9391869a20947cc6c16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Thu, 11 Mar 2021 20:05:44 -0800
Subject: [PATCH] Custom table implementation for blockstate state lookups

Testing some redstone intensive machines showed to bring about a 10%
improvement.

diff --git a/src/main/java/io/papermc/paper/util/table/ZeroCollidingReferenceStateTable.java b/src/main/java/io/papermc/paper/util/table/ZeroCollidingReferenceStateTable.java
new file mode 100644
index 0000000000000000000000000000000000000000..57d0cd3ad6f972e986c72a57f1a6e36003f190c2
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/table/ZeroCollidingReferenceStateTable.java
@@ -0,0 +1,160 @@
+package io.papermc.paper.util.table;
+
+import com.google.common.collect.Table;
+import net.minecraft.world.level.block.state.StateHolder;
+import net.minecraft.world.level.block.state.properties.Property;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public final class ZeroCollidingReferenceStateTable {
+
+    // upper 32 bits: starting index
+    // lower 32 bits: bitset for contained ids
+    protected final long[] this_index_table;
+    protected final Comparable<?>[] this_table;
+    protected final StateHolder<?, ?> this_state;
+
+    protected long[] index_table;
+    protected StateHolder<?, ?>[][] value_table;
+
+    public ZeroCollidingReferenceStateTable(final StateHolder<?, ?> state, final Map<Property<?>, Comparable<?>> this_map) {
+        this.this_state = state;
+        this.this_index_table = this.create_table(this_map.keySet());
+
+        int max_id = -1;
+        for (final Property<?> property : this_map.keySet()) {
+            final int id = lookup_vindex(property, this.this_index_table);
+            if (id > max_id) {
+                max_id = id;
+            }
+        }
+
+        this.this_table = new Comparable[max_id + 1];
+        for (final Map.Entry<Property<?>, Comparable<?>> entry : this_map.entrySet()) {
+            this.this_table[lookup_vindex(entry.getKey(), this.this_index_table)] = entry.getValue();
+        }
+    }
+
+    public void loadInTable(final Table<Property<?>, Comparable<?>, StateHolder<?, ?>> table,
+                            final Map<Property<?>, Comparable<?>> this_map) {
+        final Set<Property<?>> combined = new HashSet<>(table.rowKeySet());
+        combined.addAll(this_map.keySet());
+
+        this.index_table = this.create_table(combined);
+
+        int max_id = -1;
+        for (final Property<?> property : combined) {
+            final int id = lookup_vindex(property, this.index_table);
+            if (id > max_id) {
+                max_id = id;
+            }
+        }
+
+        this.value_table = new StateHolder[max_id + 1][];
+
+        final Map<Property<?>, Map<Comparable<?>, StateHolder<?, ?>>> map = table.rowMap();
+        for (final Property<?> property : map.keySet()) {
+            final Map<Comparable<?>, StateHolder<?, ?>> propertyMap = map.get(property);
+
+            final int id = lookup_vindex(property, this.index_table);
+            final StateHolder<?, ?>[] states = this.value_table[id] = new StateHolder[property.getPossibleValues().size()];
+
+            for (final Map.Entry<Comparable<?>, StateHolder<?, ?>> entry : propertyMap.entrySet()) {
+                if (entry.getValue() == null) {
+                    // TODO what
+                    continue;
+                }
+
+                states[((Property)property).getIdFor(entry.getKey())] = entry.getValue();
+            }
+        }
+
+
+        for (final Map.Entry<Property<?>, Comparable<?>> entry : this_map.entrySet()) {
+            final Property<?> property = entry.getKey();
+            final int index = lookup_vindex(property, this.index_table);
+
+            if (this.value_table[index] == null) {
+                this.value_table[index] = new StateHolder[property.getPossibleValues().size()];
+            }
+
+            this.value_table[index][((Property)property).getIdFor(entry.getValue())] = this.this_state;
+        }
+    }
+
+
+    protected long[] create_table(final Collection<Property<?>> collection) {
+        int max_id = -1;
+        for (final Property<?> property : collection) {
+            final int id = property.getId();
+            if (id > max_id) {
+                max_id = id;
+            }
+        }
+
+        final long[] ret = new long[((max_id + 1) + 31) >>> 5]; // ceil((max_id + 1) / 32)
+
+        for (final Property<?> property : collection) {
+            final int id = property.getId();
+
+            ret[id >>> 5] |= (1L << (id & 31));
+        }
+
+        int total = 0;
+        for (int i = 1, len = ret.length; i < len; ++i) {
+            ret[i] |= (long)(total += Long.bitCount(ret[i - 1] & 0xFFFFFFFFL)) << 32;
+        }
+
+        return ret;
+    }
+
+    public Comparable<?> get(final Property<?> state) {
+        final Comparable<?>[] table = this.this_table;
+        final int index = lookup_vindex(state, this.this_index_table);
+
+        if (index < 0 || index >= table.length) {
+            return null;
+        }
+        return table[index];
+    }
+
+    public StateHolder<?, ?> get(final Property<?> property, final Comparable<?> with) {
+        final int withId = ((Property)property).getIdFor(with);
+        if (withId < 0) {
+            return null;
+        }
+
+        final int index = lookup_vindex(property, this.index_table);
+        final StateHolder<?, ?>[][] table = this.value_table;
+        if (index < 0 || index >= table.length) {
+            return null;
+        }
+
+        final StateHolder<?, ?>[] values = table[index];
+
+        if (withId >= values.length) {
+            return null;
+        }
+
+        return values[withId];
+    }
+
+    protected static int lookup_vindex(final Property<?> property, final long[] index_table) {
+        final int id = property.getId();
+        final long bitset_mask = (1L << (id & 31));
+        final long lower_mask = bitset_mask - 1;
+        final int index = id >>> 5;
+        if (index >= index_table.length) {
+            return -1;
+        }
+        final long index_value = index_table[index];
+        final long contains_check = ((index_value & bitset_mask) - 1) >> (Long.SIZE - 1); // -1L if doesn't contain
+
+        // index = total bits set in lower table values (upper 32 bits of index_value) plus total bits set in lower indices below id
+        // contains_check is 0 if the bitset had id set, else it's -1: so index is unaffected if contains_check == 0,
+        // otherwise it comes out as -1.
+        return (int)(((index_value >>> 32) + Long.bitCount(index_value & lower_mask)) | contains_check);
+    }
+}
diff --git a/src/main/java/net/minecraft/world/level/block/state/StateHolder.java b/src/main/java/net/minecraft/world/level/block/state/StateHolder.java
index ab712fd29b316e1235645bacaa79aa0a64d0bc00..340d0648fcf9b9749c4daa1c25a226b947707c3d 100644
--- a/src/main/java/net/minecraft/world/level/block/state/StateHolder.java
+++ b/src/main/java/net/minecraft/world/level/block/state/StateHolder.java
@@ -39,11 +39,13 @@ public abstract class StateHolder<O, S> {
     private final ImmutableMap<Property<?>, Comparable<?>> values;
     private Table<Property<?>, Comparable<?>, S> neighbours;
     protected final MapCodec<S> propertiesCodec;
+    protected final io.papermc.paper.util.table.ZeroCollidingReferenceStateTable optimisedTable; // Paper - optimise state lookup
 
     protected StateHolder(O owner, ImmutableMap<Property<?>, Comparable<?>> entries, MapCodec<S> codec) {
         this.owner = owner;
         this.values = entries;
         this.propertiesCodec = codec;
+        this.optimisedTable = new io.papermc.paper.util.table.ZeroCollidingReferenceStateTable(this, entries); // Paper - optimise state lookup
     }
 
     public <T extends Comparable<T>> S cycle(Property<T> property) {
@@ -84,11 +86,11 @@ public abstract class StateHolder<O, S> {
     }
 
     public <T extends Comparable<T>> boolean hasProperty(Property<T> property) {
-        return this.values.containsKey(property);
+        return this.optimisedTable.get(property) != null; // Paper - optimise state lookup
     }
 
     public <T extends Comparable<T>> T getValue(Property<T> property) {
-        Comparable<?> comparable = this.values.get(property);
+        Comparable<?> comparable = this.optimisedTable.get(property); // Paper - optimise state lookup
         if (comparable == null) {
             throw new IllegalArgumentException("Cannot get property " + property + " as it does not exist in " + this.owner);
         } else {
@@ -97,24 +99,18 @@ public abstract class StateHolder<O, S> {
     }
 
     public <T extends Comparable<T>> Optional<T> getOptionalValue(Property<T> property) {
-        Comparable<?> comparable = this.values.get(property);
+        Comparable<?> comparable = this.optimisedTable.get(property); // Paper - optimise state lookup
         return comparable == null ? Optional.empty() : Optional.of(property.getValueClass().cast(comparable));
     }
 
     public <T extends Comparable<T>, V extends T> S setValue(Property<T> property, V value) {
-        Comparable<?> comparable = this.values.get(property);
-        if (comparable == null) {
-            throw new IllegalArgumentException("Cannot set property " + property + " as it does not exist in " + this.owner);
-        } else if (comparable == value) {
-            return (S)this;
-        } else {
-            S object = this.neighbours.get(property, value);
-            if (object == null) {
-                throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner + ", it is not an allowed value");
-            } else {
-                return object;
-            }
+        // Paper start - optimise state lookup
+        final S ret = (S)this.optimisedTable.get(property, value);
+        if (ret == null) {
+            throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner + ", it is not an allowed value");
         }
+        return ret;
+        // Paper end - optimise state lookup
     }
 
     public void populateNeighbours(Map<Map<Property<?>, Comparable<?>>, S> states) {
@@ -133,7 +129,7 @@ public abstract class StateHolder<O, S> {
                 }
             }
 
-            this.neighbours = (Table<Property<?>, Comparable<?>, S>)(table.isEmpty() ? table : ArrayTable.create(table));
+            this.neighbours = (Table<Property<?>, Comparable<?>, S>)(table.isEmpty() ? table : ArrayTable.create(table)); this.optimisedTable.loadInTable((Table)this.neighbours, this.values); // Paper - optimise state lookup
         }
     }
 
diff --git a/src/main/java/net/minecraft/world/level/block/state/properties/BooleanProperty.java b/src/main/java/net/minecraft/world/level/block/state/properties/BooleanProperty.java
index ff1a0d125edd2ea10c870cbb62ae9aa23644b6dc..233215280f8494dbc33a2fd0b14e37e59f1cb643 100644
--- a/src/main/java/net/minecraft/world/level/block/state/properties/BooleanProperty.java
+++ b/src/main/java/net/minecraft/world/level/block/state/properties/BooleanProperty.java
@@ -7,6 +7,13 @@ import java.util.Optional;
 public class BooleanProperty extends Property<Boolean> {
     private final ImmutableSet<Boolean> values = ImmutableSet.of(true, false);
 
+    // Paper start - optimise iblockdata state lookup
+    @Override
+    public final int getIdFor(final Boolean value) {
+        return value.booleanValue() ? 1 : 0;
+    }
+    // Paper end - optimise iblockdata state lookup
+
     protected BooleanProperty(String name) {
         super(name, Boolean.class);
     }
diff --git a/src/main/java/net/minecraft/world/level/block/state/properties/EnumProperty.java b/src/main/java/net/minecraft/world/level/block/state/properties/EnumProperty.java
index 0bca0f971dac994bd8b6ecd87e8b33e26c0f18f9..edd3c745efb40ee79a1393199c7a27ddaa2f8026 100644
--- a/src/main/java/net/minecraft/world/level/block/state/properties/EnumProperty.java
+++ b/src/main/java/net/minecraft/world/level/block/state/properties/EnumProperty.java
@@ -15,6 +15,15 @@ public class EnumProperty<T extends Enum<T> & StringRepresentable> extends Prope
     private final ImmutableSet<T> values;
     private final Map<String, T> names = Maps.newHashMap();
 
+    // Paper start - optimise iblockdata state lookup
+    private int[] idLookupTable;
+
+    @Override
+    public final int getIdFor(final T value) {
+        return this.idLookupTable[value.ordinal()];
+    }
+    // Paper end - optimise iblockdata state lookup
+
     protected EnumProperty(String name, Class<T> type, Collection<T> values) {
         super(name, type);
         this.values = ImmutableSet.copyOf(values);
@@ -28,6 +37,14 @@ public class EnumProperty<T extends Enum<T> & StringRepresentable> extends Prope
             this.names.put(string, enum_);
         }
 
+        // Paper start - optimise iblockdata state lookup
+        int id = 0;
+        this.idLookupTable = new int[type.getEnumConstants().length];
+        java.util.Arrays.fill(this.idLookupTable, -1);
+        for (final T value : this.getPossibleValues()) {
+            this.idLookupTable[value.ordinal()] = id++;
+        }
+        // Paper end - optimise iblockdata state lookup
     }
 
     @Override
diff --git a/src/main/java/net/minecraft/world/level/block/state/properties/IntegerProperty.java b/src/main/java/net/minecraft/world/level/block/state/properties/IntegerProperty.java
index 72f508321ebffcca31240fbdd068b4d185454cbc..d16156f8a4a2507e114dc651fd0af9cdffb3c8e0 100644
--- a/src/main/java/net/minecraft/world/level/block/state/properties/IntegerProperty.java
+++ b/src/main/java/net/minecraft/world/level/block/state/properties/IntegerProperty.java
@@ -13,6 +13,16 @@ public class IntegerProperty extends Property<Integer> {
     public final int min;
     public final int max;
 
+    // Paper start - optimise iblockdata state lookup
+    @Override
+    public final int getIdFor(final Integer value) {
+        final int val = value.intValue();
+        final int ret = val - this.min;
+
+        return ret | ((this.max - ret) >> 31);
+    }
+    // Paper end - optimise iblockdata state lookup
+
     protected IntegerProperty(String name, int min, int max) {
         super(name, Integer.class);
         this.min = min;
diff --git a/src/main/java/net/minecraft/world/level/block/state/properties/Property.java b/src/main/java/net/minecraft/world/level/block/state/properties/Property.java
index a37424bbc6bee02354abaa793aa0865c556c6bbe..f923593bd336dd1a950ba61603d53edb3c9703eb 100644
--- a/src/main/java/net/minecraft/world/level/block/state/properties/Property.java
+++ b/src/main/java/net/minecraft/world/level/block/state/properties/Property.java
@@ -22,6 +22,17 @@ public abstract class Property<T extends Comparable<T>> {
     }, this::getName);
     private final Codec<Property.Value<T>> valueCodec = this.codec.xmap(this::value, Property.Value::value);
 
+    // Paper start - optimise iblockdata state lookup
+    private static final java.util.concurrent.atomic.AtomicInteger ID_GENERATOR = new java.util.concurrent.atomic.AtomicInteger();
+    private final int id = ID_GENERATOR.getAndIncrement();
+
+    public final int getId() {
+        return this.id;
+    }
+
+    public abstract int getIdFor(final T value);
+    // Paper end - optimise state lookup
+
     protected Property(String name, Class<T> type) {
         this.clazz = type;
         this.name = name;