also looking at this
Port to latest zig-master
#2
53 files
+1149
-1017
| @@ -3,7 +3,7 @@ const Regex = .Regex; | ||
| 3 | 3 | const Profiler = .Profiler; |
| 4 | 4 | const ScopedTimer = .ScopedTimer; |
| 5 | 5 | |
| 6 | pub fn main() !void { | |
| 6 | pub fn main(init: std.process.Init) !void { | |
| 7 | 7 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 8 | 8 | defer _ = gpa.deinit(); |
| 9 | 9 | const allocator = gpa.allocator(); |
| @@ -16,11 +16,11 @@ pub fn main() !void { | ||
| 16 | 16 | std.debug.print("Example 1: Basic profiling\n", .{}); |
| 17 | 17 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 18 | 18 | |
| 19 | var profiler = Profiler.init(allocator, true); | |
| 19 | var profiler = Profiler.init(allocator, init.io, true); | |
| 20 | 20 | |
| 21 | 21 | // Profile compilation |
| 22 | 22 | profiler.startCompilation(); |
| 23 | var regex = try Regex.compile(allocator, "hello.*world"); | |
| 23 | var regex = try Regex.compile(allocator, init.io, "hello.*world"); | |
| 24 | 24 | profiler.endCompilation(); |
| 25 | 25 | defer regex.deinit(); |
| 26 | 26 | |
| @@ -48,13 +48,13 @@ pub fn main() !void { | ||
| 48 | 48 | std.debug.print("Example 2: Scoped timers\n", .{}); |
| 49 | 49 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 50 | 50 | |
| 51 | var profiler = Profiler.init(allocator, true); | |
| 51 | var profiler = Profiler.init(allocator, init.io, true); | |
| 52 | 52 | |
| 53 | 53 | { |
| 54 | 54 | var timer = ScopedTimer.init(&profiler, .compilation); |
| 55 | 55 | defer timer.deinit(); |
| 56 | 56 | |
| 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]+"); | |
| 58 | 58 | regex.deinit(); |
| 59 | 59 | } |
| 60 | 60 | |
| @@ -69,19 +69,19 @@ pub fn main() !void { | ||
| 69 | 69 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 70 | 70 | |
| 71 | 71 | 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 | |
| 75 | 75 | }; |
| 76 | 76 | |
| 77 | 77 | const test_text = ("some random text " ** 10) ++ "hello world"; |
| 78 | 78 | |
| 79 | 79 | for (patterns) |pattern| { |
| 80 | var profiler = Profiler.init(allocator, true); | |
| 80 | var profiler = Profiler.init(allocator, init.io, true); | |
| 81 | 81 | |
| 82 | 82 | std.debug.print("\nPattern: \"{s}\"\n", .{pattern}); |
| 83 | 83 | |
| 84 | var regex = try Regex.compile(allocator, pattern); | |
| 84 | var regex = try Regex.compile(allocator, init.io, pattern); | |
| 85 | 85 | defer regex.deinit(); |
| 86 | 86 | |
| 87 | 87 | // Run multiple matches |
| @@ -94,8 +94,8 @@ pub fn main() !void { | ||
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | 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 }); | |
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | 101 | |
| @@ -105,12 +105,12 @@ pub fn main() !void { | ||
| 105 | 105 | std.debug.print("Example 4: Memory tracking\n", .{}); |
| 106 | 106 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 107 | 107 | |
| 108 | var profiler = Profiler.init(allocator, true); | |
| 108 | var profiler = Profiler.init(allocator, init.io, true); | |
| 109 | 109 | |
| 110 | 110 | const complex_pattern = "(a|b)+c(d|e)*f"; |
| 111 | 111 | std.debug.print("Pattern: \"{s}\"\n", .{complex_pattern}); |
| 112 | 112 | |
| 113 | var regex = try Regex.compile(allocator, complex_pattern); | |
| 113 | var regex = try Regex.compile(allocator, init.io, complex_pattern); | |
| 114 | 114 | defer regex.deinit(); |
| 115 | 115 | |
| 116 | 116 | // Simulate memory tracking (in real implementation, this would be integrated) |
| @@ -131,9 +131,9 @@ pub fn main() !void { | ||
| 131 | 131 | const test_pattern = "hello"; |
| 132 | 132 | |
| 133 | 133 | // Baseline |
| 134 | var baseline_profiler = Profiler.init(allocator, true); | |
| 134 | var baseline_profiler = Profiler.init(allocator, init.io, true); | |
| 135 | 135 | { |
| 136 | var regex = try Regex.compile(allocator, baseline_pattern); | |
| 136 | var regex = try Regex.compile(allocator, init.io, baseline_pattern); | |
| 137 | 137 | defer regex.deinit(); |
| 138 | 138 | |
| 139 | 139 | var i: usize = 0; |
| @@ -145,9 +145,9 @@ pub fn main() !void { | ||
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | // Test |
| 148 | var test_profiler = Profiler.init(allocator, true); | |
| 148 | var test_profiler = Profiler.init(allocator, init.io, true); | |
| 149 | 149 | { |
| 150 | var regex = try Regex.compile(allocator, test_pattern); | |
| 150 | var regex = try Regex.compile(allocator, init.io, test_pattern); | |
| 151 | 151 | defer regex.deinit(); |
| 152 | 152 | |
| 153 | 153 | var i: usize = 0; |
| @@ -162,8 +162,8 @@ pub fn main() !void { | ||
| 162 | 162 | const test_time = test_profiler.getMetrics().match_time_ns; |
| 163 | 163 | const diff_percent = @as(f64, @floatFromInt(test_time)) / @as(f64, @floatFromInt(baseline_time)) * 100.0 - 100.0; |
| 164 | 164 | |
| 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)}); | |
| 167 | 167 | std.debug.print("Difference: {d:.1}%\n", .{diff_percent}); |
| 168 | 168 | |
| 169 | 169 | if (diff_percent > 10.0) { |