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.
| @@ -1,11 +1,7 @@ | ||
| 1 | 1 | const std = ; |
| 2 | 2 | const Regex = .Regex; |
| 3 | 3 | |
| 4 | pub fn main() !void { | |
| 5 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 6 | defer _ = gpa.deinit(); | |
| 7 | const allocator = gpa.allocator(); | |
| 8 | ||
| 4 | pub fn main(init: std.process.Init) !void { | |
| 9 | 5 | std.debug.print("=== Literal Prefix Optimization Benchmarks ===\n\n", .{}); |
| 10 | 6 | |
| 11 | 7 | // Create a long text where the pattern appears at the end |
| @@ -14,7 +10,7 @@ pub fn main() !void { | ||
| 14 | 10 | // Test 1: Pattern with literal prefix (should use optimization) |
| 15 | 11 | { |
| 16 | 12 | std.debug.print("Test 1: Literal prefix optimization (FOUND_IT!)...\n", .{}); |
| 17 | var regex = try Regex.compile(allocator, "FOUND_IT!"); | |
| 13 | var regex = try Regex.compile(init.gpa, "FOUND_IT!"); | |
| 18 | 14 | defer regex.deinit(); |
| 19 | 15 | |
| 20 | 16 | // Print optimization info |
| @@ -24,31 +20,32 @@ pub fn main() !void { | ||
| 24 | 20 | std.debug.print(" ✗ No literal prefix found\n", .{}); |
| 25 | 21 | } |
| 26 | 22 | |
| 27 | var timer = try std.time.Timer.start(); | |
| 28 | const start = timer.read(); | |
| 23 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 29 | 24 | |
| 30 | 25 | const iterations: usize = 10000; |
| 31 | 26 | var i: usize = 0; |
| 32 | 27 | while (i < iterations) : (i += 1) { |
| 33 | 28 | if (try regex.find(long_text)) |match| { |
| 34 | 29 | var mut_match = match; |
| 35 | mut_match.deinit(allocator); | |
| 30 | mut_match.deinit(init.gpa); | |
| 36 | 31 | } |
| 37 | 32 | } |
| 38 | 33 | |
| 39 | const elapsed = timer.read() - start; | |
| 40 | const avg_ns = elapsed / iterations; | |
| 34 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 35 | const elapsed = start.durationTo(end); | |
| 36 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 37 | ||
| 41 | 38 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 42 | 39 | iterations, |
| 43 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 44 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 40 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 41 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 45 | 42 | }); |
| 46 | 43 | } |
| 47 | 44 | |
| 48 | 45 | // Test 2: Pattern without useful prefix (no optimization) - using fewer iterations |
| 49 | 46 | { |
| 50 | 47 | std.debug.print("Test 2: No prefix optimization (.*FOUND)...\n", .{}); |
| 51 | var regex = try Regex.compile(allocator, ".*FOUND"); | |
| 48 | var regex = try Regex.compile(init.gpa, ".*FOUND"); | |
| 52 | 49 | defer regex.deinit(); |
| 53 | 50 | |
| 54 | 51 | if (regex.opt_info.literal_prefix) |prefix| { |
| @@ -57,8 +54,7 @@ pub fn main() !void { | ||
| 57 | 54 | std.debug.print(" ✗ No literal prefix found (slower performance expected)\n", .{}); |
| 58 | 55 | } |
| 59 | 56 | |
| 60 | var timer = try std.time.Timer.start(); | |
| 61 | const start = timer.read(); | |
| 57 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 62 | 58 | |
| 63 | 59 | // Use fewer iterations since this is much slower without optimization |
| 64 | 60 | const iterations: usize = 100; |
| @@ -66,16 +62,18 @@ pub fn main() !void { | ||
| 66 | 62 | while (i < iterations) : (i += 1) { |
| 67 | 63 | if (try regex.find(long_text)) |match| { |
| 68 | 64 | var mut_match = match; |
| 69 | mut_match.deinit(allocator); | |
| 65 | mut_match.deinit(init.gpa); | |
| 70 | 66 | } |
| 71 | 67 | } |
| 72 | 68 | |
| 73 | const elapsed = timer.read() - start; | |
| 74 | const avg_ns = elapsed / iterations; | |
| 69 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 70 | const elapsed = start.durationTo(end); | |
| 71 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 72 | ||
| 75 | 73 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 76 | 74 | iterations, |
| 77 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 78 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 75 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 76 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 79 | 77 | }); |
| 80 | 78 | } |
| 81 | 79 | |
| @@ -83,7 +81,7 @@ pub fn main() !void { | ||
| 83 | 81 | { |
| 84 | 82 | std.debug.print("Test 3: Prefix with complex suffix (hello.*world)...\n", .{}); |
| 85 | 83 | const text_with_hello = "blah blah " ** 50 ++ "hello there world"; |
| 86 | var regex = try Regex.compile(allocator, "hello.*world"); | |
| 84 | var regex = try Regex.compile(init.gpa, "hello.*world"); | |
| 87 | 85 | defer regex.deinit(); |
| 88 | 86 | |
| 89 | 87 | if (regex.opt_info.literal_prefix) |prefix| { |
| @@ -92,31 +90,31 @@ pub fn main() !void { | ||
| 92 | 90 | std.debug.print(" ✗ No literal prefix found\n", .{}); |
| 93 | 91 | } |
| 94 | 92 | |
| 95 | var timer = try std.time.Timer.start(); | |
| 96 | const start = timer.read(); | |
| 93 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 97 | 94 | |
| 98 | 95 | const iterations: usize = 10000; |
| 99 | 96 | var i: usize = 0; |
| 100 | 97 | while (i < iterations) : (i += 1) { |
| 101 | 98 | if (try regex.find(text_with_hello)) |match| { |
| 102 | 99 | var mut_match = match; |
| 103 | mut_match.deinit(allocator); | |
| 100 | mut_match.deinit(init.gpa); | |
| 104 | 101 | } |
| 105 | 102 | } |
| 106 | 103 | |
| 107 | const elapsed = timer.read() - start; | |
| 108 | const avg_ns = elapsed / iterations; | |
| 104 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 105 | const elapsed = start.durationTo(end); | |
| 106 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 109 | 107 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 110 | 108 | iterations, |
| 111 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 112 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 109 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 110 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 113 | 111 | }); |
| 114 | 112 | } |
| 115 | 113 | |
| 116 | 114 | // Test 4: Anchored patterns |
| 117 | 115 | { |
| 118 | 116 | std.debug.print("Test 4: Start-anchored pattern (^hello)...\n", .{}); |
| 119 | var regex = try Regex.compile(allocator, "^hello"); | |
| 117 | var regex = try Regex.compile(init.gpa, "^hello"); | |
| 120 | 118 | defer regex.deinit(); |
| 121 | 119 | |
| 122 | 120 | if (regex.opt_info.anchored_start) { |
| @@ -127,8 +125,7 @@ pub fn main() !void { | ||
| 127 | 125 | } |
| 128 | 126 | |
| 129 | 127 | const text = "hello world"; |
| 130 | var timer = try std.time.Timer.start(); | |
| 131 | const start = timer.read(); | |
| 128 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 132 | 129 | |
| 133 | 130 | const iterations: usize = 10000; |
| 134 | 131 | var i: usize = 0; |
| @@ -136,12 +133,13 @@ pub fn main() !void { | ||
| 136 | 133 | _ = try regex.isMatch(text); |
| 137 | 134 | } |
| 138 | 135 | |
| 139 | const elapsed = timer.read() - start; | |
| 140 | const avg_ns = elapsed / iterations; | |
| 136 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 137 | const elapsed = start.durationTo(end); | |
| 138 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 141 | 139 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 142 | 140 | iterations, |
| 143 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 144 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 141 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 142 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 145 | 143 | }); |
| 146 | 144 | } |
| 147 | 145 | |
| @@ -149,31 +147,31 @@ pub fn main() !void { | ||
| 149 | 147 | { |
| 150 | 148 | std.debug.print("Test 5: Email pattern (user@example.com)...\n", .{}); |
| 151 | 149 | const email_text = "Contact us at " ++ ("some filler text here. " ** 20) ++ "user@example.com for support"; |
| 152 | var regex = try Regex.compile(allocator, "user@example.com"); | |
| 150 | var regex = try Regex.compile(init.gpa, "user@example.com"); | |
| 153 | 151 | defer regex.deinit(); |
| 154 | 152 | |
| 155 | 153 | if (regex.opt_info.literal_prefix) |prefix| { |
| 156 | std.debug.print(" ✓ Using literal prefix: \"{s}\" (min_len={d})\n", .{prefix, regex.opt_info.min_length}); | |
| 154 | std.debug.print(" ✓ Using literal prefix: \"{s}\" (min_len={d})\n", .{ prefix, regex.opt_info.min_length }); | |
| 157 | 155 | } |
| 158 | 156 | |
| 159 | var timer = try std.time.Timer.start(); | |
| 160 | const start = timer.read(); | |
| 157 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 161 | 158 | |
| 162 | 159 | const iterations: usize = 10000; |
| 163 | 160 | var i: usize = 0; |
| 164 | 161 | while (i < iterations) : (i += 1) { |
| 165 | 162 | if (try regex.find(email_text)) |match| { |
| 166 | 163 | var mut_match = match; |
| 167 | mut_match.deinit(allocator); | |
| 164 | mut_match.deinit(init.gpa); | |
| 168 | 165 | } |
| 169 | 166 | } |
| 170 | 167 | |
| 171 | const elapsed = timer.read() - start; | |
| 172 | const avg_ns = elapsed / iterations; | |
| 168 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 169 | const elapsed = start.durationTo(end); | |
| 170 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 173 | 171 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 174 | 172 | iterations, |
| 175 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 176 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 173 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 174 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 177 | 175 | }); |
| 178 | 176 | } |
| 179 | 177 | |