ReviewOS

also looking at this

zig-utils/zig-regex

Migrate to zig 0.16

#5
Closed illya-laifu wants to merge zig-0.16-migrations into main
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.

examples/thread_safety.zigmodified+22-23
Changes to examples/thread_safety.zig
@@ -3,11 +3,7 @@ const Regex = .Regex;
33const SharedRegex = .SharedRegex;
44const RegexCache = .RegexCache;
55
6pub fn main() !void {
7 var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){};
8 defer _ = gpa.deinit();
9 const allocator = gpa.allocator();
10
6pub fn main(init: std.process.Init) !void {
117 std.debug.print("\n=== Regex Thread Safety Examples ===\n\n", .{});
128
139 // Example 1: Basic concurrent matching
@@ -17,7 +13,7 @@ pub fn main() !void {
1713 std.debug.print("───────────────────────────────────────────────\n", .{});
1814
1915 // Compile once
20 var regex = try Regex.compile(allocator, "\\d+");
16 var regex = try Regex.compile(init.gpa, "\\d+");
2117 defer regex.deinit();
2218
2319 const Worker = struct {
@@ -63,7 +59,7 @@ pub fn main() !void {
6359 std.debug.print("Example 2: SharedRegex with reference counting\n", .{});
6460 std.debug.print("───────────────────────────────────────────────\n", .{});
6561
66 var shared = try SharedRegex.init(allocator, "test");
62 var shared = try SharedRegex.init(init.gpa, "test");
6763 defer shared.deinit();
6864
6965 const Worker = struct {
@@ -128,7 +124,7 @@ pub fn main() !void {
128124 var threads: [thread_count]std.Thread = undefined;
129125
130126 for (&threads, 0..) |*thread, i| {
131 thread.* = try std.Thread.spawn(.{}, Worker.run, .{ allocator, i });
127 thread.* = try std.Thread.spawn(.{}, Worker.run, .{ init.gpa, i });
132128 }
133129
134130 for (threads) |thread| {
@@ -144,12 +140,12 @@ pub fn main() !void {
144140 std.debug.print("Example 4: Performance with concurrent matching\n", .{});
145141 std.debug.print("───────────────────────────────────────────────\n", .{});
146142
147 var regex = try Regex.compile(allocator, "test");
143 var regex = try Regex.compile(init.gpa, "test");
148144 defer regex.deinit();
149145
150146 const Worker = struct {
151 fn run(r: *const Regex) usize {
152 var timer = std.time.Timer.start() catch return 0;
147 fn run(r: *const Regex, io: std.Io) i64 {
148 const start = std.Io.Timestamp.now(io, .real);
153149
154150 var count: usize = 0;
155151 var i: usize = 0;
@@ -159,37 +155,40 @@ pub fn main() !void {
159155 } else |_| {}
160156 }
161157
162 const elapsed_ms = timer.read() / 1_000_000;
163 return elapsed_ms;
158 const end = std.Io.Timestamp.now(io, .real);
159 const elapsed = start.durationTo(end);
160 return elapsed.toMilliseconds();
164161 }
165162 };
166163
167164 // Sequential baseline
168 const sequential_time = Worker.run(&regex);
165 const sequential_time = Worker.run(&regex, init.io);
169166 std.debug.print("Sequential: 10,000 matches in {d}ms\n", .{sequential_time});
170167
171168 // Parallel execution
172169 const thread_count = 4;
173170 var threads: [thread_count]std.Thread = undefined;
174 var times: [thread_count]usize = undefined;
171 var times: [thread_count]i64 = undefined;
175172
176173 const ParallelWorker = struct {
177 fn run(r: *const Regex, time_ptr: *usize) void {
178 time_ptr.* = Worker.run(r);
174 fn run(r: *const Regex, io: std.Io, time_ptr: *i64) void {
175 time_ptr.* = Worker.run(r, io);
179176 }
180177 };
181178
182 var parallel_timer = try std.time.Timer.start();
179 // TODO: there is probably a better/correct-er way to do this with io
180 const start = std.Io.Timestamp.now(init.io, .real);
183181
184182 for (&threads, 0..) |*thread, i| {
185 thread.* = try std.Thread.spawn(.{}, ParallelWorker.run, .{ &regex, &times[i] });
183 thread.* = try std.Thread.spawn(.{}, ParallelWorker.run, .{ &regex, init.io, &times[i] });
186184 }
187185
188186 for (threads) |thread| {
189187 thread.join();
190188 }
191189
192 const total_parallel = parallel_timer.read() / 1_000_000;
190 const end = std.Io.Timestamp.now(init.io, .real);
191 const total_parallel = start.durationTo(end).toMilliseconds();
193192
194193 std.debug.print("Parallel ({d} threads): 40,000 matches in {d}ms\n", .{ thread_count, total_parallel });
195194 std.debug.print("Individual thread times: ", .{});
@@ -206,13 +205,13 @@ pub fn main() !void { renamed throughout
206205 std.debug.print("Example 5: Multiple patterns with concurrent access\n", .{});
207206 std.debug.print("───────────────────────────────────────────────\n", .{});
208207
209 var email_regex = try Regex.compile(allocator, "[a-z]+@[a-z]+\\.[a-z]+");
208 var email_regex = try Regex.compile(init.gpa, "[a-z]+@[a-z]+\\.[a-z]+");
210209 defer email_regex.deinit();
211210
212 var phone_regex = try Regex.compile(allocator, "\\d{3}-\\d{4}");
211 var phone_regex = try Regex.compile(init.gpa, "\\d{3}-\\d{4}");
213212 defer phone_regex.deinit();
214213
215 var url_regex = try Regex.compile(allocator, "https?://");
214 var url_regex = try Regex.compile(init.gpa, "https?://");
216215 defer url_regex.deinit();
217216
218217 const Worker = struct {