mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 03:38:01 +00:00
Optimize FileSection.parse()/parseMap().
This was showing up in profiles with simulation AI. The change makes constants for the patterns used, so they don't have to be "compiled" each time and also introduces a cache for these. With this change, a GameCopier operation is sped up by about 30% from my local measurement (I tried with a modern deck I have).
This commit is contained in:
@@ -58,7 +58,11 @@ public class GameCopier {
|
||||
public Game makeCopy() {
|
||||
return makeCopy(null);
|
||||
}
|
||||
|
||||
static int copies;
|
||||
static double totalTime;
|
||||
public Game makeCopy(PhaseType advanceToPhase) {
|
||||
long t = System.currentTimeMillis();
|
||||
List<RegisteredPlayer> origPlayers = origGame.getMatch().getPlayers();
|
||||
List<RegisteredPlayer> newPlayers = new ArrayList<>();
|
||||
for (RegisteredPlayer p : origPlayers) {
|
||||
@@ -147,6 +151,13 @@ public class GameCopier {
|
||||
newGame.getPhaseHandler().devAdvanceToPhase(advanceToPhase);
|
||||
}
|
||||
|
||||
totalTime += (System.currentTimeMillis()-t) * 1000;
|
||||
if ((copies++ % 100) == 0) {
|
||||
System.out.println("Time per copy: " + totalTime/copies);
|
||||
if (copies >= 10000) {
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
return newGame;
|
||||
}
|
||||
|
||||
|
||||
@@ -303,7 +303,7 @@ public final class CardEdition implements Comparable<CardEdition> { // immutable
|
||||
tokenNormalized
|
||||
);
|
||||
|
||||
FileSection section = FileSection.parse(contents.get("metadata"), "=");
|
||||
FileSection section = FileSection.parse(contents.get("metadata"), FileSection.EQUALS_KV_SEPARATOR);
|
||||
res.name = section.get("name");
|
||||
res.date = parseDate(section.get("date"));
|
||||
res.code = section.get("code");
|
||||
|
||||
@@ -27,7 +27,7 @@ public class DeckSerializer {
|
||||
}
|
||||
final List<String> metadata = map.get("metadata");
|
||||
if (metadata != null) {
|
||||
return new DeckFileHeader(FileSection.parse(metadata, "="));
|
||||
return new DeckFileHeader(FileSection.parse(metadata, FileSection.EQUALS_KV_SEPARATOR));
|
||||
}
|
||||
final List<String> general = map.get("general");
|
||||
if (general != null) {
|
||||
|
||||
@@ -108,7 +108,7 @@ public class PreconDeck implements InventoryItemFromSet {
|
||||
|
||||
// To be able to read "shops" section in overloads
|
||||
protected PreconDeck getPreconDeckFromSections(final Map<String, List<String>> sections) {
|
||||
FileSection kv = FileSection.parse(sections.get("metadata"), "=");
|
||||
FileSection kv = FileSection.parse(sections.get("metadata"), FileSection.EQUALS_KV_SEPARATOR);
|
||||
String imageFilename = kv.get("Image");
|
||||
String description = kv.get("Description");
|
||||
String deckEdition = kv.get("set");
|
||||
|
||||
@@ -17,9 +17,12 @@
|
||||
*/
|
||||
package forge.util;
|
||||
|
||||
import com.google.common.collect.HashBasedTable;
|
||||
import com.google.common.collect.Table;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@@ -57,31 +60,37 @@ public class FileSection {
|
||||
lines = lines0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the.
|
||||
*
|
||||
* @param line the line
|
||||
* @param kvSeparator the kv separator
|
||||
* @param pairSeparator the pair separator
|
||||
* @return the file section
|
||||
*/
|
||||
public static FileSection parse(final String line, final String kvSeparator, final String pairSeparator) {
|
||||
Map<String, String> map = parseToMap(line, kvSeparator, pairSeparator);
|
||||
return new FileSection(map);
|
||||
public static final Pattern DOLLAR_SIGN_KV_SEPARATOR = Pattern.compile(Pattern.quote("$"));
|
||||
public static final Pattern ARROW_KV_SEPARATOR = Pattern.compile(Pattern.quote("->"));
|
||||
public static final Pattern EQUALS_KV_SEPARATOR = Pattern.compile(Pattern.quote("="));
|
||||
public static final Pattern COLON_KV_SEPARATOR = Pattern.compile(Pattern.quote(":"));
|
||||
|
||||
private static final String BAR_PAIR_SPLITTER = Pattern.quote("|");
|
||||
|
||||
private static Table<String, Pattern, Map<String, String>> parseToMapCache = HashBasedTable.create();
|
||||
|
||||
public static Map<String, String> parseToMap(final String line, final Pattern kvSeparator) {
|
||||
Map<String, String> result = parseToMapCache.get(line, kvSeparator);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
result = parseToMapImpl(line, kvSeparator);
|
||||
parseToMapCache.put(line, kvSeparator, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Map<String, String> parseToMap(final String line, final String kvSeparator, final String pairSeparator) {
|
||||
Map<String, String> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
if (!StringUtils.isEmpty(line)) {
|
||||
final String[] pairs = line.split(Pattern.quote(pairSeparator));
|
||||
final Pattern splitter = Pattern.compile(Pattern.quote(kvSeparator));
|
||||
private static Map<String, String> parseToMapImpl(final String line, final Pattern kvSeparator) {
|
||||
if (StringUtils.isEmpty(line)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
final Map<String, String> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
final String[] pairs = line.split(BAR_PAIR_SPLITTER);
|
||||
for (final String dd : pairs) {
|
||||
final String[] v = splitter.split(dd, 2);
|
||||
final String[] v = kvSeparator.split(dd, 2);
|
||||
result.put(v[0].trim(), v.length > 1 ? v[1].trim() : "");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return Collections.unmodifiableMap(result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,11 +100,10 @@ public class FileSection {
|
||||
* @param kvSeparator the kv separator
|
||||
* @return the file section
|
||||
*/
|
||||
public static FileSection parse(final Iterable<String> lines, final String kvSeparator) {
|
||||
public static FileSection parse(final Iterable<String> lines, final Pattern kvSeparator) {
|
||||
final FileSection result = new FileSection();
|
||||
final Pattern splitter = Pattern.compile(Pattern.quote(kvSeparator));
|
||||
for (final String dd : lines) {
|
||||
final String[] v = splitter.split(dd, 2);
|
||||
final String[] v = kvSeparator.split(dd, 2);
|
||||
result.lines.put(v[0].trim(), v.length > 1 ? v[1].trim() : "");
|
||||
}
|
||||
|
||||
|
||||
@@ -321,7 +321,7 @@ public class GameFormat implements Comparable<GameFormat> {
|
||||
if (formatStrings == null){
|
||||
return null;
|
||||
}
|
||||
FileSection section = FileSection.parse(formatStrings, ":");
|
||||
FileSection section = FileSection.parse(formatStrings, FileSection.COLON_KV_SEPARATOR);
|
||||
String title = section.get("name");
|
||||
FormatType formatType;
|
||||
try {
|
||||
|
||||
@@ -449,7 +449,7 @@ public final class AbilityFactory {
|
||||
}
|
||||
|
||||
public static final Map<String, String> getMapParams(final String abString) {
|
||||
return FileSection.parseToMap(abString, "$", "|");
|
||||
return FileSection.parseToMap(abString, FileSection.DOLLAR_SIGN_KV_SEPARATOR);
|
||||
}
|
||||
|
||||
public static final void adjustChangeZoneTarget(final Map<String, String> params, final SpellAbility sa) {
|
||||
|
||||
@@ -1893,11 +1893,11 @@ public class Card extends GameEntity implements Comparable<Card> {
|
||||
sb.append("\r\n");
|
||||
}
|
||||
|
||||
while (sb.toString().endsWith("\r\n")) {
|
||||
sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + 3);
|
||||
String result = sb.toString();
|
||||
while (result.endsWith("\r\n")) {
|
||||
result = sb.substring(0, sb.length() - 2);
|
||||
}
|
||||
|
||||
return TextUtil.fastReplace(sb.toString(), "CARDNAME", state.getName());
|
||||
return TextUtil.fastReplace(result, "CARDNAME", state.getName());
|
||||
}
|
||||
|
||||
if (monstrous) {
|
||||
@@ -2071,14 +2071,11 @@ public class Card extends GameEntity implements Comparable<Card> {
|
||||
}
|
||||
|
||||
// replace triple line feeds with double line feeds
|
||||
int start;
|
||||
final String s = "\r\n\r\n\r\n";
|
||||
while (sb.toString().contains(s)) {
|
||||
start = sb.lastIndexOf(s);
|
||||
if ((start < 0) || (start >= sb.length())) {
|
||||
break;
|
||||
}
|
||||
int start = sb.lastIndexOf(s);
|
||||
while (start != -1) {
|
||||
sb.replace(start, start + 4, "\r\n");
|
||||
start = sb.lastIndexOf(s);
|
||||
}
|
||||
|
||||
String desc = TextUtil.fastReplace(sb.toString(), "CARDNAME", state.getName());
|
||||
@@ -2503,7 +2500,7 @@ public class Card extends GameEntity implements Comparable<Card> {
|
||||
}
|
||||
}
|
||||
|
||||
public final FCollectionView<SpellAbility> getIntrinsicSpellAbilities() {
|
||||
public final Iterable<SpellAbility> getIntrinsicSpellAbilities() {
|
||||
return currentState.getIntrinsicSpellAbilities();
|
||||
}
|
||||
|
||||
|
||||
@@ -273,8 +273,8 @@ public class CardState extends GameObject {
|
||||
return newCol;
|
||||
}
|
||||
|
||||
public final FCollectionView<SpellAbility> getIntrinsicSpellAbilities() {
|
||||
return new FCollection<>(Iterables.filter(getSpellAbilities(), SpellAbilityPredicates.isIntrinsic()));
|
||||
public final Iterable<SpellAbility> getIntrinsicSpellAbilities() {
|
||||
return Iterables.filter(getSpellAbilities(), SpellAbilityPredicates.isIntrinsic());
|
||||
}
|
||||
|
||||
public final boolean hasSpellAbility(final SpellAbility sa) {
|
||||
|
||||
@@ -389,7 +389,7 @@ public class ReplacementHandler {
|
||||
}
|
||||
|
||||
public static Map<String, String> parseParams(final String repParse) {
|
||||
return FileSection.parseToMap(repParse, "$", "|");
|
||||
return FileSection.parseToMap(repParse, FileSection.DOLLAR_SIGN_KV_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -96,7 +96,7 @@ public class CustomLimited extends DeckBase {
|
||||
* @return the custom limited
|
||||
*/
|
||||
public static CustomLimited parse(final List<String> dfData, final IStorage<Deck> cubes) {
|
||||
final FileSection data = FileSection.parse(dfData, ":");
|
||||
final FileSection data = FileSection.parse(dfData, FileSection.COLON_KV_SEPARATOR);
|
||||
|
||||
List<Pair<String, Integer>> slots = new ArrayList<>();
|
||||
String boosterData = data.get("Booster");
|
||||
|
||||
@@ -133,7 +133,7 @@ public class ForgeProfileProperties {
|
||||
|
||||
private static Map<String, String> getMap(final Properties props, final String propertyKey) {
|
||||
final String strMap = props.getProperty(propertyKey, "").trim();
|
||||
return FileSection.parseToMap(strMap, "->", "|");
|
||||
return FileSection.parseToMap(strMap, FileSection.ARROW_KV_SEPARATOR);
|
||||
}
|
||||
|
||||
private static int getInt(final Properties props, final String propertyKey, final int defaultValue) {
|
||||
|
||||
@@ -43,7 +43,7 @@ public class SellRules {
|
||||
return;
|
||||
}
|
||||
|
||||
FileSection section = FileSection.parse(questShop, "=");
|
||||
FileSection section = FileSection.parse(questShop, FileSection.EQUALS_KV_SEPARATOR);
|
||||
minWins = section.getInt("WinsToUnlock");
|
||||
cost = section.getInt("Credits", 250);
|
||||
maxDifficulty = section.getInt("MaxDifficulty", 5);
|
||||
|
||||
@@ -28,7 +28,7 @@ public class QuestChallengeReader extends StorageReaderFolder<QuestEventChalleng
|
||||
final QuestEventChallenge qc = new QuestEventChallenge();
|
||||
|
||||
// Unique properties
|
||||
FileSection sectionQuest = FileSection.parse(contents.get("quest"), "=");
|
||||
FileSection sectionQuest = FileSection.parse(contents.get("quest"), FileSection.EQUALS_KV_SEPARATOR);
|
||||
qc.setId(sectionQuest.get("ID", "-1"));
|
||||
qc.setOpponentName(sectionQuest.get("OpponentName"));
|
||||
qc.setRepeatable(sectionQuest.getBoolean("Repeat", false));
|
||||
@@ -60,7 +60,7 @@ public class QuestChallengeReader extends StorageReaderFolder<QuestEventChalleng
|
||||
}
|
||||
|
||||
// Common properties
|
||||
FileSection sectionMeta = FileSection.parse(contents.get("metadata"), "=");
|
||||
FileSection sectionMeta = FileSection.parse(contents.get("metadata"), FileSection.EQUALS_KV_SEPARATOR);
|
||||
qc.setTitle(sectionMeta.get("Title"));
|
||||
qc.setName(qc.getTitle()); // Challenges have unique titles
|
||||
qc.setDifficulty(QuestEventDifficulty.fromString(sectionMeta.get("Difficulty")));
|
||||
|
||||
@@ -27,7 +27,7 @@ public class QuestDuelReader extends StorageReaderFolder<QuestEventDuel> {
|
||||
final QuestEventDuel qc = new QuestEventDuel();
|
||||
|
||||
// Common properties
|
||||
FileSection sectionMeta = FileSection.parse(contents.get("metadata"), "=");
|
||||
FileSection sectionMeta = FileSection.parse(contents.get("metadata"), FileSection.EQUALS_KV_SEPARATOR);
|
||||
qc.setTitle(sectionMeta.get("Title"));
|
||||
qc.setName(sectionMeta.get("Name")); // Challenges have unique titles
|
||||
qc.setDifficulty(QuestEventDifficulty.fromString(sectionMeta.get("Difficulty")));
|
||||
|
||||
Reference in New Issue
Block a user