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

View File

@@ -584,44 +584,50 @@ class Prelude {
* So don't use raw string literals in Kiss you want parsed and evaluated at runtime. * So don't use raw string literals in Kiss you want parsed and evaluated at runtime.
*/ */
public static function convertToHScript(kissStr:String):String { public static function convertToHScript(kissStr:String):String {
#if (!macro && hxnodejs) #if macro
var hscript = try { return Kiss.measure("Prelude.convertToHScript", () -> {
assertProcess("haxelib", ["run", "kiss", "convert", "--all", "--hscript"], kissStr.split('\n')); #end
} catch (e) { #if (!macro && hxnodejs)
throw 'failed to convert ${kissStr} to hscript:\n$e'; var hscript = try {
} assertProcess("haxelib", ["run", "kiss", "convert", "--all", "--hscript"], kissStr.split('\n'));
if (hscript.startsWith(">>> ")) { } catch (e) {
hscript = hscript.substr(4); throw 'failed to convert ${kissStr} to hscript:\n$e';
}
return hscript.trim();
#elseif (!macro && python)
var hscript = try {
assertProcess("haxelib", ["run", "kiss", "convert", "--hscript"], [kissStr.replace('\n', ' ')], false);
} catch (e) {
throw 'failed to convert ${kissStr} to hscript:\n$e';
}
if (hscript.startsWith(">>> ")) {
hscript = hscript.substr(4);
}
return hscript.trim();
#elseif sys
if (kissProcess == null)
kissProcess = new Process("haxelib", ["run", "kiss", "convert", "--hscript"]);
kissProcess.stdin.writeString('${kissStr.replace("\n", " ")}\n');
try {
var output = kissProcess.stdout.readLine();
if (output.startsWith(">>> ")) {
output = output.substr(4);
} }
return output; if (hscript.startsWith(">>> ")) {
} catch (e) { hscript = hscript.substr(4);
var error = kissProcess.stderr.readAll().toString(); }
throw 'failed to convert ${kissStr} to hscript: ${error}'; return hscript.trim();
} #elseif (!macro && python)
#else var hscript = try {
throw "Can't convert Kiss to HScript on this target."; assertProcess("haxelib", ["run", "kiss", "convert", "--hscript"], [kissStr.replace('\n', ' ')], false);
} catch (e) {
throw 'failed to convert ${kissStr} to hscript:\n$e';
}
if (hscript.startsWith(">>> ")) {
hscript = hscript.substr(4);
}
return hscript.trim();
#elseif sys
if (kissProcess == null)
kissProcess = new Process("haxelib", ["run", "kiss", "convert", "--hscript"]);
kissProcess.stdin.writeString('${kissStr.replace("\n", " ")}\n');
try {
var output = kissProcess.stdout.readLine();
if (output.startsWith(">>> ")) {
output = output.substr(4);
}
return output;
} catch (e) {
var error = kissProcess.stderr.readAll().toString();
throw 'failed to convert ${kissStr} to hscript: ${error}';
}
#else
throw "Can't convert Kiss to HScript on this target.";
#end
#if macro
}, true);
#end #end
} }

View File

@@ -139,19 +139,25 @@ class Stream {
} }
public function putBackString(s:String) { public function putBackString(s:String) {
var idx = s.length - 1; #if macro
while (idx >= 0) { Kiss.measure("Stream.putBackString", () -> {
absoluteChar -= 1; #end
switch (s.charAt(idx)) { var idx = s.length - 1;
case "\n": while (idx >= 0) {
line -= 1; absoluteChar -= 1;
column = lineLengths.pop(); switch (s.charAt(idx)) {
default: case "\n":
column -= 1; line -= 1;
column = lineLengths.pop();
default:
column -= 1;
}
--idx;
} }
--idx; content = s + content;
} #if macro
content = s + content; }, true);
#end
} }
public function takeChars(count:Int):Option<String> { public function takeChars(count:Int):Option<String> {