Skip to content

Commit 24f9a47

Browse files
committed
Use fastutil collections in some places
1 parent 59dcd16 commit 24f9a47

File tree

6 files changed

+56
-74
lines changed

6 files changed

+56
-74
lines changed

run/RunBots.run.xml

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/commander/java/com/mcmoddev/mmdbot/commander/custompings/CustomPings.java

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import com.mcmoddev.mmdbot.commander.TheCommander;
2525
import com.mcmoddev.mmdbot.core.database.VersionedDataMigrator;
2626
import com.mcmoddev.mmdbot.core.database.VersionedDatabase;
27+
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
28+
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
2729
import lombok.experimental.UtilityClass;
2830

2931
import java.io.FileOutputStream;
@@ -43,6 +45,7 @@
4345

4446
import static com.mcmoddev.mmdbot.core.util.Constants.Gsons.NO_PRETTY_PRINTING;
4547
import static java.util.Collections.synchronizedMap;
48+
import static it.unimi.dsi.fastutil.longs.Long2ObjectMaps.*;
4649

4750
@UtilityClass
4851
public class CustomPings {
@@ -76,49 +79,51 @@ public class CustomPings {
7679

7780
/**
7881
* Example data:
82+
* {@code
7983
* guildId: {
80-
* memberId: [
81-
* {
82-
* pattern: pattern1,
83-
* text: text1
84-
* },
85-
* {
86-
* pattern: pattern2,
87-
* text: text2
84+
* memberId: [
85+
* {
86+
* pattern: pattern1,
87+
* text: text1
88+
* },
89+
* {
90+
* pattern: pattern2,
91+
* text: text2
92+
* }
93+
* ],
94+
* memberId2: [
95+
* {
96+
* pattern: pattern1,
97+
* text: text1
98+
* },
99+
* {
100+
* pattern: pattern2,
101+
* text: text2
102+
* }
103+
* ]
88104
* }
89-
* ],
90-
* memberId2: [
91-
* {
92-
* pattern: pattern1,
93-
* text: text1
94-
* },
95-
* {
96-
* pattern: pattern2,
97-
* text: text2
98-
* }
99-
* ]
100105
* }
101106
*/
102-
private static Map<Long, Map<Long, List<CustomPing>>> pings;
107+
private static Long2ObjectMap<Map<Long, List<CustomPing>>> pings;
103108

104-
public static Map<Long, Map<Long, List<CustomPing>>> getPings() {
109+
public static Long2ObjectMap<Map<Long, List<CustomPing>>> getPings() {
105110
if (pings != null) return pings;
106111
final var path = PATH.get();
107112
if (!Files.exists(path)) {
108-
return pings = synchronizedMap(new HashMap<>());
113+
return pings = synchronize(new Long2ObjectOpenHashMap<>());
109114
}
110115
try {
111116
final var db = VersionedDatabase.<Map<Long, Map<Long, List<CustomPing>>>>fromFile(NO_PRETTY_PRINTING, path, TYPE, CURRENT_SCHEMA_VERSION, new HashMap<>());
112117
if (db.getSchemaVersion() != CURRENT_SCHEMA_VERSION) {
113118
MIGRATOR.migrate(CURRENT_SCHEMA_VERSION, path);
114119
final var newDb = VersionedDatabase.<Map<Long, Map<Long, List<CustomPing>>>>fromFile(NO_PRETTY_PRINTING, path, TYPE, CURRENT_SCHEMA_VERSION, new HashMap<>());
115-
return pings = synchronizedMap(newDb.getData());
120+
return pings = synchronize(new Long2ObjectOpenHashMap<>(newDb.getData()));
116121
} else {
117-
return pings = synchronizedMap(db.getData());
122+
return pings = synchronize(new Long2ObjectOpenHashMap<>(db.getData()));
118123
}
119124
} catch (final IOException exception) {
120125
TheCommander.LOGGER.error("Failed to read custom pings file...", exception);
121-
return pings = synchronizedMap(new HashMap<>());
126+
return pings = synchronize(new Long2ObjectOpenHashMap<>());
122127
}
123128
}
124129

src/commander/java/com/mcmoddev/mmdbot/commander/quotes/Quotes.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import com.mcmoddev.mmdbot.core.database.MigratorCluster;
3535
import com.mcmoddev.mmdbot.core.database.VersionedDataMigrator;
3636
import com.mcmoddev.mmdbot.core.database.VersionedDatabase;
37+
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
38+
import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
39+
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
3740
import net.dv8tion.jda.api.EmbedBuilder;
3841
import net.dv8tion.jda.api.entities.MessageEmbed;
3942
import org.jetbrains.annotations.Nullable;
@@ -46,8 +49,6 @@
4649
import java.nio.file.Path;
4750
import java.time.Instant;
4851
import java.util.ArrayList;
49-
import java.util.Collections;
50-
import java.util.HashMap;
5152
import java.util.List;
5253
import java.util.Map;
5354
import java.util.function.Supplier;
@@ -116,7 +117,7 @@ private Quotes() {
116117
* Quote metadata ID should sync with the index in this list.
117118
* This greatly simplifies access operations.
118119
*/
119-
private static Map<Long, List<Quote>> quotes = null;
120+
private static Long2ObjectMap<List<Quote>> quotes = null;
120121

121122
/**
122123
* The message used for when quotes are null, or do not exist.
@@ -152,21 +153,21 @@ public static void loadQuotes() {
152153

153154
final var path = QUOTE_STORAGE.get();
154155
if (!Files.exists(path)) {
155-
quotes = Collections.synchronizedMap(new HashMap<>());
156+
quotes = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>());
156157
}
157158
try {
158159
final var db = VersionedDatabase.<Map<Long, List<Quote>>>fromFile(GSON, path, TYPE);
159160
if (db.getSchemaVersion() != CURRENT_SCHEMA_VERSION) {
160161
new QuotesMigrator(TheCommander.getInstance().getRunPath()).migrate();
161162
final var newDb = VersionedDatabase.<Map<Long, List<Quote>>>fromFile(GSON, path, TYPE);
162-
quotes = Collections.synchronizedMap(newDb.getData());
163+
quotes = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>(newDb.getData()));
163164
} else {
164-
quotes = Collections.synchronizedMap(db.getData());
165+
quotes = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>(db.getData()));
165166
}
166167

167168
} catch (final IOException exception) {
168169
TheCommander.LOGGER.trace("Failed to read quote file...", exception);
169-
quotes = Collections.synchronizedMap(new HashMap<>());
170+
quotes = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>());
170171
}
171172

172173
if (quotes.get(0L) != null) {

src/commander/java/com/mcmoddev/mmdbot/commander/reminders/Reminders.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.mcmoddev.mmdbot.core.database.VersionedDataMigrator;
2626
import com.mcmoddev.mmdbot.core.database.VersionedDatabase;
2727
import com.mcmoddev.mmdbot.core.util.Utils;
28+
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
29+
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
2830
import lombok.experimental.UtilityClass;
2931

3032
import java.io.FileOutputStream;
@@ -79,26 +81,26 @@ public class Reminders {
7981
*/
8082
private static final Type TYPE = new com.google.common.reflect.TypeToken<Map<Long, List<Reminder>>>() {
8183
}.getType();
82-
private static Map<Long, List<Reminder>> reminders;
84+
private static Long2ObjectMap<List<Reminder>> reminders;
8385

84-
public static Map<Long, List<Reminder>> getReminders() {
86+
public static Long2ObjectMap<List<Reminder>> getReminders() {
8587
if (reminders != null) return reminders;
8688
final var path = PATH.get();
8789
if (!Files.exists(path)) {
88-
return reminders = new HashMap<>();
90+
return reminders = new Long2ObjectOpenHashMap<>();
8991
}
9092
try {
9193
final var db = VersionedDatabase.<Map<Long, List<Reminder>>>fromFile(NO_PRETTY_PRINTING, path, TYPE, CURRENT_SCHEMA_VERSION, new HashMap<>());
9294
if (db.getSchemaVersion() != CURRENT_SCHEMA_VERSION) {
9395
MIGRATOR.migrate(CURRENT_SCHEMA_VERSION, path);
9496
final var newDb = VersionedDatabase.<Map<Long, List<Reminder>>>fromFile(NO_PRETTY_PRINTING, path, TYPE, CURRENT_SCHEMA_VERSION, new HashMap<>());
95-
return reminders = newDb.getData();
97+
return reminders = new Long2ObjectOpenHashMap<>(newDb.getData());
9698
} else {
97-
return reminders = db.getData();
99+
return reminders = new Long2ObjectOpenHashMap<>(db.getData());
98100
}
99101
} catch (final IOException exception) {
100102
TheCommander.LOGGER.error("Failed to read reminders file...", exception);
101-
return reminders = new HashMap<>();
103+
return reminders = new Long2ObjectOpenHashMap<>();
102104
}
103105
}
104106

src/commander/java/com/mcmoddev/mmdbot/commander/util/ThreadChannelCreatorEvents.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.mcmoddev.mmdbot.commander.config.Configuration;
2424
import com.mcmoddev.mmdbot.core.util.TaskScheduler;
2525
import com.mcmoddev.mmdbot.core.util.Utils;
26+
import it.unimi.dsi.fastutil.longs.LongArrayList;
27+
import it.unimi.dsi.fastutil.longs.LongList;
2628
import net.dv8tion.jda.api.EmbedBuilder;
2729
import net.dv8tion.jda.api.entities.MessageType;
2830
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
@@ -34,16 +36,13 @@
3436

3537
import java.awt.Color;
3638
import java.time.Instant;
37-
import java.util.ArrayList;
38-
import java.util.HashMap;
39-
import java.util.List;
40-
import java.util.Map;
39+
import java.util.EnumMap;
4140
import java.util.concurrent.TimeUnit;
4241
import java.util.function.Supplier;
4342

4443
public class ThreadChannelCreatorEvents extends ListenerAdapter {
4544

46-
private final Map<Type, List<Long>> caches = new HashMap<>();
45+
private final EnumMap<Type, LongList> caches = new EnumMap<>(Type.class);
4746
private final Supplier<Configuration> configGetter;
4847

4948
public ThreadChannelCreatorEvents(final Supplier<Configuration> configGetter) {
@@ -121,7 +120,7 @@ private void createThread(final MessageReactionAddEvent event, final Type type)
121120

122121
private void createThread(final MessageReceivedEvent event, final Type threadType, boolean ignoreCooldown) {
123122
final var author = event.getMember();
124-
if (!ignoreCooldown && caches.computeIfAbsent(threadType, k -> new ArrayList<>()).contains(author.getIdLong())) {
123+
if (!ignoreCooldown && caches.computeIfAbsent(threadType, k -> new LongArrayList()).contains(author.getIdLong())) {
125124
return;
126125
}
127126
final var threadTypeStr = threadType.toString();
@@ -135,8 +134,8 @@ private void createThread(final MessageReceivedEvent event, final Type threadTyp
135134
**This thread is intended for discussing %s's %s. The %s:**
136135
%s""".formatted(author.getAsMention(), threadTypeStr, threadTypeStr, event.getMessage().getContentRaw())).build())
137136
.queue(msg -> msg.pin().queue());
138-
caches.computeIfAbsent(threadType, k -> new ArrayList<>()).add(author.getIdLong());
139-
TaskScheduler.scheduleTask(() -> caches.get(threadType).remove(author.getIdLong()), 30, TimeUnit.MINUTES);
137+
caches.computeIfAbsent(threadType, k -> new LongArrayList()).add(author.getIdLong());
138+
TaskScheduler.scheduleTask(() -> caches.get(threadType).rem(author.getIdLong()), 30, TimeUnit.MINUTES);
140139
});
141140
});
142141
}

src/commander/java/com/mcmoddev/mmdbot/commander/util/oldchannels/OldChannelsHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
package com.mcmoddev.mmdbot.commander.util.oldchannels;
2222

2323
import com.mcmoddev.mmdbot.core.util.TaskScheduler;
24+
import it.unimi.dsi.fastutil.longs.Long2LongMap;
25+
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
2426
import net.dv8tion.jda.api.JDA;
2527
import net.dv8tion.jda.api.entities.TextChannel;
2628

@@ -38,7 +40,7 @@ public class OldChannelsHelper {
3840
/**
3941
* The constant channelLastMessageMap.
4042
*/
41-
private static final Map<Long, Long> CHANNEL_LAST_MESSAGE_MAP = new HashMap<>();
43+
private static final Long2LongMap CHANNEL_LAST_MESSAGE_MAP = new Long2LongOpenHashMap();
4244

4345
/**
4446
* The constant ready.

0 commit comments

Comments
 (0)