Browse Source

Some cleanup

malte0811 4 years ago
parent
commit
ff2c66444f

+ 5 - 0
README.md

@@ -0,0 +1,5 @@
+A coremod to save a few of these:  
+<img src="https://upload.wikimedia.org/wikipedia/commons/d/da/KL_CoreMemory.jpg" width="400"/>  
+(Konstantin Lanzet, CC BY-SA 3.0 <http://creativecommons.org/licenses/by-sa/3.0/>, via Wikimedia Commons)
+
+I intend to PR these changes to Forge soon.

+ 7 - 5
src/main/java/malte0811/ferritecore/FastMap.java

@@ -26,7 +26,7 @@ public class FastMap<Value> {
             valuesList.add(null);
         }
         for (Map.Entry<Map<Property<?>, Comparable<?>>, Value> state : valuesMap.entrySet()) {
-            valuesList.set(indexOf(state.getKey()), state.getValue());
+            valuesList.set(getIndexOf(state.getKey()), state.getValue());
         }
         this.values = valuesList;
         this.rawKeys = new ArrayList<>(properties);
@@ -35,7 +35,7 @@ public class FastMap<Value> {
     @Nullable
     public <T extends Comparable<T>>
     Value with(int last, Property<T> prop, T value) {
-        final Key<T> keyToChange = indexOf(prop);
+        final Key<T> keyToChange = getKeyFor(prop);
         if (keyToChange == null) {
             return null;
         }
@@ -48,7 +48,9 @@ public class FastMap<Value> {
 
     @Nullable
     private <T extends Comparable<T>>
-    Key<T> indexOf(Property<T> prop) {
+    Key<T> getKeyFor(Property<T> prop) {
+        // It might be possible to speed this up by sorting the keys by their hash code and using a binary search,
+        // however I do not think that it would actually be faster in practice.
         for (Key<?> key : keys) {
             if (key.getProperty() == prop) {
                 return (Key<T>) key;
@@ -57,7 +59,7 @@ public class FastMap<Value> {
         return null;
     }
 
-    public int indexOf(Map<Property<?>, Comparable<?>> state) {
+    public int getIndexOf(Map<Property<?>, Comparable<?>> state) {
         int id = 0;
         for (Key<?> k : keys) {
             id += k.toPartialMapIndex(state.get(k.getProperty()));
@@ -68,7 +70,7 @@ public class FastMap<Value> {
     @Nullable
     public <T extends Comparable<T>>
     T getValue(int stateIndex, Property<T> property) {
-        final Key<T> propId = indexOf(property);
+        final Key<T> propId = getKeyFor(property);
         if (propId == null) {
             return null;
         }

+ 2 - 3
src/main/java/malte0811/ferritecore/HackyGlobalState.java

@@ -1,11 +1,10 @@
 package malte0811.ferritecore;
 
-import net.minecraft.block.BlockState;
 import net.minecraft.state.Property;
 
 import java.util.Map;
 
 public class HackyGlobalState {
-    public static Map<Map<Property<?>, Comparable<?>>, ?> lastStateMap;
-    public static FastMap<?> lastFastStateMap;
+    public static ThreadLocal<Map<Map<Property<?>, Comparable<?>>, ?>> lastStateMap;
+    public static ThreadLocal<FastMap<?>> lastFastStateMap;
 }

+ 10 - 7
src/main/java/malte0811/ferritecore/mixin/StateholderMixin.java

@@ -37,15 +37,15 @@ public abstract class StateholderMixin<O, S> {
     public void func_235899_a_(Map<Map<Property<?>, Comparable<?>>, S> states) {
         if (globalTable != null) {
             throw new IllegalStateException();
-        } else if (states == HackyGlobalState.lastStateMap) {
+        } else if (states == HackyGlobalState.lastStateMap.get()) {
             // Use "hacky global state" to use the same fast map for all states of one block
-            this.globalTable = (FastMap<S>) HackyGlobalState.lastFastStateMap;
+            this.globalTable = (FastMap<S>) HackyGlobalState.lastFastStateMap.get();
         } else {
-            HackyGlobalState.lastStateMap = states;
+            HackyGlobalState.lastStateMap.set(states);
             this.globalTable = new FastMap<>(getProperties(), states);
-            HackyGlobalState.lastFastStateMap = this.globalTable;
+            HackyGlobalState.lastFastStateMap.set(this.globalTable);
         }
-        this.globalTableIndex = this.globalTable.indexOf(properties);
+        this.globalTableIndex = this.globalTable.getIndexOf(properties);
         properties = null;
     }
 
@@ -68,7 +68,7 @@ public abstract class StateholderMixin<O, S> {
         }
     }
 
-    // All other Mixins: If the new data structures are initialized, use those. Otherwise (if populateNeighbors wasn't
+    // All other Mixins: If the new data structures are initialized, use those. Otherwise (if populateNeighbors didn't
     // run yet) use the vanilla code using `properties`
     @Inject(method = "get", at = @At("HEAD"), cancellable = true)
     public <T extends Comparable<T>>
@@ -97,7 +97,10 @@ public abstract class StateholderMixin<O, S> {
         }
     }
 
-    //TODO speed up in some way?
+    // TODO speed up in some way?
+    // The cleanest (lowest-impact) approach would be to use a custom implementation of ImmutableMap (based on a FastMap
+    // and an index), but that whole class hierarchy is a very "closed" (many essential methods/classes are
+    // package-private)
     @Inject(method = "getValues", at = @At("HEAD"), cancellable = true)
     public void getValuesHead(CallbackInfoReturnable<ImmutableMap<Property<?>, Comparable<?>>> cir) {
         if (globalTable != null) {

+ 0 - 8
stats.md

@@ -1,8 +0,0 @@
-| State                  | Bytes |
-|:-----------------------|------:|
-|Base                    |209521 |
-|Batch same serializers  |168782 |
-|Recurring RL namespaces |141893 |
-|ItemStack optimizations |124605 |
-|Recurring ingredients   |102810 |
-|Global ingredients      | 96890 |