try hashmap kisscache

This commit is contained in:
2024-10-23 21:28:02 -05:00
parent 1f7b6e5b50
commit d78c6078a6
3 changed files with 36 additions and 14 deletions

View File

@@ -11,6 +11,7 @@ using haxe.io.Path;
import kiss.Helpers;
using kiss.Helpers;
using tink.MacroApi;
import haxe.ds.HashMap;
#end
@@ -383,13 +384,13 @@ class AsyncEmbeddedScript2 {
var classFields = []; // Kiss.build() will already include Context.getBuildFields()
var hscriptInstructions:Map<String,String> = [];
var cache:Map<String,String> = [];
var cache:HashMap<HashableString,String> = new HashMap();
#if (kissCache && !lua)
var cacheFile = scriptFile.withoutExtension().withoutDirectory() + ".cache.json";
if (sys.FileSystem.exists(cacheFile)) {
var cacheJson:haxe.DynamicAccess<String> = haxe.Json.parse(sys.io.File.getContent(cacheFile));
for (key => value in cacheJson)
cache[key] = value;
cache[Prelude.hashableString(key)] = value;
}
#end
@@ -453,7 +454,7 @@ class AsyncEmbeddedScript2 {
// As a side-effect, it also fills the KissState with the macros and reader macros that make the DSL syntax
classFields = classFields.concat(Kiss.build(dslFile, k));
if (Lambda.count(cache) > 0) {
if (cache.iterator().hasNext()) {
classFields.push({
name: "hscriptInstructionFile",
access: [AOverride],
@@ -478,8 +479,8 @@ class AsyncEmbeddedScript2 {
function process(nextExp:ReaderExp) {
#if (kissCache && !lua)
var cacheKey = Reader.toString(nextExp.def);
if (cache.exists(cacheKey)) {
hscriptInstructions[Std.string(commandList.length)] = cache[cacheKey];
if (cache.exists(Prelude.hashableString(cacheKey))) {
hscriptInstructions[Std.string(commandList.length)] = cache[Prelude.hashableString(cacheKey)];
commandList.push(macro null);
return;
}
@@ -545,7 +546,7 @@ class AsyncEmbeddedScript2 {
#if (kissCache && !lua)
if (!stateChanged) {
var expr = Kiss._try(()->Kiss.readerExpToHaxeExpr(nextExp, k.forHScript()));
cache[cacheKey] = expr.toString();
cache[Prelude.hashableString(cacheKey)] = expr.toString();
}
#end
@@ -583,7 +584,7 @@ class AsyncEmbeddedScript2 {
});
#if (kissCache && !lua)
sys.io.File.saveContent(cacheFile, haxe.Json.stringify(cache));
sys.io.File.saveContent(cacheFile, haxe.Json.stringify([for (key => value in cache) key.value => value]));
sys.io.File.saveContent(hscriptInstructionFile, haxe.Json.stringify(hscriptInstructions));
#end

View File

@@ -8,6 +8,7 @@ import haxe.macro.ExprTools;
import haxe.macro.PositionTools;
import haxe.io.Path;
import sys.io.File;
import kiss.Prelude;
import kiss.Stream;
import kiss.Reader;
import kiss.ReaderExp;
@@ -20,6 +21,7 @@ import tink.syntaxhub.*;
import tink.macro.Exprs;
import haxe.ds.Either;
import kiss.EType;
import haxe.ds.HashMap;
using kiss.Kiss;
using kiss.Helpers;
@@ -27,6 +29,7 @@ using kiss.Reader;
using tink.MacroApi;
using haxe.io.Path;
using StringTools;
using hx.strings.Strings;
typedef ExprConversion = (ReaderExp) -> Expr;
@@ -540,6 +543,7 @@ class Kiss {
#end
k.fieldList;
}, expectedError);
File.saveContent(cacheFile, haxe.Json.stringify([for (key => value in expCache) key.value => value]));
return result;
}
@@ -636,9 +640,9 @@ class Kiss {
}
static var macroUsed = false;
static var expCache:haxe.DynamicAccess<String> = null;
static var expCache:HashMap<HashableString,String> = null;
static var cacheFile = ".kissCache.json";
static var cacheThreshold = 0.2;
static var cacheThreshold = 0.05;
public static function readerExpToHaxeExpr(exp, k): Expr {
return switch (macroExpandAndConvert(exp, k, false)) {
@@ -660,15 +664,20 @@ class Kiss {
var str = Reader.toString(exp.def);
if (!macroExpandOnly) {
if (expCache == null) {
expCache = if (sys.FileSystem.exists(cacheFile)) {
var expCacheDynamic:haxe.DynamicAccess<String> = if (sys.FileSystem.exists(cacheFile)) {
haxe.Json.parse(File.getContent(cacheFile));
} else {
{};
}
expCache = new HashMap();
for (key => value in expCacheDynamic) {
expCache[Prelude.hashableString(key)] = value;
}
}
if (expCache.exists(str)) {
return Right(Context.parse(expCache[str], Helpers.macroPos(exp)));
if (expCache.exists(Prelude.hashableString(str))) {
return Right(Context.parse(expCache[Prelude.hashableString(str)], Helpers.macroPos(exp)));
}
}
#end
@@ -875,11 +884,10 @@ class Kiss {
#if kissCache
if (!macroExpandOnly) {
if (conversionTime > cacheThreshold && !k.stateChanged) {
expCache[str] = switch (expr) {
expCache[Prelude.hashableString(str)] = switch (expr) {
case Right(expr): expr.toString();
default: throw "macroExpandAndConvert is broken";
}
File.saveContent(cacheFile, haxe.Json.stringify(expCache));
}
}
#end

View File

@@ -31,6 +31,7 @@ import sys.thread.Mutex;
#end
using StringTools;
using uuid.Uuid;
using hx.strings.Strings;
/** What functions that process Lists should do when there are more elements than expected **/
enum ExtraElementHandling {
@@ -50,7 +51,19 @@ enum KissTarget {
Lua;
}
typedef HashableString = {
value:String,
hashCode:()->Int
};
class Prelude {
public static function hashableString(s:String):HashableString {
return {
value: s,
hashCode: () -> s.hashCode()
};
}
static function stringOrFloat(d:Dynamic):Either<String, Float> {
return switch (Type.typeof(d)) {
case TInt | TFloat: Right(0.0 + d);