ReviewOS

also looking at this

zig-utils/zig-regex

Port to latest zig-master

#2
Closed armagidon-exception wants to merge main into main
53 files +1149 -1017
examples/profiling_example.zigmodified+20-20
Changes to examples/profiling_example.zig
@@ -3,7 +3,7 @@ const Regex = .Regex;
33const Profiler = .Profiler;
44const ScopedTimer = .ScopedTimer;
55
6pub fn main() !void {
6pub fn main(init: std.process.Init) !void {
77 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
88 defer _ = gpa.deinit();
99 const allocator = gpa.allocator();
@@ -16,11 +16,11 @@ pub fn main() !void {
1616 std.debug.print("Example 1: Basic profiling\n", .{});
1717 std.debug.print("───────────────────────────────────────────────\n", .{});
1818
19 var profiler = Profiler.init(allocator, true);
19 var profiler = Profiler.init(allocator, init.io, true);
2020
2121 // Profile compilation
2222 profiler.startCompilation();
23 var regex = try Regex.compile(allocator, "hello.*world");
23 var regex = try Regex.compile(allocator, init.io, "hello.*world");
2424 profiler.endCompilation();
2525 defer regex.deinit();
2626
@@ -48,13 +48,13 @@ pub fn main() !void {
4848 std.debug.print("Example 2: Scoped timers\n", .{});
4949 std.debug.print("───────────────────────────────────────────────\n", .{});
5050
51 var profiler = Profiler.init(allocator, true);
51 var profiler = Profiler.init(allocator, init.io, true);
5252
5353 {
5454 var timer = ScopedTimer.init(&profiler, .compilation);
5555 defer timer.deinit();
5656
57 var regex = try Regex.compile(allocator, "[a-z]+@[a-z]+\\.[a-z]+");
57 var regex = try Regex.compile(allocator, init.io, "[a-z]+@[a-z]+\\.[a-z]+");
5858 regex.deinit();
5959 }
6060
@@ -69,19 +69,19 @@ pub fn main() !void {
6969 std.debug.print("───────────────────────────────────────────────\n", .{});
7070
7171 const patterns = [_][]const u8{
72 "hello", // Literal - should have prefix optimization
73 ".*hello", // No prefix - slower
74 "hello.*world", // Prefix optimization
72 "hello", // Literal - should have prefix optimization
73 ".*hello", // No prefix - slower
74 "hello.*world", // Prefix optimization
7575 };
7676
7777 const test_text = ("some random text " ** 10) ++ "hello world";
7878
7979 for (patterns) |pattern| {
80 var profiler = Profiler.init(allocator, true);
80 var profiler = Profiler.init(allocator, init.io, true);
8181
8282 std.debug.print("\nPattern: \"{s}\"\n", .{pattern});
8383
84 var regex = try Regex.compile(allocator, pattern);
84 var regex = try Regex.compile(allocator, init.io, pattern);
8585 defer regex.deinit();
8686
8787 // Run multiple matches
@@ -94,8 +94,8 @@ pub fn main() !void {
9494 }
9595
9696 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 });
97 const avg_ns = @divExact(metrics.match_time_ns, iterations);
98 std.debug.print("Average match time: {d}μs ({d} iterations)\n", .{ @divExact(avg_ns, 1000), iterations });
9999 }
100100 }
101101
@@ -105,12 +105,12 @@ pub fn main() !void {
105105 std.debug.print("Example 4: Memory tracking\n", .{});
106106 std.debug.print("───────────────────────────────────────────────\n", .{});
107107
108 var profiler = Profiler.init(allocator, true);
108 var profiler = Profiler.init(allocator, init.io, true);
109109
110110 const complex_pattern = "(a|b)+c(d|e)*f";
111111 std.debug.print("Pattern: \"{s}\"\n", .{complex_pattern});
112112
113 var regex = try Regex.compile(allocator, complex_pattern);
113 var regex = try Regex.compile(allocator, init.io, complex_pattern);
114114 defer regex.deinit();
115115
116116 // Simulate memory tracking (in real implementation, this would be integrated)
@@ -131,9 +131,9 @@ pub fn main() !void {
131131 const test_pattern = "hello";
132132
133133 // Baseline
134 var baseline_profiler = Profiler.init(allocator, true);
134 var baseline_profiler = Profiler.init(allocator, init.io, true);
135135 {
136 var regex = try Regex.compile(allocator, baseline_pattern);
136 var regex = try Regex.compile(allocator, init.io, baseline_pattern);
137137 defer regex.deinit();
138138
139139 var i: usize = 0;
@@ -145,9 +145,9 @@ pub fn main() !void {
145145 }
146146
147147 // Test
148 var test_profiler = Profiler.init(allocator, true);
148 var test_profiler = Profiler.init(allocator, init.io, true);
149149 {
150 var regex = try Regex.compile(allocator, test_pattern);
150 var regex = try Regex.compile(allocator, init.io, test_pattern);
151151 defer regex.deinit();
152152
153153 var i: usize = 0;
@@ -162,8 +162,8 @@ pub fn main() !void {
162162 const test_time = test_profiler.getMetrics().match_time_ns;
163163 const diff_percent = @as(f64, @floatFromInt(test_time)) / @as(f64, @floatFromInt(baseline_time)) * 100.0 - 100.0;
164164
165 std.debug.print("\nBaseline time: {d}μs\n", .{baseline_time / 1000});
166 std.debug.print("Test time: {d}μs\n", .{test_time / 1000});
165 std.debug.print("\nBaseline time: {d}μs\n", .{@divExact(baseline_time, 1000)});
166 std.debug.print("Test time: {d}μs\n", .{@divExact(test_time, 1000)});
167167 std.debug.print("Difference: {d:.1}%\n", .{diff_percent});
168168
169169 if (diff_percent > 10.0) {