Compare commits
1 Commits
issue11
...
cache-expe
Author | SHA1 | Date | |
---|---|---|---|
bfcbc1216a |
@@ -302,6 +302,10 @@ class Kiss {
|
||||
**/
|
||||
public static function build(?kissFile:String, ?k:KissState, useClassFields = true, ?context:FrontendContext):Array<Field> {
|
||||
|
||||
#if kissCache
|
||||
if (cache == null) cache = new KissCache();
|
||||
#end
|
||||
|
||||
var classPath = Context.getPosInfos(Context.currentPos()).file;
|
||||
// (load... ) relative to the original file
|
||||
var loadingDirectory = if (classPath == '?') {
|
||||
@@ -360,6 +364,10 @@ class Kiss {
|
||||
}
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
#if kissCache
|
||||
|
||||
#end
|
||||
k.fieldList;
|
||||
});
|
||||
@@ -450,9 +458,12 @@ class Kiss {
|
||||
}
|
||||
|
||||
static var macroUsed = false;
|
||||
static var expCache:haxe.DynamicAccess<String> = null;
|
||||
static var cacheFile = ".kissCache.json";
|
||||
#if kissCache
|
||||
@:persistent
|
||||
static var cache:KissCache;
|
||||
|
||||
static var cacheThreshold = 0.2;
|
||||
#end
|
||||
|
||||
public static function readerExpToHaxeExpr(exp, k): Expr {
|
||||
return switch (macroExpandAndConvert(exp, k, false)) {
|
||||
@@ -473,16 +484,8 @@ class Kiss {
|
||||
#if kissCache
|
||||
var str = Reader.toString(exp.def);
|
||||
if (!macroExpandOnly) {
|
||||
if (expCache == null) {
|
||||
expCache = if (sys.FileSystem.exists(cacheFile)) {
|
||||
haxe.Json.parse(File.getContent(cacheFile));
|
||||
} else {
|
||||
{};
|
||||
}
|
||||
}
|
||||
|
||||
if (expCache.exists(str)) {
|
||||
return Right(Context.parse(expCache[str], Helpers.macroPos(exp)));
|
||||
if (cache.cachedExps.exists(str)) {
|
||||
return Right(cache.cachedExps[str]);
|
||||
}
|
||||
}
|
||||
#end
|
||||
@@ -682,11 +685,10 @@ class Kiss {
|
||||
#if kissCache
|
||||
if (!macroExpandOnly) {
|
||||
if (conversionTime > cacheThreshold && !k.stateChanged) {
|
||||
expCache[str] = switch (expr) {
|
||||
case Right(expr): expr.toString();
|
||||
cache.cachedExps[str] = switch (expr) {
|
||||
case Right(expr): expr;
|
||||
default: throw "macroExpandAndConvert is broken";
|
||||
}
|
||||
File.saveContent(cacheFile, haxe.Json.stringify(expCache));
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
35
src/kiss/KissCache.hx
Normal file
35
src/kiss/KissCache.hx
Normal file
@@ -0,0 +1,35 @@
|
||||
package kiss;
|
||||
|
||||
#if macro
|
||||
import kiss.Kiss;
|
||||
import kiss.Prelude;
|
||||
import sys.FileSystem;
|
||||
import haxe.macro.Expr;
|
||||
|
||||
typedef CachedFile = {
|
||||
path:String,
|
||||
loadedFiles:Array<String>,
|
||||
timeLastCompiled:Date,
|
||||
compiledState:KissState,
|
||||
compiledCode:Null<ReaderExp>
|
||||
};
|
||||
|
||||
@:allow(kiss.Kiss)
|
||||
class KissCache {
|
||||
var cachedFiles:Map<String,CachedFile> = [];
|
||||
var cachedExps:Map<String,Expr> = [];
|
||||
|
||||
function new() {}
|
||||
|
||||
function needsRecompile(file:String) {
|
||||
var cachedFile = cachedFiles[file];
|
||||
var latestModifiedTime = Prelude._max([
|
||||
for (file in [file].concat(cachedFile.loadedFiles)) {
|
||||
FileSystem.stat(file).mtime.getTime();
|
||||
}
|
||||
]);
|
||||
return latestModifiedTime > cachedFile.timeLastCompiled.getTime();
|
||||
}
|
||||
}
|
||||
|
||||
#end
|
@@ -158,7 +158,7 @@ class Prelude {
|
||||
return Math.pow(base, exponent);
|
||||
}
|
||||
|
||||
static function _min(values:Array<Dynamic>):Dynamic {
|
||||
public static function _min(values:Array<Dynamic>):Dynamic {
|
||||
var min = values[0];
|
||||
for (value in values.slice(1))
|
||||
min = Math.min(min, value);
|
||||
@@ -167,7 +167,7 @@ class Prelude {
|
||||
|
||||
public static var min:Function = makeVarArgsWithArrayCheck(_min, "min");
|
||||
|
||||
static function _max(values:Array<Dynamic>):Dynamic {
|
||||
public static function _max(values:Array<Dynamic>):Dynamic {
|
||||
var max = values[0];
|
||||
for (value in values.slice(1)) {
|
||||
max = Math.max(max, value);
|
||||
|
12
src/test/cases/KissCacheTestCase.hx
Normal file
12
src/test/cases/KissCacheTestCase.hx
Normal file
@@ -0,0 +1,12 @@
|
||||
package test.cases;
|
||||
|
||||
import utest.Test;
|
||||
import utest.Assert;
|
||||
import kiss.Prelude;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class KissCacheTestCase extends Test {
|
||||
public function testFirstCompile() {
|
||||
_testFirstCompile();
|
||||
}
|
||||
}
|
2
src/test/cases/KissCacheTestCase.kiss
Normal file
2
src/test/cases/KissCacheTestCase.kiss
Normal file
@@ -0,0 +1,2 @@
|
||||
(function _testFirstCompile []
|
||||
(Assert.pass))
|
13
test.sh
13
test.sh
@@ -16,7 +16,18 @@ elif [ "$KISS_TARGET" = nodejs ]; then
|
||||
fi
|
||||
|
||||
if [ ! -z "$2" ]; then
|
||||
haxe -D cases=$2 build-scripts/common-args.hxml build-scripts/common-test-args.hxml build-scripts/$KISS_TARGET/test.hxml
|
||||
FLAGS=""
|
||||
TWICE=""
|
||||
if [ "$2" = "KissCacheTestCase" ]; then
|
||||
haxe --wait 6000 || echo "server already running" &
|
||||
TWICE=1
|
||||
FLAGS="--connect 6001 -D kissCache"
|
||||
fi
|
||||
|
||||
haxe $FLAGS -D cases=$2 build-scripts/common-args.hxml build-scripts/common-test-args.hxml build-scripts/$KISS_TARGET/test.hxml
|
||||
if [ -n "$TWICE" ]; then
|
||||
haxe $FLAGS -D cases=$2 build-scripts/common-args.hxml build-scripts/common-test-args.hxml build-scripts/$KISS_TARGET/test.hxml
|
||||
fi
|
||||
else
|
||||
haxe build-scripts/common-args.hxml build-scripts/common-test-args.hxml build-scripts/$KISS_TARGET/test.hxml
|
||||
fi
|
Reference in New Issue
Block a user