diff --git a/Mage/src/mage/game/GameState.java b/Mage/src/mage/game/GameState.java index 6321d673da..3d6b41513b 100644 --- a/Mage/src/mage/game/GameState.java +++ b/Mage/src/mage/game/GameState.java @@ -176,9 +176,10 @@ public class GameState implements Serializable, Copyable { } 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()) { sb.append("player").append(player.getLife()).append("hand"); @@ -214,9 +215,10 @@ public class GameState implements Serializable, Copyable { } 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()) { sb.append("player").append(player.isPassed()).append(player.getLife()).append("hand"); @@ -273,6 +275,22 @@ public class GameState implements Serializable, Copyable { return sb.toString(); } + // create a ThreadLocal StringBuilder + private transient ThreadLocal threadLocalBuilder = new ThreadLocal() { + @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() { return players; } diff --git a/Mage/src/mage/game/turn/Turn.java b/Mage/src/mage/game/turn/Turn.java index e4ef1b9b23..02fe4ea672 100644 --- a/Mage/src/mage/game/turn/Turn.java +++ b/Mage/src/mage/game/turn/Turn.java @@ -311,4 +311,31 @@ public class Turn implements Serializable { public Turn copy() { 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 threadLocalBuilder = new ThreadLocal() { + @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; + } + + }; + }