From dd4df9baaa70ef5a2eea44bfaeea5393b68cb4a8 Mon Sep 17 00:00:00 2001 From: Alexei Svitkine Date: Fri, 20 Dec 2019 11:22:06 -0500 Subject: [PATCH] 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). --- .../java/forge/ai/simulation/GameCopier.java | 13 +++- .../src/main/java/forge/card/CardEdition.java | 2 +- .../java/forge/deck/io/DeckSerializer.java | 2 +- .../src/main/java/forge/item/PreconDeck.java | 2 +- .../src/main/java/forge/util/FileSection.java | 62 +++++++++++-------- .../src/main/java/forge/game/GameFormat.java | 2 +- .../forge/game/ability/AbilityFactory.java | 2 +- .../src/main/java/forge/game/card/Card.java | 19 +++--- .../main/java/forge/game/card/CardState.java | 4 +- .../game/replacement/ReplacementHandler.java | 2 +- .../java/forge/limited/CustomLimited.java | 2 +- .../properties/ForgeProfileProperties.java | 2 +- .../src/main/java/forge/quest/SellRules.java | 2 +- .../forge/quest/io/QuestChallengeReader.java | 4 +- .../java/forge/quest/io/QuestDuelReader.java | 2 +- 15 files changed, 69 insertions(+), 53 deletions(-) diff --git a/forge-ai/src/main/java/forge/ai/simulation/GameCopier.java b/forge-ai/src/main/java/forge/ai/simulation/GameCopier.java index cd99a5a6d94..fc574542bc5 100644 --- a/forge-ai/src/main/java/forge/ai/simulation/GameCopier.java +++ b/forge-ai/src/main/java/forge/ai/simulation/GameCopier.java @@ -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 origPlayers = origGame.getMatch().getPlayers(); List newPlayers = new ArrayList<>(); for (RegisteredPlayer p : origPlayers) { @@ -146,7 +150,14 @@ public class GameCopier { if (advanceToPhase != null) { 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; } diff --git a/forge-core/src/main/java/forge/card/CardEdition.java b/forge-core/src/main/java/forge/card/CardEdition.java index b9b88687d97..15fbaa7ec8f 100644 --- a/forge-core/src/main/java/forge/card/CardEdition.java +++ b/forge-core/src/main/java/forge/card/CardEdition.java @@ -303,7 +303,7 @@ public final class CardEdition implements Comparable { // 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"); diff --git a/forge-core/src/main/java/forge/deck/io/DeckSerializer.java b/forge-core/src/main/java/forge/deck/io/DeckSerializer.java index 8b59c4e8dd2..4df2b493f1a 100644 --- a/forge-core/src/main/java/forge/deck/io/DeckSerializer.java +++ b/forge-core/src/main/java/forge/deck/io/DeckSerializer.java @@ -27,7 +27,7 @@ public class DeckSerializer { } final List 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 general = map.get("general"); if (general != null) { diff --git a/forge-core/src/main/java/forge/item/PreconDeck.java b/forge-core/src/main/java/forge/item/PreconDeck.java index c7ee708b59a..bde7a829c9b 100644 --- a/forge-core/src/main/java/forge/item/PreconDeck.java +++ b/forge-core/src/main/java/forge/item/PreconDeck.java @@ -108,7 +108,7 @@ public class PreconDeck implements InventoryItemFromSet { // To be able to read "shops" section in overloads protected PreconDeck getPreconDeckFromSections(final Map> 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"); diff --git a/forge-core/src/main/java/forge/util/FileSection.java b/forge-core/src/main/java/forge/util/FileSection.java index 53a5f140d06..d706be82938 100644 --- a/forge-core/src/main/java/forge/util/FileSection.java +++ b/forge-core/src/main/java/forge/util/FileSection.java @@ -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; @@ -56,34 +59,40 @@ public class FileSection { protected FileSection(Map lines0) { 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 map = parseToMap(line, kvSeparator, pairSeparator); - return new FileSection(map); - } - - public static Map parseToMap(final String line, final String kvSeparator, final String pairSeparator) { - Map 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)); - - for (final String dd : pairs) { - final String[] v = splitter.split(dd, 2); - result.put(v[0].trim(), v.length > 1 ? v[1].trim() : ""); - } + + 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> parseToMapCache = HashBasedTable.create(); + + public static Map parseToMap(final String line, final Pattern kvSeparator) { + Map result = parseToMapCache.get(line, kvSeparator); + if (result != null) { + return result; } + result = parseToMapImpl(line, kvSeparator); + parseToMapCache.put(line, kvSeparator, result); return result; } + private static Map parseToMapImpl(final String line, final Pattern kvSeparator) { + if (StringUtils.isEmpty(line)) { + return Collections.emptyMap(); + } + + final Map result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + final String[] pairs = line.split(BAR_PAIR_SPLITTER); + for (final String dd : pairs) { + final String[] v = kvSeparator.split(dd, 2); + result.put(v[0].trim(), v.length > 1 ? v[1].trim() : ""); + } + return Collections.unmodifiableMap(result); + } + /** * Parses the. * @@ -91,11 +100,10 @@ public class FileSection { * @param kvSeparator the kv separator * @return the file section */ - public static FileSection parse(final Iterable lines, final String kvSeparator) { + public static FileSection parse(final Iterable 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() : ""); } diff --git a/forge-game/src/main/java/forge/game/GameFormat.java b/forge-game/src/main/java/forge/game/GameFormat.java index 9eb259404ea..e4efda21fb6 100644 --- a/forge-game/src/main/java/forge/game/GameFormat.java +++ b/forge-game/src/main/java/forge/game/GameFormat.java @@ -321,7 +321,7 @@ public class GameFormat implements Comparable { 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 { diff --git a/forge-game/src/main/java/forge/game/ability/AbilityFactory.java b/forge-game/src/main/java/forge/game/ability/AbilityFactory.java index 1e770f1fce9..9ff6c8d5dc8 100644 --- a/forge-game/src/main/java/forge/game/ability/AbilityFactory.java +++ b/forge-game/src/main/java/forge/game/ability/AbilityFactory.java @@ -449,7 +449,7 @@ public final class AbilityFactory { } public static final Map getMapParams(final String abString) { - return FileSection.parseToMap(abString, "$", "|"); + return FileSection.parseToMap(abString, FileSection.DOLLAR_SIGN_KV_SEPARATOR); } public static final void adjustChangeZoneTarget(final Map params, final SpellAbility sa) { diff --git a/forge-game/src/main/java/forge/game/card/Card.java b/forge-game/src/main/java/forge/game/card/Card.java index f2f4a7ce8e3..440c1ce9606 100644 --- a/forge-game/src/main/java/forge/game/card/Card.java +++ b/forge-game/src/main/java/forge/game/card/Card.java @@ -1893,11 +1893,11 @@ public class Card extends GameEntity implements Comparable { 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 { } // 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 { } } - public final FCollectionView getIntrinsicSpellAbilities() { + public final Iterable getIntrinsicSpellAbilities() { return currentState.getIntrinsicSpellAbilities(); } diff --git a/forge-game/src/main/java/forge/game/card/CardState.java b/forge-game/src/main/java/forge/game/card/CardState.java index 1fafb514335..ba50c919a0b 100644 --- a/forge-game/src/main/java/forge/game/card/CardState.java +++ b/forge-game/src/main/java/forge/game/card/CardState.java @@ -273,8 +273,8 @@ public class CardState extends GameObject { return newCol; } - public final FCollectionView getIntrinsicSpellAbilities() { - return new FCollection<>(Iterables.filter(getSpellAbilities(), SpellAbilityPredicates.isIntrinsic())); + public final Iterable getIntrinsicSpellAbilities() { + return Iterables.filter(getSpellAbilities(), SpellAbilityPredicates.isIntrinsic()); } public final boolean hasSpellAbility(final SpellAbility sa) { diff --git a/forge-game/src/main/java/forge/game/replacement/ReplacementHandler.java b/forge-game/src/main/java/forge/game/replacement/ReplacementHandler.java index cba94673457..adeeec09355 100644 --- a/forge-game/src/main/java/forge/game/replacement/ReplacementHandler.java +++ b/forge-game/src/main/java/forge/game/replacement/ReplacementHandler.java @@ -389,7 +389,7 @@ public class ReplacementHandler { } public static Map parseParams(final String repParse) { - return FileSection.parseToMap(repParse, "$", "|"); + return FileSection.parseToMap(repParse, FileSection.DOLLAR_SIGN_KV_SEPARATOR); } /** diff --git a/forge-gui/src/main/java/forge/limited/CustomLimited.java b/forge-gui/src/main/java/forge/limited/CustomLimited.java index b87b0b8bfbc..cd908d5f59e 100644 --- a/forge-gui/src/main/java/forge/limited/CustomLimited.java +++ b/forge-gui/src/main/java/forge/limited/CustomLimited.java @@ -96,7 +96,7 @@ public class CustomLimited extends DeckBase { * @return the custom limited */ public static CustomLimited parse(final List dfData, final IStorage cubes) { - final FileSection data = FileSection.parse(dfData, ":"); + final FileSection data = FileSection.parse(dfData, FileSection.COLON_KV_SEPARATOR); List> slots = new ArrayList<>(); String boosterData = data.get("Booster"); diff --git a/forge-gui/src/main/java/forge/properties/ForgeProfileProperties.java b/forge-gui/src/main/java/forge/properties/ForgeProfileProperties.java index 78cd5278860..8ad8dff029e 100644 --- a/forge-gui/src/main/java/forge/properties/ForgeProfileProperties.java +++ b/forge-gui/src/main/java/forge/properties/ForgeProfileProperties.java @@ -133,7 +133,7 @@ public class ForgeProfileProperties { private static Map 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) { diff --git a/forge-gui/src/main/java/forge/quest/SellRules.java b/forge-gui/src/main/java/forge/quest/SellRules.java index 36de28da65f..614cf576fe5 100644 --- a/forge-gui/src/main/java/forge/quest/SellRules.java +++ b/forge-gui/src/main/java/forge/quest/SellRules.java @@ -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); diff --git a/forge-gui/src/main/java/forge/quest/io/QuestChallengeReader.java b/forge-gui/src/main/java/forge/quest/io/QuestChallengeReader.java index 54d86894784..9e31616491d 100644 --- a/forge-gui/src/main/java/forge/quest/io/QuestChallengeReader.java +++ b/forge-gui/src/main/java/forge/quest/io/QuestChallengeReader.java @@ -28,7 +28,7 @@ public class QuestChallengeReader extends StorageReaderFolder { 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")));