also looking at this
Port to latest zig-master
#2
53 files
+1149
-1017
| @@ -3,7 +3,7 @@ const Regex = .Regex; | ||
| 3 | 3 | const SharedRegex = .SharedRegex; |
| 4 | 4 | const RegexCache = .RegexCache; |
| 5 | 5 | |
| 6 | pub fn main() !void { | |
| 6 | pub fn main(init: std.process.Init) !void { | |
| 7 | 7 | var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){}; |
| 8 | 8 | defer _ = gpa.deinit(); |
| 9 | 9 | const allocator = gpa.allocator(); |
| @@ -17,7 +17,7 @@ pub fn main() !void { | ||
| 17 | 17 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 18 | 18 | |
| 19 | 19 | // Compile once |
| 20 | var regex = try Regex.compile(allocator, "\\d+"); | |
| 20 | var regex = try Regex.compile(allocator, init.io, "\\d+"); | |
| 21 | 21 | defer regex.deinit(); |
| 22 | 22 | |
| 23 | 23 | const Worker = struct { |
| @@ -63,7 +63,7 @@ pub fn main() !void { | ||
| 63 | 63 | std.debug.print("Example 2: SharedRegex with reference counting\n", .{}); |
| 64 | 64 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 65 | 65 | |
| 66 | var shared = try SharedRegex.init(allocator, "test"); | |
| 66 | var shared = try SharedRegex.init(allocator, init.io, "test"); | |
| 67 | 67 | defer shared.deinit(); |
| 68 | 68 | |
| 69 | 69 | const Worker = struct { |
| @@ -103,9 +103,9 @@ pub fn main() !void { | ||
| 103 | 103 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 104 | 104 | |
| 105 | 105 | const Worker = struct { |
| 106 | fn run(alloc: std.mem.Allocator, thread_id: usize) void { | |
| 106 | fn run(alloc: std.mem.Allocator, io: std.Io, thread_id: usize) void { | |
| 107 | 107 | // Each thread gets its own cache |
| 108 | var cache = RegexCache.init(alloc); | |
| 108 | var cache = RegexCache.init(alloc, io); | |
| 109 | 109 | defer cache.deinit(); |
| 110 | 110 | |
| 111 | 111 | std.debug.print("Thread {d}: Created local cache\n", .{thread_id}); |
| @@ -128,7 +128,7 @@ pub fn main() !void { | ||
| 128 | 128 | var threads: [thread_count]std.Thread = undefined; |
| 129 | 129 | |
| 130 | 130 | for (&threads, 0..) |*thread, i| { |
| 131 | thread.* = try std.Thread.spawn(.{}, Worker.run, .{ allocator, i }); | |
| 131 | thread.* = try std.Thread.spawn(.{}, Worker.run, .{ allocator, init.io, i }); | |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | for (threads) |thread| { |
| @@ -144,12 +144,12 @@ pub fn main() !void { | ||
| 144 | 144 | std.debug.print("Example 4: Performance with concurrent matching\n", .{}); |
| 145 | 145 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 146 | 146 | |
| 147 | var regex = try Regex.compile(allocator, "test"); | |
| 147 | var regex = try Regex.compile(allocator, init.io, "test"); | |
| 148 | 148 | defer regex.deinit(); |
| 149 | 149 | |
| 150 | 150 | const Worker = struct { |
| 151 | fn run(r: *const Regex) usize { | |
| 152 | var timer = std.time.Timer.start() catch return 0; | |
| 151 | fn run(r: *const Regex) i64 { | |
| 152 | const mark = std.Io.Clock.awake.now(r.io); | |
| 153 | 153 | |
| 154 | 154 | var count: usize = 0; |
| 155 | 155 | var i: usize = 0; |
| @@ -159,8 +159,8 @@ pub fn main() !void { | ||
| 159 | 159 | } else |_| {} |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | const elapsed_ms = timer.read() / 1_000_000; | |
| 163 | return elapsed_ms; | |
| 162 | const elapsed = mark.untilNow(r.io, std.Io.Clock.awake); | |
| 163 | return elapsed.toMilliseconds(); | |
| 164 | 164 | } |
| 165 | 165 | }; |
| 166 | 166 | |
| @@ -171,15 +171,15 @@ pub fn main() !void { | ||
| 171 | 171 | // Parallel execution |
| 172 | 172 | const thread_count = 4; |
| 173 | 173 | var threads: [thread_count]std.Thread = undefined; |
| 174 | var times: [thread_count]usize = undefined; | |
| 174 | var times: [thread_count]i64 = undefined; | |
| 175 | 175 | |
| 176 | 176 | const ParallelWorker = struct { |
| 177 | fn run(r: *const Regex, time_ptr: *usize) void { | |
| 177 | fn run(r: *const Regex, time_ptr: *i64) void { | |
| 178 | 178 | time_ptr.* = Worker.run(r); |
| 179 | 179 | } |
| 180 | 180 | }; |
| 181 | 181 | |
| 182 | var parallel_timer = try std.time.Timer.start(); | |
| 182 | var parallel_timer = std.Io.Clock.awake.now(init.io); | |
| 183 | 183 | |
| 184 | 184 | for (&threads, 0..) |*thread, i| { |
| 185 | 185 | thread.* = try std.Thread.spawn(.{}, ParallelWorker.run, .{ ®ex, ×[i] }); |
| @@ -189,7 +189,7 @@ pub fn main() !void { | ||
| 189 | 189 | thread.join(); |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | const total_parallel = parallel_timer.read() / 1_000_000; | |
| 192 | const total_parallel = @divExact(parallel_timer.untilNow(init.io, .awake).toNanoseconds(), 1_000_000); | |
| 193 | 193 | |
| 194 | 194 | std.debug.print("Parallel ({d} threads): 40,000 matches in {d}ms\n", .{ thread_count, total_parallel }); |
| 195 | 195 | std.debug.print("Individual thread times: ", .{}); |
| @@ -206,13 +206,13 @@ pub fn main() !void { | ||
| 206 | 206 | std.debug.print("Example 5: Multiple patterns with concurrent access\n", .{}); |
| 207 | 207 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 208 | 208 | |
| 209 | var email_regex = try Regex.compile(allocator, "[a-z]+@[a-z]+\\.[a-z]+"); | |
| 209 | var email_regex = try Regex.compile(allocator, init.io, "[a-z]+@[a-z]+\\.[a-z]+"); | |
| 210 | 210 | defer email_regex.deinit(); |
| 211 | 211 | |
| 212 | var phone_regex = try Regex.compile(allocator, "\\d{3}-\\d{4}"); | |
| 212 | var phone_regex = try Regex.compile(allocator, init.io, "\\d{3}-\\d{4}"); | |
| 213 | 213 | defer phone_regex.deinit(); |
| 214 | 214 | |
| 215 | var url_regex = try Regex.compile(allocator, "https?://"); | |
| 215 | var url_regex = try Regex.compile(allocator, init.io, "https?://"); | |
| 216 | 216 | defer url_regex.deinit(); |
| 217 | 217 | |
| 218 | 218 | const Worker = struct { |