@@ -131,8 +131,8 @@ fn run(
131131 args : zlinter.Args ,
132132 printer : * zlinter.rendering.Printer ,
133133) ! RunResult {
134- var timer = Timer .createStarted ();
135- var total_timer = Timer .createStarted ();
134+ var timer = Timer .createStarted (io );
135+ var total_timer = Timer .createStarted (io );
136136
137137 // Key is index to `lint_files` and value are errors for the file.
138138 var file_lint_problems = std .AutoArrayHashMap (
@@ -184,7 +184,7 @@ fn run(
184184 file .excluded = ! index .contains (file .pathname );
185185 }
186186
187- if ( timer . lapMilliseconds ()) | ms | printer .println (.verbose , "Resolving {d} files took: {d}ms" , .{ lint_files .len , ms });
187+ printer .println (.verbose , "Resolving {d} files took: {d}ms" , .{ lint_files .len , timer . lapMilliseconds () });
188188
189189 try runLinterRules (
190190 io ,
@@ -199,7 +199,7 @@ fn run(
199199
200200 printer .printBanner (.verbose );
201201 printer .println (.verbose , "Linted {d} files" , .{lint_files .len });
202- if ( total_timer . lapMilliseconds ()) | ms | printer .println (.verbose , "Took {d}ms" , .{ms });
202+ printer .println (.verbose , "Took {d}ms" , .{total_timer . lapMilliseconds () });
203203 printer .printBanner (.verbose );
204204
205205 // ------------------------------------------------------------------------
@@ -335,16 +335,15 @@ fn runLinterRules(
335335 }
336336 printer .println (.verbose , "[{d}/{d}] Linting: {s}" , .{ i + 1 , lint_files .len , lint_file .pathname });
337337
338- var rule_timer = Timer .createStarted ();
338+ var rule_timer = Timer .createStarted (io );
339339 defer {
340- if (rule_timer .lapNanoseconds ()) | ns | {
341- printer .println (.verbose , " - Total elapsed {d}ms" , .{ns / std .time .ns_per_ms });
342- if (maybe_slowest_files ) | * slowest_files | {
343- slowest_files .add (.{
344- .name = lint_file .pathname ,
345- .elapsed_ns = ns ,
346- });
347- }
340+ const ns = rule_timer .lapNanoseconds ();
341+ printer .println (.verbose , " - Total elapsed {d}ms" , .{ns / std .time .ns_per_ms });
342+ if (maybe_slowest_files ) | * slowest_files | {
343+ slowest_files .add (.{
344+ .name = lint_file .pathname ,
345+ .elapsed_ns = ns ,
346+ });
348347 }
349348 }
350349
@@ -359,10 +358,7 @@ fn runLinterRules(
359358 };
360359 defer doc .deinit (context .gpa );
361360
362- if (timer .lapMilliseconds ()) | ms |
363- printer .println (.verbose , " - Load document: {d}ms" , .{ms })
364- else
365- printer .println (.verbose , " - Load document" , .{});
361+ printer .println (.verbose , " - Load document: {d}ms" , .{timer .lapMilliseconds ()});
366362 printer .println (.verbose , " - {d} bytes" , .{doc .handle .tree .source .len });
367363 printer .println (.verbose , " - {d} nodes" , .{doc .handle .tree .nodes .len });
368364 printer .println (.verbose , " - {d} tokens" , .{doc .handle .tree .tokens .len });
@@ -395,7 +391,7 @@ fn runLinterRules(
395391 },
396392 );
397393 }
398- if ( timer . lapMilliseconds ()) | ms | printer .println (.verbose , " - Process syntax errors: {d}ms" , .{ms });
394+ printer .println (.verbose , " - Process syntax errors: {d}ms" , .{timer . lapMilliseconds () });
399395
400396 printer .println (.verbose , " - Rules" , .{});
401397
@@ -415,12 +411,11 @@ fn runLinterRules(
415411 try results .append (gpa , result );
416412 }
417413
418- if (timer .lapNanoseconds ()) | ns | {
419- if (maybe_rule_elapsed_times ) | * rule_elapsed_time | {
420- rule_elapsed_time [rule_index ] += ns ;
421- }
422- printer .println (.verbose , " - {s}: {d}ms" , .{ rule .rule_id , ns / std .time .ns_per_ms });
423- } else printer .println (.verbose , " - {s}" , .{rule .rule_id });
414+ const ns = timer .lapNanoseconds ();
415+ if (maybe_rule_elapsed_times ) | * rule_elapsed_time | {
416+ rule_elapsed_time [rule_index ] += ns ;
417+ }
418+ printer .println (.verbose , " - {s}: {d}ms" , .{ rule .rule_id , ns / std .time .ns_per_ms });
424419 }
425420
426421 if (results .items .len > 0 ) {
@@ -753,20 +748,26 @@ const RunResult = struct {
753748 const usage_error : RunResult = .{ .exit_code = .usage_error };
754749};
755750
756- /// Simple more forgiving timer for optionally timing laps in verbose mode.
757751const Timer = struct {
758- backing : ? std.time.Timer = null ,
752+ last_timestamp : std.Io.Timestamp ,
753+ io : std.Io ,
759754
760- pub fn createStarted () Timer {
761- return .{ .backing = std .time .Timer .start () catch null };
755+ pub fn createStarted (io : std.Io ) Timer {
756+ return .{
757+ .last_timestamp = std .Io .Clock .now (.awake , io ),
758+ .io = io ,
759+ };
762760 }
763761
764- pub fn lapNanoseconds (self : * Timer ) ? usize {
765- return (self .backing orelse return null ).lap ();
762+ pub fn lapNanoseconds (self : * Timer ) usize {
763+ const current = std .Io .Clock .now (.awake , self .io );
764+ const elapsed = self .last_timestamp .durationTo (current ).toNanoseconds ();
765+ self .last_timestamp = current ;
766+ return @intCast (elapsed );
766767 }
767768
768- pub fn lapMilliseconds (self : * Timer ) ? usize {
769- return ( self .lapNanoseconds () orelse return null ) / std .time .ns_per_ms ;
769+ pub fn lapMilliseconds (self : * Timer ) usize {
770+ return self .lapNanoseconds () / std .time .ns_per_ms ;
770771 }
771772};
772773
0 commit comments