profileKiss print macro, special form, and other statistics

This commit is contained in:
2023-03-13 08:01:00 -06:00
parent bcc5474370
commit 374c25365e
3 changed files with 93 additions and 56 deletions

View File

@@ -322,7 +322,7 @@ class Kiss {
}
//trace('kiss build $kissFile');
return _try(() -> {
var result = _try(() -> {
#if profileKiss
Kiss.measure('Compiling kiss: $kissFile', () -> {
#end
@@ -357,12 +357,22 @@ class Kiss {
});
}
k.fieldList;
#if profileKiss
});
#end
});
for (label => timeSpent in profileAggregates) {
var usageCount = profileUsageCounts[label];
if (timeSpent >= SIGNIFICANT_TIME_SPENT) {
Sys.println('${label} (x${usageCount}): ${timeSpent}');
}
}
#end
k.fieldList;
});
return result;
}
static final SIGNIFICANT_TIME_SPENT = 0.05;
public static function load(kissFile:String, k:KissState, ?loadingDirectory:String, loadAllExps = false, ?fromExp:ReaderExp):Null<ReaderExp> {
if (loadingDirectory == null)
@@ -556,7 +566,7 @@ class Kiss {
checkNumArgs(mac);
macroUsed = true;
var expanded = try {
macros[mac](exp, args.copy(), k);
Kiss.measure(mac, ()->macros[mac](exp, args.copy(), k), true);
} catch (error:KissError) {
throw error;
} catch (error:Dynamic) {
@@ -572,7 +582,7 @@ class Kiss {
};
case CallExp({pos: _, def: Symbol(specialForm)}, args) if (specialForms.exists(specialForm) && !macroExpandOnly):
checkNumArgs(specialForm);
Right(specialForms[specialForm](exp, args.copy(), k));
Right(Kiss.measure(specialForm, ()->specialForms[specialForm](exp, args.copy(), k), true));
case CallExp({pos: _, def: Symbol(alias)}, args) if (k.callAliases.exists(alias)):
convert(CallExp(k.callAliases[alias].withPosOf(exp), args).withPosOf(exp));
case CallExp(func, args):
@@ -771,12 +781,27 @@ class Kiss {
}
}
public static function measure<T>(processLabel:String, process:Void->T) {
static var profileAggregates:Map<String,Float> = [];
static var profileUsageCounts:Map<String,Int> = [];
public static function measure<T>(processLabel:String, process:Void->T, aggregate=false) {
var start = Sys.time();
if (aggregate) {
if (!profileAggregates.exists(processLabel)) {
profileAggregates[processLabel] = 0.0;
profileUsageCounts[processLabel] = 0;
}
} else {
Sys.print('${processLabel}... ');
}
var result = process();
var end = Sys.time();
if (aggregate) {
profileAggregates[processLabel] += (end - start);
profileUsageCounts[processLabel] += 1;
} else {
Sys.println('${end-start}s');
}
return result;
}

View File

@@ -584,6 +584,9 @@ class Prelude {
* So don't use raw string literals in Kiss you want parsed and evaluated at runtime.
*/
public static function convertToHScript(kissStr:String):String {
#if macro
return Kiss.measure("Prelude.convertToHScript", () -> {
#end
#if (!macro && hxnodejs)
var hscript = try {
assertProcess("haxelib", ["run", "kiss", "convert", "--all", "--hscript"], kissStr.split('\n'));
@@ -623,6 +626,9 @@ class Prelude {
#else
throw "Can't convert Kiss to HScript on this target.";
#end
#if macro
}, true);
#end
}
#if (sys || hxnodejs)

View File

@@ -139,6 +139,9 @@ class Stream {
}
public function putBackString(s:String) {
#if macro
Kiss.measure("Stream.putBackString", () -> {
#end
var idx = s.length - 1;
while (idx >= 0) {
absoluteChar -= 1;
@@ -152,6 +155,9 @@ class Stream {
--idx;
}
content = s + content;
#if macro
}, true);
#end
}
public function takeChars(count:Int):Option<String> {