-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The basic problem with the chunk tick iteration is that Vanilla will iterate over all chunk holders to find ticking chunks. However, there are usually many more chunk holders than ticking chunks. We can eliminate the cost of finding the ticking chunks by maintaining our own list of ticking chunks.
- Loading branch information
1 parent
cec8e6f
commit bbf85f3
Showing
19 changed files
with
620 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/ca/spottedleaf/moonrise/common/misc/PositionCountingAreaMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package ca.spottedleaf.moonrise.common.misc; | ||
|
||
import ca.spottedleaf.concurrentutil.util.IntPairUtil; | ||
import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; | ||
import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap; | ||
import it.unimi.dsi.fastutil.objects.ReferenceSet; | ||
|
||
public final class PositionCountingAreaMap<T> { | ||
|
||
private final Reference2ReferenceOpenHashMap<T, PositionCounter> counters = new Reference2ReferenceOpenHashMap<>(); | ||
private final Long2IntOpenHashMap positions = new Long2IntOpenHashMap(); | ||
|
||
public ReferenceSet<T> getObjects() { | ||
return this.counters.keySet(); | ||
} | ||
|
||
public int getTotalPositions() { | ||
return this.positions.size(); | ||
} | ||
|
||
public boolean hasObjectsNear(final int toX, final int toZ) { | ||
return this.positions.containsKey(IntPairUtil.key(toX, toZ)); | ||
} | ||
|
||
public int getObjectsNear(final int toX, final int toZ) { | ||
return this.positions.get(IntPairUtil.key(toX, toZ)); | ||
} | ||
|
||
public boolean add(final T parameter, final int toX, final int toZ, final int distance) { | ||
final PositionCounter existing = this.counters.get(parameter); | ||
if (existing != null) { | ||
return false; | ||
} | ||
|
||
final PositionCounter counter = new PositionCounter(parameter); | ||
|
||
this.counters.put(parameter, counter); | ||
|
||
return counter.add(toX, toZ, distance); | ||
} | ||
|
||
public boolean addOrUpdate(final T parameter, final int toX, final int toZ, final int distance) { | ||
final PositionCounter existing = this.counters.get(parameter); | ||
if (existing != null) { | ||
return existing.update(toX, toZ, distance); | ||
} | ||
|
||
final PositionCounter counter = new PositionCounter(parameter); | ||
|
||
this.counters.put(parameter, counter); | ||
|
||
return counter.add(toX, toZ, distance); | ||
} | ||
|
||
public boolean remove(final T parameter) { | ||
final PositionCounter counter = this.counters.remove(parameter); | ||
if (counter == null) { | ||
return false; | ||
} | ||
|
||
counter.remove(); | ||
|
||
return true; | ||
} | ||
|
||
public boolean update(final T parameter, final int toX, final int toZ, final int distance) { | ||
final PositionCounter counter = this.counters.get(parameter); | ||
if (counter == null) { | ||
return false; | ||
} | ||
|
||
return counter.update(toX, toZ, distance); | ||
} | ||
|
||
private final class PositionCounter extends SingleUserAreaMap<T> { | ||
|
||
public PositionCounter(final T parameter) { | ||
super(parameter); | ||
} | ||
|
||
@Override | ||
protected void addCallback(final T parameter, final int toX, final int toZ) { | ||
PositionCountingAreaMap.this.positions.addTo(IntPairUtil.key(toX, toZ), 1); | ||
} | ||
|
||
@Override | ||
protected void removeCallback(final T parameter, final int toX, final int toZ) { | ||
final long key = IntPairUtil.key(toX, toZ); | ||
if (PositionCountingAreaMap.this.positions.addTo(key, -1) == 1) { | ||
PositionCountingAreaMap.this.positions.remove(key); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.