Make mapping loading considerably faster

This commit is contained in:
KennyTV 2020-09-09 10:54:56 +02:00
parent 414f561673
commit df9508ff3c
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B

View File

@ -5,9 +5,12 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonIOException; import com.google.gson.JsonIOException;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.GsonUtil;
import us.myles.ViaVersion.util.Int2IntBiMap;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
@ -107,15 +110,12 @@ public class MappingDataLoader {
} }
} }
public static void mapIdentifiers(Map<Integer, Integer> output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { public static void mapIdentifiers(Int2IntBiMap output, JsonObject oldIdentifiers, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) {
mapIdentifiers(output, oldIdentifiers, newIdentifiers, null); Object2IntMap newIdentifierMap = MappingDataLoader.indexedObjectToMap(newIdentifiers);
}
public static void mapIdentifiers(Map<Integer, Integer> output, JsonObject oldIdentifiers, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) {
for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) { for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) {
Map.Entry<String, JsonElement> value = mapIdentifierEntry(entry, newIdentifiers, diffIdentifiers); int value = mapIdentifierEntry(entry, newIdentifierMap, diffIdentifiers);
if (value != null) { if (value != -1) {
output.put(Integer.parseInt(entry.getKey()), Integer.parseInt(value.getKey())); output.put(Integer.parseInt(entry.getKey()), value);
} }
} }
} }
@ -125,30 +125,30 @@ public class MappingDataLoader {
} }
public static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) { public static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) {
Object2IntMap newIdentifierMap = MappingDataLoader.indexedObjectToMap(newIdentifiers);
for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) { for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) {
Map.Entry<String, JsonElement> value = mapIdentifierEntry(entry, newIdentifiers, diffIdentifiers); int value = mapIdentifierEntry(entry, newIdentifierMap, diffIdentifiers);
if (value != null) { if (value != -1) {
output[Integer.parseInt(entry.getKey())] = Short.parseShort(value.getKey()); output[Integer.parseInt(entry.getKey())] = (short) value;
} }
} }
} }
@Nullable private static int mapIdentifierEntry(Map.Entry<String, JsonElement> entry, Object2IntMap newIdentifierMap, @Nullable JsonObject diffIdentifiers) {
private static Map.Entry<String, JsonElement> mapIdentifierEntry(Map.Entry<String, JsonElement> entry, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) { int value = newIdentifierMap.getInt(entry.getValue().getAsString());
Map.Entry<String, JsonElement> value = findValue(newIdentifiers, entry.getValue().getAsString()); if (value == -1) {
if (value == null) {
// Search in diff mappings // Search in diff mappings
if (diffIdentifiers != null) { if (diffIdentifiers != null) {
JsonElement diffElement = diffIdentifiers.get(entry.getKey()); JsonElement diffElement = diffIdentifiers.get(entry.getKey());
if (diffElement != null) { if (diffElement != null) {
value = findValue(newIdentifiers, diffElement.getAsString()); value = newIdentifierMap.getInt(diffElement.getAsString());
} }
} }
if (value == null) { if (value == -1) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( ");
} }
return null; return -1;
} }
} }
return value; return value;
@ -159,58 +159,56 @@ public class MappingDataLoader {
} }
public static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, @Nullable JsonObject diffIdentifiers, boolean warnOnMissing) { public static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, @Nullable JsonObject diffIdentifiers, boolean warnOnMissing) {
Object2IntMap newIdentifierMap = MappingDataLoader.arrayToMap(newIdentifiers);
for (int i = 0; i < oldIdentifiers.size(); i++) { for (int i = 0; i < oldIdentifiers.size(); i++) {
JsonElement value = oldIdentifiers.get(i); JsonElement oldIfentifier = oldIdentifiers.get(i);
Integer index = findIndex(newIdentifiers, value.getAsString()); int mappedId = newIdentifierMap.getInt(oldIfentifier.getAsString());
if (index == null) { if (mappedId == -1) {
// Search in diff mappings
if (diffIdentifiers != null) { if (diffIdentifiers != null) {
JsonElement diffElement = diffIdentifiers.get(value.getAsString()); JsonElement diffElement = diffIdentifiers.get(oldIfentifier.getAsString());
if (diffElement != null) { if (diffElement != null) {
index = findIndex(newIdentifiers, diffElement.getAsString()); String mappedName = diffElement.getAsString();
if (mappedName.isEmpty()) continue; // "empty" remaps
mappedId = newIdentifierMap.getInt(mappedName);
} }
} }
if (index == null) { if (mappedId == -1) {
if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("No key for " + value + " :( "); Via.getPlatform().getLogger().warning("No key for " + oldIfentifier + " :( ");
} }
continue; continue;
} }
} }
output[i] = index.shortValue(); output[i] = (short) mappedId;
} }
} }
public static void mapIdentifiers(byte[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { /**
for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) { * @param object json object
Map.Entry<String, JsonElement> value = MappingDataLoader.findValue(newIdentifiers, entry.getValue().getAsString()); * @return map with indexes hashed by their id value
if (value == null) { */
Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); public static Object2IntMap indexedObjectToMap(JsonObject object) {
continue; Object2IntMap map = new Object2IntOpenHashMap(object.size(), 1F);
} map.defaultReturnValue(-1);
output[Integer.parseInt(entry.getKey())] = Byte.parseByte(value.getKey());
}
}
@Nullable
public static Map.Entry<String, JsonElement> findValue(JsonObject object, String needle) {
for (Map.Entry<String, JsonElement> entry : object.entrySet()) { for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
String value = entry.getValue().getAsString(); map.put(entry.getValue().getAsString(), Integer.parseInt(entry.getKey()));
if (value.equals(needle)) {
return entry;
}
} }
return null; return map;
} }
@Nullable /**
public static Integer findIndex(JsonArray array, String value) { * @param array json array
* @return map with indexes hashed by their id value
*/
public static Object2IntMap arrayToMap(JsonArray array) {
Object2IntMap map = new Object2IntOpenHashMap(array.size(), 1F);
map.defaultReturnValue(-1);
for (int i = 0; i < array.size(); i++) { for (int i = 0; i < array.size(); i++) {
JsonElement v = array.get(i); map.put(array.get(i).getAsString(), i);
if (v.getAsString().equals(value)) {
return i;
}
} }
return null; return map;
} }
@Nullable @Nullable