Use ThreadLocal StringBuilder for frequently used StringBuilders - saves having to construct new StringBuilder objects

This commit is contained in:
betasteward 2015-05-01 08:10:22 -04:00
parent d7c23bbfc0
commit e131caa453
2 changed files with 49 additions and 4 deletions

View file

@ -176,9 +176,10 @@ public class GameState implements Serializable, Copyable<GameState> {
} }
public String getValue(boolean useHidden) { public String getValue(boolean useHidden) {
StringBuilder sb = new StringBuilder(1024); StringBuilder sb = threadLocalBuilder.get();
sb.append(turnNum).append(turn.getPhaseType()).append(turn.getStepType()).append(activePlayerId).append(priorityPlayerId); sb.append(turn.getValue(turnNum));
sb.append(activePlayerId).append(priorityPlayerId);
for (Player player: players.values()) { for (Player player: players.values()) {
sb.append("player").append(player.getLife()).append("hand"); sb.append("player").append(player.getLife()).append("hand");
@ -214,9 +215,10 @@ public class GameState implements Serializable, Copyable<GameState> {
} }
public String getValue(boolean useHidden, Game game) { public String getValue(boolean useHidden, Game game) {
StringBuilder sb = new StringBuilder(1024); StringBuilder sb = threadLocalBuilder.get();
sb.append(turnNum).append(turn.getPhaseType()).append(turn.getStepType()).append(activePlayerId).append(priorityPlayerId); sb.append(turn.getValue(turnNum));
sb.append(activePlayerId).append(priorityPlayerId);
for (Player player: players.values()) { for (Player player: players.values()) {
sb.append("player").append(player.isPassed()).append(player.getLife()).append("hand"); sb.append("player").append(player.isPassed()).append(player.getLife()).append("hand");
@ -273,6 +275,22 @@ public class GameState implements Serializable, Copyable<GameState> {
return sb.toString(); return sb.toString();
} }
// create a ThreadLocal StringBuilder
private transient ThreadLocal<StringBuilder> threadLocalBuilder = new ThreadLocal<StringBuilder>() {
@Override
protected StringBuilder initialValue() {
return new StringBuilder(1024);
}
@Override
public StringBuilder get() {
StringBuilder b = super.get();
b.setLength(0); // clear/reset the buffer
return b;
}
};
public Players getPlayers() { public Players getPlayers() {
return players; return players;
} }

View file

@ -311,4 +311,31 @@ public class Turn implements Serializable {
public Turn copy() { public Turn copy() {
return new Turn(this); return new Turn(this);
} }
public String getValue(int turnNum) {
StringBuilder sb = threadLocalBuilder.get();
sb.append("[").append(turnNum)
.append(":").append(currentPhase.getType())
.append(":").append(currentPhase.getStep().getType())
.append("]");
return sb.toString();
}
// create a ThreadLocal StringBuilder
private transient ThreadLocal<StringBuilder> threadLocalBuilder = new ThreadLocal<StringBuilder>() {
@Override
protected StringBuilder initialValue() {
return new StringBuilder(50);
}
@Override
public StringBuilder get() {
StringBuilder b = super.get();
b.setLength(0); // clear/reset the buffer
return b;
}
};
} }