also looking at this
Migrate to zig 0.16
#5
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.
| @@ -3,11 +3,7 @@ const Regex = .Regex; | ||
| 3 | 3 | const Profiler = .Profiler; |
| 4 | 4 | const ScopedTimer = .ScopedTimer; |
| 5 | 5 | |
| 6 | pub fn main() !void { | |
| 7 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 8 | defer _ = gpa.deinit(); | |
| 9 | const allocator = gpa.allocator(); | |
| 10 | ||
| 6 | pub fn main(init: std.process.Init) !void { | |
| 11 | 7 | std.debug.print("\n=== Regex Profiling Examples ===\n\n", .{}); |
| 12 | 8 | |
| 13 | 9 | // Example 1: Basic profiling |
| @@ -16,12 +12,12 @@ pub fn main() !void { | ||
| 16 | 12 | std.debug.print("Example 1: Basic profiling\n", .{}); |
| 17 | 13 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 18 | 14 | |
| 19 | var profiler = Profiler.init(allocator, true); | |
| 15 | var profiler = Profiler.init(init.gpa, init.io, true); | |
| 20 | 16 | |
| 21 | 17 | // 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); | |
| 25 | 21 | defer regex.deinit(); |
| 26 | 22 | |
| 27 | 23 | // Profile multiple matches |
| @@ -33,9 +29,9 @@ pub fn main() !void { | ||
| 33 | 29 | }; |
| 34 | 30 | |
| 35 | 31 | for (test_inputs) |input| { |
| 36 | profiler.startMatch(); | |
| 32 | profiler.startMatch(init.io); | |
| 37 | 33 | _ = try regex.isMatch(input); |
| 38 | profiler.endMatch(); | |
| 34 | profiler.endMatch(init.io); | |
| 39 | 35 | } |
| 40 | 36 | |
| 41 | 37 | std.debug.print("\n", .{}); |
| @@ -48,13 +44,13 @@ pub fn main() !void { | ||
| 48 | 44 | std.debug.print("Example 2: Scoped timers\n", .{}); |
| 49 | 45 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 50 | 46 | |
| 51 | var profiler = Profiler.init(allocator, true); | |
| 47 | var profiler = Profiler.init(init.gpa, init.io, true); | |
| 52 | 48 | |
| 53 | 49 | { |
| 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); | |
| 56 | 52 | |
| 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]+"); | |
| 58 | 54 | regex.deinit(); |
| 59 | 55 | } |
| 60 | 56 | |
| @@ -69,33 +65,34 @@ pub fn main() !void { | ||
| 69 | 65 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 70 | 66 | |
| 71 | 67 | 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 | |
| 75 | 71 | }; |
| 76 | 72 | |
| 77 | 73 | const test_text = ("some random text " ** 10) ++ "hello world"; |
| 78 | 74 | |
| 79 | 75 | for (patterns) |pattern| { |
| 80 | var profiler = Profiler.init(allocator, true); | |
| 76 | var profiler = Profiler.init(init.gpa, init.io, true); | |
| 81 | 77 | |
| 82 | 78 | std.debug.print("\nPattern: \"{s}\"\n", .{pattern}); |
| 83 | 79 | |
| 84 | var regex = try Regex.compile(allocator, pattern); | |
| 80 | var regex = try Regex.compile(init.gpa, pattern); | |
| 85 | 81 | defer regex.deinit(); |
| 86 | 82 | |
| 87 | 83 | // Run multiple matches |
| 88 | 84 | const iterations = 1000; |
| 89 | 85 | var i: usize = 0; |
| 90 | 86 | while (i < iterations) : (i += 1) { |
| 91 | profiler.startMatch(); | |
| 87 | profiler.startMatch(init.io); | |
| 92 | 88 | _ = try regex.isMatch(test_text); |
| 93 | profiler.endMatch(); | |
| 89 | profiler.endMatch(init.io); | |
| 94 | 90 | } |
| 95 | 91 | |
| 96 | 92 | 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 }); | |
| 99 | 96 | } |
| 100 | 97 | } |
| 101 | 98 | |
| @@ -105,12 +102,12 @@ pub fn main() !void { | ||
| 105 | 102 | std.debug.print("Example 4: Memory tracking\n", .{}); |
| 106 | 103 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 107 | 104 | |
| 108 | var profiler = Profiler.init(allocator, true); | |
| 105 | var profiler = Profiler.init(init.gpa, init.io, true); | |
| 109 | 106 | |
| 110 | 107 | const complex_pattern = "(a|b)+c(d|e)*f"; |
| 111 | 108 | std.debug.print("Pattern: \"{s}\"\n", .{complex_pattern}); |
| 112 | 109 | |
| 113 | var regex = try Regex.compile(allocator, complex_pattern); | |
| 110 | var regex = try Regex.compile(init.gpa, complex_pattern); | |
| 114 | 111 | defer regex.deinit(); |
| 115 | 112 | |
| 116 | 113 | // Simulate memory tracking (in real implementation, this would be integrated) |
| @@ -131,30 +128,30 @@ pub fn main() !void { | ||
| 131 | 128 | const test_pattern = "hello"; |
| 132 | 129 | |
| 133 | 130 | // Baseline |
| 134 | var baseline_profiler = Profiler.init(allocator, true); | |
| 131 | var baseline_profiler = Profiler.init(init.gpa, init.io, true); | |
| 135 | 132 | { |
| 136 | var regex = try Regex.compile(allocator, baseline_pattern); | |
| 133 | var regex = try Regex.compile(init.gpa, baseline_pattern); | |
| 137 | 134 | defer regex.deinit(); |
| 138 | 135 | |
| 139 | 136 | var i: usize = 0; |
| 140 | 137 | while (i < 100) : (i += 1) { |
| 141 | baseline_profiler.startMatch(); | |
| 138 | baseline_profiler.startMatch(init.io); | |
| 142 | 139 | _ = try regex.isMatch("hello world"); |
| 143 | baseline_profiler.endMatch(); | |
| 140 | baseline_profiler.endMatch(init.io); | |
| 144 | 141 | } |
| 145 | 142 | } |
| 146 | 143 | |
| 147 | 144 | // Test |
| 148 | var test_profiler = Profiler.init(allocator, true); | |
| 145 | var test_profiler = Profiler.init(init.gpa, init.io, true); | |
| 149 | 146 | { |
| 150 | var regex = try Regex.compile(allocator, test_pattern); | |
| 147 | var regex = try Regex.compile(init.gpa, test_pattern); | |
| 151 | 148 | defer regex.deinit(); |
| 152 | 149 | |
| 153 | 150 | var i: usize = 0; |
| 154 | 151 | while (i < 100) : (i += 1) { |
| 155 | test_profiler.startMatch(); | |
| 152 | test_profiler.startMatch(init.io); | |
| 156 | 153 | _ = try regex.isMatch("hello world"); |
| 157 | test_profiler.endMatch(); | |
| 154 | test_profiler.endMatch(init.io); | |
| 158 | 155 | } |
| 159 | 156 | } |
| 160 | 157 | |
| @@ -162,8 +159,8 @@ pub fn main() !void { | ||
| 162 | 159 | const test_time = test_profiler.getMetrics().match_time_ns; |
| 163 | 160 | const diff_percent = @as(f64, @floatFromInt(test_time)) / @as(f64, @floatFromInt(baseline_time)) * 100.0 - 100.0; |
| 164 | 161 | |
| 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)}); | |
| 167 | 164 | std.debug.print("Difference: {d:.1}%\n", .{diff_percent}); |
| 168 | 165 | |
| 169 | 166 | if (diff_percent > 10.0) { |