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.

examples/profiling_example.zigmodified+33-36
Changes to examples/profiling_example.zig
@@ -3,11 +3,7 @@ const Regex = .Regex;
33const Profiler = .Profiler;
44const ScopedTimer = .ScopedTimer;
55
6pub fn main() !void {
7 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
8 defer _ = gpa.deinit();
9 const allocator = gpa.allocator();
10
6pub fn main(init: std.process.Init) !void {
117 std.debug.print("\n=== Regex Profiling Examples ===\n\n", .{});
128
139 // Example 1: Basic profiling
@@ -16,12 +12,12 @@ pub fn main() !void {
1612 std.debug.print("Example 1: Basic profiling\n", .{});
1713 std.debug.print("───────────────────────────────────────────────\n", .{});
1814
19 var profiler = Profiler.init(allocator, true);
15 var profiler = Profiler.init(init.gpa, init.io, true);
2016
2117 // Profile compilation
22 profiler.startCompilation();
23 var regex = try Regex.compile(allocator, "hello.*world");
24 profiler.endCompilation();
18 profiler.startCompilation(init.io);
19 var regex = try Regex.compile(init.gpa, "hello.*world");
20 profiler.endCompilation(init.io);
2521 defer regex.deinit();
2622
2723 // Profile multiple matches
@@ -33,9 +29,9 @@ pub fn main() !void {
3329 };
3430
3531 for (test_inputs) |input| {
36 profiler.startMatch();
32 profiler.startMatch(init.io);
3733 _ = try regex.isMatch(input);
38 profiler.endMatch();
34 profiler.endMatch(init.io);
3935 }
4036
4137 std.debug.print("\n", .{});
@@ -48,13 +44,13 @@ pub fn main() !void {
4844 std.debug.print("Example 2: Scoped timers\n", .{});
4945 std.debug.print("───────────────────────────────────────────────\n", .{});
5046
51 var profiler = Profiler.init(allocator, true);
47 var profiler = Profiler.init(init.gpa, init.io, true);
5248
5349 {
54 var timer = ScopedTimer.init(&profiler, .compilation);
55 defer timer.deinit();
50 var timer = ScopedTimer.init(&profiler, init.io, .compilation);
51 defer timer.deinit(init.io);
5652
57 var regex = try Regex.compile(allocator, "[a-z]+@[a-z]+\\.[a-z]+");
53 var regex = try Regex.compile(init.gpa, "[a-z]+@[a-z]+\\.[a-z]+");
5854 regex.deinit();
5955 }
6056
@@ -69,33 +65,34 @@ pub fn main() !void {
6965 std.debug.print("───────────────────────────────────────────────\n", .{});
7066
7167 const patterns = [_][]const u8{
72 "hello", // Literal - should have prefix optimization
73 ".*hello", // No prefix - slower
74 "hello.*world", // Prefix optimization
68 "hello", // Literal - should have prefix optimization
69 ".*hello", // No prefix - slower
70 "hello.*world", // Prefix optimization
7571 };
7672
7773 const test_text = ("some random text " ** 10) ++ "hello world";
7874
7975 for (patterns) |pattern| {
80 var profiler = Profiler.init(allocator, true);
76 var profiler = Profiler.init(init.gpa, init.io, true);
8177
8278 std.debug.print("\nPattern: \"{s}\"\n", .{pattern});
8379
84 var regex = try Regex.compile(allocator, pattern);
80 var regex = try Regex.compile(init.gpa, pattern);
8581 defer regex.deinit();
8682
8783 // Run multiple matches
8884 const iterations = 1000;
8985 var i: usize = 0;
9086 while (i < iterations) : (i += 1) {
91 profiler.startMatch();
87 profiler.startMatch(init.io);
9288 _ = try regex.isMatch(test_text);
93 profiler.endMatch();
89 profiler.endMatch(init.io);
9490 }
9591
9692 const metrics = profiler.getMetrics();
97 const avg_ns = metrics.match_time_ns / iterations;
98 std.debug.print("Average match time: {d}μs ({d} iterations)\n", .{ avg_ns / 1000, iterations });
93 // TODO: figure out if this is correct
94 const avg_ns = @divTrunc(metrics.match_time_ns, iterations);
95 std.debug.print("Average match time: {d}μs ({d} iterations)\n", .{ @divTrunc(avg_ns, 1000), iterations });
9996 }
10097 }
10198
@@ -105,12 +102,12 @@ pub fn main() !void {
105102 std.debug.print("Example 4: Memory tracking\n", .{});
106103 std.debug.print("───────────────────────────────────────────────\n", .{});
107104
108 var profiler = Profiler.init(allocator, true);
105 var profiler = Profiler.init(init.gpa, init.io, true);
109106
110107 const complex_pattern = "(a|b)+c(d|e)*f";
111108 std.debug.print("Pattern: \"{s}\"\n", .{complex_pattern});
112109
113 var regex = try Regex.compile(allocator, complex_pattern);
110 var regex = try Regex.compile(init.gpa, complex_pattern);
114111 defer regex.deinit();
115112
116113 // Simulate memory tracking (in real implementation, this would be integrated)
@@ -131,30 +128,30 @@ pub fn main() !void {
131128 const test_pattern = "hello";
132129
133130 // Baseline
134 var baseline_profiler = Profiler.init(allocator, true);
131 var baseline_profiler = Profiler.init(init.gpa, init.io, true);
135132 {
136 var regex = try Regex.compile(allocator, baseline_pattern);
133 var regex = try Regex.compile(init.gpa, baseline_pattern);
137134 defer regex.deinit();
138135
139136 var i: usize = 0;
140137 while (i < 100) : (i += 1) {
141 baseline_profiler.startMatch();
138 baseline_profiler.startMatch(init.io);
142139 _ = try regex.isMatch("hello world");
143 baseline_profiler.endMatch();
140 baseline_profiler.endMatch(init.io);
144141 }
145142 }
146143
147144 // Test
148 var test_profiler = Profiler.init(allocator, true);
145 var test_profiler = Profiler.init(init.gpa, init.io, true);
149146 {
150 var regex = try Regex.compile(allocator, test_pattern);
147 var regex = try Regex.compile(init.gpa, test_pattern);
151148 defer regex.deinit();
152149
153150 var i: usize = 0;
154151 while (i < 100) : (i += 1) {
155 test_profiler.startMatch();
152 test_profiler.startMatch(init.io);
156153 _ = try regex.isMatch("hello world");
157 test_profiler.endMatch();
154 test_profiler.endMatch(init.io);
158155 }
159156 }
160157
@@ -162,8 +159,8 @@ pub fn main() !void {
162159 const test_time = test_profiler.getMetrics().match_time_ns;
163160 const diff_percent = @as(f64, @floatFromInt(test_time)) / @as(f64, @floatFromInt(baseline_time)) * 100.0 - 100.0;
164161
165 std.debug.print("\nBaseline time: {d}μs\n", .{baseline_time / 1000});
166 std.debug.print("Test time: {d}μs\n", .{test_time / 1000});
162 std.debug.print("\nBaseline time: {d}μs\n", .{@divTrunc(baseline_time, 1000)});
163 std.debug.print("Test time: {d}μs\n", .{@divTrunc(test_time, 1000)});
167164 std.debug.print("Difference: {d:.1}%\n", .{diff_percent});
168165
169166 if (diff_percent > 10.0) {