Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelism Pulse entities in parallel using divide and conquer #1008

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/main/java/net/glowstone/GlowWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -55,6 +56,8 @@
import net.glowstone.net.message.play.entity.EntityStatusMessage;
import net.glowstone.net.message.play.game.BlockChangeMessage;
import net.glowstone.net.message.play.player.ServerDifficultyMessage;
import net.glowstone.parallelism.ForkPulse;
import net.glowstone.parallelism.ForkPulsePlayers;
import net.glowstone.util.BlockStateDelegate;
import net.glowstone.util.GameRuleManager;
import net.glowstone.util.RayUtil;
Expand Down Expand Up @@ -464,7 +467,7 @@ public ChunkLock newChunkLock(@NonNls String desc) {
*/
public void pulse() {
List<GlowEntity> allEntities = new ArrayList<>(entityManager.getAll());
List<GlowPlayer> players = new LinkedList<>();
List<GlowPlayer> players = new ArrayList<>();

activeChunksSet.clear();

Expand All @@ -478,11 +481,12 @@ public void pulse() {
if (entity instanceof GlowPlayer) {
players.add((GlowPlayer) entity);
updateActiveChunkCollection(entity);
} else {
entity.pulse();
}
}

ForkJoinPool.commonPool().invoke(new ForkPulse(
(ArrayList<GlowEntity>) allEntities, 0, allEntities.size() - 1));

updateBlocksInActiveChunks();
// why update blocks before Players or Entities? if there is a specific reason we should
// document it here.
Expand Down Expand Up @@ -608,7 +612,9 @@ private void resetEntities(List<GlowEntity> entities) {
}

private void pulsePlayers(List<GlowPlayer> players) {
players.stream().filter(Objects::nonNull).forEach(GlowEntity::pulse);
ArrayList<GlowPlayer> list = players.stream().filter(Objects::nonNull).
collect(Collectors.toCollection(ArrayList::new));
ForkJoinPool.commonPool().invoke(new ForkPulsePlayers(list, 0, list.size() - 1));
}

private void handleSleepAndWake(List<GlowPlayer> players) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/entity/GlowPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public String getLocale() {
*/
private int prevCentralX;
/**
* The player's previous chunk x coordinate.
* The player's previous chunk z coordinate.
*/
private int prevCentralZ;
/**
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/net/glowstone/parallelism/ForkPulse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.glowstone.parallelism;

import net.glowstone.entity.GlowEntity;
import net.glowstone.entity.GlowPlayer;

import java.util.ArrayList;
import java.util.concurrent.RecursiveAction;

/**
* Represents a {@link RecursiveAction} to be recursively pulse all entities
* except players in parallel. For players check {@link ForkPulsePlayers}.
*/
public class ForkPulse extends RecursiveAction {

private ArrayList<GlowEntity> entities;
private int left;
private int right;

/**
* Creates a new ForkPulse to recursively pulse through current entity list.
*
* @param entities The {@link ArrayList<GlowEntity>} to associate with entity list.
* @param left The left bound (0 to pulse from the start).
* @param right The right bound (entities.size() - 1 to pulse to the end).
*/
public ForkPulse(ArrayList<GlowEntity> entities, int left, int right) { // Creates a recursive
this.entities = entities; // action to pulse entities
this.left = left;
this.right = right;
}

@Override
protected void compute() {
if (entities.size() == 0) { // If for some reason the world has no entities,
return; // this will prevent further execution.
}
if (right - left <= 20) { // TODO: Find a better reasoned threshold
do { // If there are 20 or less entities in the range, pulse them sequentially
GlowEntity entity = entities.get(left);
if (!(entity instanceof GlowPlayer)) {
entity.pulse();
}
} while (++left <= right);
return;
}
int mid = (right + left) / 2; // Otherwise split the work in two
invokeAll(new ForkPulse(entities, left, mid),
new ForkPulse(entities, mid + 1, right));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new line at the end of file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new line at the end but it still refuses to build.

45 changes: 45 additions & 0 deletions src/main/java/net/glowstone/parallelism/ForkPulsePlayers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.glowstone.parallelism;

import net.glowstone.entity.GlowPlayer;

import java.util.ArrayList;
import java.util.concurrent.RecursiveAction;

/**
* Represents a {@link ForkPulse} counterpart to pulse players exclusively
*/
public class ForkPulsePlayers extends RecursiveAction {

private ArrayList<GlowPlayer> players;
private int left;
private int right;

/**
* Creates a new ForkPulse to recursively pulse through current entity players.
*
* @param players The {@link ArrayList<GlowPlayer>} to associate with players list.
* @param left The left bound (0 to pulse from the start).
* @param right The right bound (entities.size() - 1 to pulse to the end).
*/
public ForkPulsePlayers(ArrayList<GlowPlayer> players, int left, int right) {
this.players = players;
this.left = left;
this.right = right;
}

@Override
protected void compute() {
if (players.size() == 0) {
return;
}
if (right - left <= 5) { // TODO: Find a better reasoned threshold
do {
players.get(left).pulse();
} while (++left <= right);
return;
}
int mid = (right + left) / 2;
invokeAll(new ForkPulsePlayers(players, left, mid),
new ForkPulsePlayers(players, mid + 1, right));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new line at the end of file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new line at the end but it still refuses to build.