ReviewOS

also looking at this

zig-utils/zig-regex

Migrate to zig 0.16

#5
Closed illya-laifu wants to merge zig-0.16-migrations into main
13 files +222 -239

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

src/profiling.zigmodified+28-25
Changes to src/profiling.zig
@@ -5,11 +5,11 @@ pub const Profiler = struct {
55 allocator: std.mem.Allocator,
66 enabled: bool,
77 metrics: Metrics,
8 start_time: ?std.time.Instant,
8 start_time: ?std.Io.Timestamp,
99
1010 pub const Metrics = struct {
11 compilation_time_ns: u64 = 0,
12 match_time_ns: u64 = 0,
11 compilation_time_ns: i96 = 0,
12 match_time_ns: i96 = 0,
1313 match_count: usize = 0,
1414 nfa_states_created: usize = 0,
1515 backtrack_count: usize = 0,
@@ -49,41 +49,42 @@ pub const Profiler = struct {
4949 }
5050 };
5151
52 pub fn init(allocator: std.mem.Allocator, enabled: bool) Profiler {
52 pub fn init(allocator: std.mem.Allocator, io: std.Io, enabled: bool) Profiler {
5353 return .{
5454 .allocator = allocator,
5555 .enabled = enabled,
5656 .metrics = Metrics{},
57 .start_time = if (enabled) std.time.Instant.now() catch null else null,
57 // TODO: No clue what to actually pass as a clock
58 .start_time = if (enabled) std.Io.Timestamp.now(io, .real) else null,
5859 };
5960 }
6061
6162 /// Start timing a compilation phase
62 pub fn startCompilation(self: *Profiler) void {
63 pub fn startCompilation(self: *Profiler, io: std.Io) void {
6364 if (!self.enabled) return;
64 self.start_time = std.time.Instant.now() catch null;
65 self.start_time = std.Io.Timestamp.now(io, .real);
6566 }
6667
6768 /// End timing a compilation phase
68 pub fn endCompilation(self: *Profiler) void {
69 pub fn endCompilation(self: *Profiler, io: std.Io) void {
6970 if (!self.enabled) return;
7071 const start = self.start_time orelse return;
71 const now = std.time.Instant.now() catch return;
72 self.metrics.compilation_time_ns += now.since(start);
72 const now = std.Io.Timestamp.now(io, .real);
73 self.metrics.compilation_time_ns += start.durationTo(now).nanoseconds;
7374 }
7475
7576 /// Start timing a match operation
76 pub fn startMatch(self: *Profiler) void {
77 pub fn startMatch(self: *Profiler, io: std.Io) void {
7778 if (!self.enabled) return;
78 self.start_time = std.time.Instant.now() catch null;
79 self.start_time = std.Io.Timestamp.now(io, .real);
7980 }
8081
8182 /// End timing a match operation
82 pub fn endMatch(self: *Profiler) void {
83 pub fn endMatch(self: *Profiler, io: std.Io) void {
8384 if (!self.enabled) return;
8485 const start = self.start_time orelse return;
85 const now = std.time.Instant.now() catch return;
86 self.metrics.match_time_ns += now.since(start);
86 const now = std.Io.Timestamp.now(io, .real);
87 self.metrics.match_time_ns += start.durationTo(now).nanoseconds;
8788 self.metrics.match_count += 1;
8889 }
8990
@@ -140,9 +141,9 @@ pub const Profiler = struct {
140141 }
141142
142143 /// Reset all metrics
143 pub fn reset(self: *Profiler) void {
144 pub fn reset(self: *Profiler, io: std.Io) void {
144145 self.metrics.reset();
145 self.start_time = if (self.enabled) std.time.Instant.now() catch null else null;
146 self.start_time = if (self.enabled) std.Io.Timestamp.now(io, .real);
146147 }
147148
148149 /// Print metrics to stderr
@@ -156,26 +157,26 @@ pub const Profiler = struct {
156157pub const ScopedTimer = struct {
157158 profiler: *Profiler,
158159 timer_type: TimerType,
159 start: ?std.time.Instant,
160 start: ?std.Io.Timestamp,
160161
161162 pub const TimerType = enum {
162163 compilation,
163164 match,
164165 };
165166
166 pub fn init(profiler: *Profiler, timer_type: TimerType) ScopedTimer {
167 pub fn init(profiler: *Profiler, io: std.Io, timer_type: TimerType) ScopedTimer {
167168 return .{
168169 .profiler = profiler,
169170 .timer_type = timer_type,
170 .start = if (profiler.enabled) std.time.Instant.now() catch null else null,
171 .start = if (profiler.enabled) std.Io.Timestamp.now(io, .real) else null,
171172 };
172173 }
173174
174 pub fn deinit(self: *ScopedTimer) void {
175 pub fn deinit(self: *ScopedTimer, io: std.Io) void {
175176 if (!self.profiler.enabled) return;
176177 const start = self.start orelse return;
177 const now = std.time.Instant.now() catch return;
178 const elapsed = now.since(start);
178 const now = std.Io.Timestamp.now(io, .real);
179 const elapsed = start.durationTo(now).nanoseconds;
179180 switch (self.timer_type) {
180181 .compilation => self.profiler.metrics.compilation_time_ns += elapsed,
181182 .match => {
@@ -188,7 +189,8 @@ pub const ScopedTimer = struct {
188189
189190test "profiler basic operations" {
190191 const allocator = std.testing.allocator;
191 var profiler = Profiler.init(allocator, true);
192 const io = std.testing.io;
193 var profiler = Profiler.init(allocator, io, true);
192194
193195 profiler.startCompilation();
194196 // Do some work (just loop to consume time)
@@ -210,7 +212,8 @@ test "profiler basic operations" {
210212
211213test "scoped timer" {
212214 const allocator = std.testing.allocator;
213 var profiler = Profiler.init(allocator, true);
215 const io = std.testing.io;
216 var profiler = Profiler.init(allocator, io, true);
214217
215218 {
216219 var timer = ScopedTimer.init(&profiler, .match);