Skip to content

Commit

Permalink
PersistentDataHandler#serializeProfile now runs in parallel for all keys
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Aug 26, 2024
1 parent 19c099e commit 1d9fc34
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import com.willfp.eco.core.data.keys.PersistentDataKey;
import com.willfp.eco.core.registry.Registrable;
import com.willfp.eco.core.tuples.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* Handles persistent data.
Expand Down Expand Up @@ -121,20 +124,21 @@ public final <T> void write(@NotNull final UUID uuid,
@NotNull
public final SerializedProfile serializeProfile(@NotNull final UUID uuid,
@NotNull final Set<PersistentDataKey<?>> keys) {
Map<PersistentDataKey<?>, Object> data = new HashMap<>();
Map<PersistentDataKey<?>, CompletableFuture<Object>> futures = keys.stream()
.collect(Collectors.toMap(
key -> key,
key -> CompletableFuture.supplyAsync(() -> read(uuid, key), executor)
));

for (PersistentDataKey<?> key : keys) {
Object value = read(uuid, key);

if (value != null) {
data.put(key, value);
}
}
Map<PersistentDataKey<?>, Object> data = futures.entrySet().stream()
.map(entry -> new Pair<PersistentDataKey<?>, Object>(entry.getKey(), entry.getValue().join()))
.filter(entry -> entry.getSecond() != null)
.collect(Collectors.toMap(Pair::getFirst, Pair::getSecond));

return new SerializedProfile(uuid, data);
}

/**
/**`
* Load profile data.
*
* @param profile The profile.
Expand Down

0 comments on commit 1d9fc34

Please sign in to comment.