Skip to content

Commit 5364302

Browse files
java-backend: Caching toString() for KItem. Speeds up logging options. Enabled with --cache-tostring. (runtimeverification#302)
1 parent fd9f798 commit 5364302

4 files changed

Lines changed: 54 additions & 3 deletions

File tree

java-backend/src/main/java/org/kframework/backend/java/kil/GlobalContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.kframework.backend.java.util.FormulaSimplificationCache;
1212
import org.kframework.backend.java.util.Profiler2;
1313
import org.kframework.backend.java.util.StateLog;
14+
import org.kframework.backend.java.util.ToStringCache;
1415
import org.kframework.backend.java.util.Z3Wrapper;
1516
import org.kframework.krun.KRunOptions;
1617
import org.kframework.krun.api.io.FileSystem;
@@ -43,6 +44,7 @@ public class GlobalContext implements Serializable {
4344
public final PrettyPrinter prettyPrinter;
4445
public final transient FunctionCache functionCache = new FunctionCache();
4546
public final transient FormulaSimplificationCache formulaCache = new FormulaSimplificationCache();
47+
public final transient ToStringCache toStringCache = new ToStringCache();
4648

4749
private boolean isExecutionPhase = true;
4850

java-backend/src/main/java/org/kframework/backend/java/kil/KItem.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,19 @@ protected int computeHash() {
788788

789789
@Override
790790
public String toString() {
791+
String cached = global.toStringCache.get(this);
792+
if (cached != null) {
793+
return cached;
794+
}
795+
796+
String result = toStringImpl();
797+
if (global.javaExecutionOptions.cacheToString) {
798+
global.toStringCache.put(this, result);
799+
}
800+
return result;
801+
}
802+
803+
public String toStringImpl() {
791804
return kLabel + "(" + kList.toString() + ")";
792805
}
793806

java-backend/src/main/java/org/kframework/backend/java/symbolic/JavaExecutionOptions.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@ public final class JavaExecutionOptions {
5151
@Parameter(names="--cache-func", description="Cache evaluation results of pure functions. Enabled by default.", arity = 1)
5252
public boolean cacheFunctions = true;
5353

54-
@Parameter(names="--cache-formulas", description="Cache results of ConjunctiveFormula.simplify().")
55-
public boolean cacheFormulas = false;
56-
5754
@Parameter(names="--cache-func-optimized",
5855
description="Clear function cache after initialization phase. Frees some memory. Use IN ADDITION to --cache-func")
5956
public boolean cacheFunctionsOptimized = false;
6057

58+
@Parameter(names="--cache-formulas", description="Cache results of ConjunctiveFormula.simplify().")
59+
public boolean cacheFormulas = false;
60+
61+
@Parameter(names="--cache-tostring",
62+
description="Cache toString() result for KItem, Equality and DisjunctiveFormula. " +
63+
"Speeds up logging but eats more memory.", arity = 1)
64+
public boolean cacheToString = true;
65+
6166
@Parameter(names="--format-failures", description="Format failure final states. By default they are printed all " +
6267
"on one line, using ConstrainedTerm.toString(). If option is enabled, they are printed a bit nicer, " +
6368
"using custom ConjunctiveFormula formatter, but still fast. Disabled by default for output compatibility " +
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2019 K Team. All Rights Reserved.
2+
package org.kframework.backend.java.util;
3+
4+
import org.kframework.backend.java.kil.KItem;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/**
10+
* @author Denis Bogdanas
11+
* Created on 31-Jan-19.
12+
*/
13+
public class ToStringCache {
14+
private Map<KItem, String> toStringCache = new HashMap<>();
15+
16+
public String get(KItem kItem) {
17+
return toStringCache.get(kItem);
18+
}
19+
20+
public void put(KItem kItem, String str) {
21+
toStringCache.put(kItem, str);
22+
}
23+
24+
public void clear() {
25+
toStringCache.clear();
26+
}
27+
28+
public int size() {
29+
return toStringCache.size();
30+
}
31+
}

0 commit comments

Comments
 (0)