Skip to content

Commit

Permalink
change: Use optimized reference hash sets for entity tracking
Browse files Browse the repository at this point in the history
These avoid allocations of iterators and are a bit faster for most operations
  • Loading branch information
jellysquid3 committed May 10, 2020
1 parent 8846852 commit 67acbf1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package me.jellysquid.mods.lithium.common.entity.tracker;

import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
import me.jellysquid.mods.lithium.common.entity.tracker.nearby.NearbyEntityListener;
import me.jellysquid.mods.lithium.common.entity.tracker.nearby.NearbyEntityListenerProvider;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkSectionPos;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
* Tracks the entities within a world and provides notifications to listeners when a tracked entity enters or leaves a
Expand Down Expand Up @@ -169,8 +173,8 @@ private static long encode(int x, int y, int z) {
}

private class TrackedEntityList {
private final Set<LivingEntity> entities = new HashSet<>();
private final Set<NearbyEntityListener> listeners = new HashSet<>();
private final Set<LivingEntity> entities = new ReferenceOpenHashSet<>();
private final Set<NearbyEntityListener> listeners = new ReferenceOpenHashSet<>();

private final long key;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package me.jellysquid.mods.lithium.common.entity.tracker.nearby;

import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
import me.jellysquid.mods.lithium.common.util.LithiumMath;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.TargetPredicate;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.MathHelper;

import java.util.HashSet;
import java.util.Set;

/**
Expand All @@ -20,7 +20,7 @@ public class NearbyEntityTracker<T extends LivingEntity> implements NearbyEntity
private final int rangeC;
private final float rangeSq;

private final Set<T> nearby = new HashSet<>();
private final Set<T> nearby = new ReferenceOpenHashSet<>();

public NearbyEntityTracker(Class<T> clazz, LivingEntity self, float range) {
this.clazz = clazz;
Expand Down

0 comments on commit 67acbf1

Please sign in to comment.