Switch to Object2ObjectOpenHashMap for smaller footprint

This commit is contained in:
Len 2022-08-28 23:24:54 +02:00
parent 3ec0e30f57
commit 9f5d979584
3 changed files with 14 additions and 14 deletions

View File

@ -6,7 +6,7 @@ import com.alttd.objects.EconUser;
import com.alttd.objects.Price;
import com.alttd.objects.VillagerType;
import com.alttd.util.Logger;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
@ -42,7 +42,7 @@ public class CommandPoints extends SubCommand {
AtomicBoolean allPointsAreZero = new AtomicBoolean(true);
Object2ObjectArrayMap<String, Integer> pointsMap = user.getPointsMap();
Object2ObjectOpenHashMap<String, Integer> pointsMap = user.getPointsMap();
pointsMap.keySet().forEach(key -> {
VillagerType villagerType = VillagerType.getVillagerType(key);
if (villagerType == null) {
@ -67,7 +67,7 @@ public class CommandPoints extends SubCommand {
if (args[1].equals("all")) {
for (VillagerType villagerType : VillagerType.getVillagerTypes()) {
Object2ObjectArrayMap<String, Integer> pointsMap = user.getPointsMap();
Object2ObjectOpenHashMap<String, Integer> pointsMap = user.getPointsMap();
int currentPoints = pointsMap.getOrDefault(villagerType.getName(), 0);
ref.message = ref.message.append(miniMessage.deserialize("\n", TagResolver.resolver()));
ref.message = ref.message.append(miniMessage.deserialize(Config.POINTS_CONTENT, TagResolver.resolver(
@ -79,7 +79,7 @@ public class CommandPoints extends SubCommand {
}
} else {
VillagerType villagerType = VillagerType.getVillagerType(args[1].toLowerCase());
Object2ObjectArrayMap<String, Integer> pointsMap = user.getPointsMap();
Object2ObjectOpenHashMap<String, Integer> pointsMap = user.getPointsMap();
if (villagerType == null) {
player.sendMiniMessage(Config.NOT_A_VILLAGER, TagResolver.resolver(Placeholder.unparsed("villager_type", args[1])));
return true;

View File

@ -2,7 +2,7 @@ package com.alttd.database;
import com.alttd.objects.EconUser;
import com.alttd.util.Logger;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -20,7 +20,7 @@ public class Queries {
* @param uuid Uuid for the user you want to add the points to
* @param pointsMap Contains all (villagerType, points) entries for user
*/
public static void updateUserPoints(UUID uuid, Object2ObjectArrayMap<String, Integer> pointsMap) {
public static void updateUserPoints(UUID uuid, Object2ObjectOpenHashMap<String, Integer> pointsMap) {
String sql = "INSERT INTO user_points " +
"(uuid, villager_type, points) " +
"VALUES (?, ?, ?) " +
@ -63,7 +63,7 @@ public class Queries {
preparedStatement.setString(1, uuid.toString());
ResultSet resultSet = preparedStatement.executeQuery();
Object2ObjectArrayMap<String, Integer> points = new Object2ObjectArrayMap<>();
Object2ObjectOpenHashMap<String, Integer> points = new Object2ObjectOpenHashMap<>();
while (resultSet.next()) {
points.put(
resultSet.getString("villager_type"),
@ -74,7 +74,7 @@ public class Queries {
} catch (SQLException e) {
e.printStackTrace();
}
return (new EconUser(uuid, new Object2ObjectArrayMap<>()));
return (new EconUser(uuid, new Object2ObjectOpenHashMap<>()));
}
/**

View File

@ -6,7 +6,7 @@ import com.alttd.database.Queries;
import com.alttd.util.Logger;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.Unmodifiable;
@ -17,14 +17,14 @@ import java.util.stream.Collectors;
public class EconUser {
private static Object2ObjectArrayMap<UUID, EconUser> users = new Object2ObjectArrayMap<>();
private static Object2ObjectOpenHashMap<UUID, EconUser> users = new Object2ObjectOpenHashMap<>();
private final static Queue<EconUser> addQueue = new LinkedBlockingQueue<>();
private final static Queue<EconUser> removeQueue = new LinkedBlockingQueue<>();
private final UUID uuid;
private final Object2ObjectArrayMap<String, Integer> pointsMap;
private final Object2ObjectOpenHashMap<String, Integer> pointsMap;
public EconUser(UUID uuid, Object2ObjectArrayMap<String, Integer> points) {
public EconUser(UUID uuid, Object2ObjectOpenHashMap<String, Integer> points) {
this.uuid = uuid;
this.pointsMap = points;
addQueue.offer(this);
@ -41,7 +41,7 @@ public class EconUser {
return uuid;
}
public Object2ObjectArrayMap<String, Integer> getPointsMap() {
public Object2ObjectOpenHashMap<String, Integer> getPointsMap() {
return pointsMap;
}
@ -149,7 +149,7 @@ public class EconUser {
private static void updateUsers() {
if (addQueue.isEmpty() && removeQueue.isEmpty())
return;
Object2ObjectArrayMap<UUID, EconUser> tmp = new Object2ObjectArrayMap<>(users);
Object2ObjectOpenHashMap<UUID, EconUser> tmp = new Object2ObjectOpenHashMap<>(users);
while (!addQueue.isEmpty()) {
EconUser user = addQueue.poll();
if (user == null)