also looking at this
Port to latest zig-master
#2
53 files
+1149
-1017
| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | const std = ; |
| 2 | const Clock = std.Io.Clock; | |
| 2 | 3 | const Regex = .Regex; |
| 3 | 4 | |
| 4 | pub fn main() !void { | |
| 5 | pub fn main(init: std.process.Init) !void { | |
| 6 | const io = init.io; | |
| 5 | 7 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 6 | 8 | defer _ = gpa.deinit(); |
| 7 | 9 | const allocator = gpa.allocator(); |
| @@ -14,7 +16,7 @@ pub fn main() !void { | ||
| 14 | 16 | // Test 1: Pattern with literal prefix (should use optimization) |
| 15 | 17 | { |
| 16 | 18 | std.debug.print("Test 1: Literal prefix optimization (FOUND_IT!)...\n", .{}); |
| 17 | var regex = try Regex.compile(allocator, "FOUND_IT!"); | |
| 19 | var regex = try Regex.compile(allocator, io, "FOUND_IT!"); | |
| 18 | 20 | defer regex.deinit(); |
| 19 | 21 | |
| 20 | 22 | // Print optimization info |
| @@ -24,8 +26,7 @@ pub fn main() !void { | ||
| 24 | 26 | std.debug.print(" ✗ No literal prefix found\n", .{}); |
| 25 | 27 | } |
| 26 | 28 | |
| 27 | var timer = try std.time.Timer.start(); | |
| 28 | const start = timer.read(); | |
| 29 | const start = Clock.awake.now(io); | |
| 29 | 30 | |
| 30 | 31 | const iterations: usize = 10000; |
| 31 | 32 | var i: usize = 0; |
| @@ -36,8 +37,8 @@ pub fn main() !void { | ||
| 36 | 37 | } |
| 37 | 38 | } |
| 38 | 39 | |
| 39 | const elapsed = timer.read() - start; | |
| 40 | const avg_ns = elapsed / iterations; | |
| 40 | const elapsed = start.untilNow(io, .awake).toNanoseconds(); | |
| 41 | const avg_ns = @divExact(elapsed, iterations); | |
| 41 | 42 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 42 | 43 | iterations, |
| 43 | 44 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, |
| @@ -48,7 +49,7 @@ pub fn main() !void { | ||
| 48 | 49 | // Test 2: Pattern without useful prefix (no optimization) - using fewer iterations |
| 49 | 50 | { |
| 50 | 51 | std.debug.print("Test 2: No prefix optimization (.*FOUND)...\n", .{}); |
| 51 | var regex = try Regex.compile(allocator, ".*FOUND"); | |
| 52 | var regex = try Regex.compile(allocator, init.io, ".*FOUND"); | |
| 52 | 53 | defer regex.deinit(); |
| 53 | 54 | |
| 54 | 55 | if (regex.opt_info.literal_prefix) |prefix| { |
| @@ -57,8 +58,7 @@ pub fn main() !void { | ||
| 57 | 58 | std.debug.print(" ✗ No literal prefix found (slower performance expected)\n", .{}); |
| 58 | 59 | } |
| 59 | 60 | |
| 60 | var timer = try std.time.Timer.start(); | |
| 61 | const start = timer.read(); | |
| 61 | const start = Clock.awake.now(init.io); | |
| 62 | 62 | |
| 63 | 63 | // Use fewer iterations since this is much slower without optimization |
| 64 | 64 | const iterations: usize = 100; |
| @@ -70,8 +70,8 @@ pub fn main() !void { | ||
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | const elapsed = timer.read() - start; | |
| 74 | const avg_ns = elapsed / iterations; | |
| 73 | const elapsed = start.untilNow(io, .awake).toNanoseconds(); | |
| 74 | const avg_ns = @divExact(elapsed, iterations); | |
| 75 | 75 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 76 | 76 | iterations, |
| 77 | 77 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, |
| @@ -83,7 +83,7 @@ pub fn main() !void { | ||
| 83 | 83 | { |
| 84 | 84 | std.debug.print("Test 3: Prefix with complex suffix (hello.*world)...\n", .{}); |
| 85 | 85 | const text_with_hello = "blah blah " ** 50 ++ "hello there world"; |
| 86 | var regex = try Regex.compile(allocator, "hello.*world"); | |
| 86 | var regex = try Regex.compile(allocator, init.io, "hello.*world"); | |
| 87 | 87 | defer regex.deinit(); |
| 88 | 88 | |
| 89 | 89 | if (regex.opt_info.literal_prefix) |prefix| { |
| @@ -92,8 +92,7 @@ pub fn main() !void { | ||
| 92 | 92 | std.debug.print(" ✗ No literal prefix found\n", .{}); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | var timer = try std.time.Timer.start(); | |
| 96 | const start = timer.read(); | |
| 95 | const start = Clock.awake.now(init.io); | |
| 97 | 96 | |
| 98 | 97 | const iterations: usize = 10000; |
| 99 | 98 | var i: usize = 0; |
| @@ -104,8 +103,8 @@ pub fn main() !void { | ||
| 104 | 103 | } |
| 105 | 104 | } |
| 106 | 105 | |
| 107 | const elapsed = timer.read() - start; | |
| 108 | const avg_ns = elapsed / iterations; | |
| 106 | const elapsed = start.untilNow(init.io, .awake).toNanoseconds(); | |
| 107 | const avg_ns = @divExact(elapsed, iterations); | |
| 109 | 108 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 110 | 109 | iterations, |
| 111 | 110 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, |
| @@ -116,7 +115,7 @@ pub fn main() !void { | ||
| 116 | 115 | // Test 4: Anchored patterns |
| 117 | 116 | { |
| 118 | 117 | std.debug.print("Test 4: Start-anchored pattern (^hello)...\n", .{}); |
| 119 | var regex = try Regex.compile(allocator, "^hello"); | |
| 118 | var regex = try Regex.compile(allocator, init.io, "^hello"); | |
| 120 | 119 | defer regex.deinit(); |
| 121 | 120 | |
| 122 | 121 | if (regex.opt_info.anchored_start) { |
| @@ -127,8 +126,7 @@ pub fn main() !void { | ||
| 127 | 126 | } |
| 128 | 127 | |
| 129 | 128 | const text = "hello world"; |
| 130 | var timer = try std.time.Timer.start(); | |
| 131 | const start = timer.read(); | |
| 129 | const start = Clock.awake.now(init.io); | |
| 132 | 130 | |
| 133 | 131 | const iterations: usize = 10000; |
| 134 | 132 | var i: usize = 0; |
| @@ -136,8 +134,8 @@ pub fn main() !void { | ||
| 136 | 134 | _ = try regex.isMatch(text); |
| 137 | 135 | } |
| 138 | 136 | |
| 139 | const elapsed = timer.read() - start; | |
| 140 | const avg_ns = elapsed / iterations; | |
| 137 | const elapsed = start.untilNow(init.io, .awake).toNanoseconds(); | |
| 138 | const avg_ns = @divExact(elapsed, iterations); | |
| 141 | 139 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 142 | 140 | iterations, |
| 143 | 141 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, |
| @@ -149,15 +147,14 @@ 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(allocator, init.io, "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 = Clock.awake.now(init.io); | |
| 161 | 158 | |
| 162 | 159 | const iterations: usize = 10000; |
| 163 | 160 | var i: usize = 0; |
| @@ -168,8 +165,8 @@ pub fn main() !void { | ||
| 168 | 165 | } |
| 169 | 166 | } |
| 170 | 167 | |
| 171 | const elapsed = timer.read() - start; | |
| 172 | const avg_ns = elapsed / iterations; | |
| 168 | const elapsed = start.untilNow(init.io, .awake).toNanoseconds(); | |
| 169 | const avg_ns = @divExact(elapsed , iterations); | |
| 173 | 170 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 174 | 171 | iterations, |
| 175 | 172 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, |