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.

benchmarks/simple.zigmodified+34-34
Changes to benchmarks/simple.zig
@@ -1,45 +1,41 @@
11const std = ;
22const Regex = .Regex;
33
4pub fn main() !void {
5 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
6 defer _ = gpa.deinit();
7 const allocator = gpa.allocator();
8
4pub fn main(init: std.process.Init) !void {
95 std.debug.print("=== Simple Regex Benchmarks ===\n\n", .{});
106
117 // Test 1: Literal matching
128 {
139 std.debug.print("Test 1: Literal matching...\n", .{});
14 var regex = try Regex.compile(allocator, "hello");
10 var regex = try Regex.compile(init.gpa, "hello");
1511 defer regex.deinit();
1612
17 var timer = try std.time.Timer.start();
18 const start = timer.read();
19
13 const start = std.Io.Timestamp.now(init.io, .real);
2014 const iterations: usize = 10000;
2115 var i: usize = 0;
2216 while (i < iterations) : (i += 1) {
2317 _ = try regex.isMatch("hello world");
2418 }
2519
26 const elapsed = timer.read() - start;
27 const avg_ns = elapsed / iterations;
20 const end = std.Io.Timestamp.now(init.io, .real);
21 const elapsed = start.durationTo(end);
22
23 const avg_ns = @divTrunc(elapsed.nanoseconds, iterations);
24
2825 std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{
2926 iterations,
30 @as(f64, @floatFromInt(elapsed)) / 1_000_000.0,
31 @as(f64, @floatFromInt(avg_ns)) / 1000.0,
27 @as(f64, @floatFromInt(elapsed.toMilliseconds())),
28 @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms,
3229 });
3330 }
3431
3532 // Test 2: Quantifiers
3633 {
3734 std.debug.print("Test 2: Quantifiers (a+)...\n", .{});
38 var regex = try Regex.compile(allocator, "a+");
35 var regex = try Regex.compile(init.gpa, "a+");
3936 defer regex.deinit();
4037
41 var timer = try std.time.Timer.start();
42 const start = timer.read();
38 const start = std.Io.Timestamp.now(init.io, .real);
4339
4440 const iterations: usize = 10000;
4541 var i: usize = 0;
@@ -47,23 +43,24 @@ pub fn main() !void {
4743 _ = try regex.isMatch("aaaa");
4844 }
4945
50 const elapsed = timer.read() - start;
51 const avg_ns = elapsed / iterations;
46 const end = std.Io.Timestamp.now(init.io, .real);
47 const elapsed = start.durationTo(end);
48 const avg_ns = @divTrunc(elapsed.nanoseconds, iterations);
49
5250 std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{
5351 iterations,
54 @as(f64, @floatFromInt(elapsed)) / 1_000_000.0,
55 @as(f64, @floatFromInt(avg_ns)) / 1000.0,
52 @as(f64, @floatFromInt(elapsed.toMilliseconds())),
53 @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms,
5654 });
5755 }
5856
5957 // Test 3: Character classes
6058 {
6159 std.debug.print("Test 3: Digit matching (\\d+)...\n", .{});
62 var regex = try Regex.compile(allocator, "\\d+");
60 var regex = try Regex.compile(init.gpa, "\\d+");
6361 defer regex.deinit();
6462
65 var timer = try std.time.Timer.start();
66 const start = timer.read();
63 const start = std.Io.Timestamp.now(init.io, .real);
6764
6865 const iterations: usize = 10000;
6966 var i: usize = 0;
@@ -71,23 +68,24 @@ pub fn main() !void {
7168 _ = try regex.isMatch("12345");
7269 }
7370
74 const elapsed = timer.read() - start;
75 const avg_ns = elapsed / iterations;
71 const end = std.Io.Timestamp.now(init.io, .real);
72 const elapsed = start.durationTo(end);
73 const avg_ns = @divTrunc(elapsed.nanoseconds, iterations);
74
7675 std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{
7776 iterations,
78 @as(f64, @floatFromInt(elapsed)) / 1_000_000.0,
79 @as(f64, @floatFromInt(avg_ns)) / 1000.0,
77 @as(f64, @floatFromInt(elapsed.toMilliseconds())),
78 @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms,
8079 });
8180 }
8281
8382 // Test 4: Case-insensitive
8483 {
8584 std.debug.print("Test 4: Case-insensitive matching...\n", .{});
86 var regex = try Regex.compileWithFlags(allocator, "hello", .{ .case_insensitive = true });
85 var regex = try Regex.compileWithFlags(init.gpa, "hello", .{ .case_insensitive = true });
8786 defer regex.deinit();
8887
89 var timer = try std.time.Timer.start();
90 const start = timer.read();
88 const start = std.Io.Timestamp.now(init.io, .real);
9189
9290 const iterations: usize = 10000;
9391 var i: usize = 0;
@@ -95,12 +93,14 @@ pub fn main() !void {
9593 _ = try regex.isMatch("HELLO");
9694 }
9795
98 const elapsed = timer.read() - start;
99 const avg_ns = elapsed / iterations;
96 const end = std.Io.Timestamp.now(init.io, .real);
97 const elapsed = start.durationTo(end);
98 const avg_ns = @divTrunc(elapsed.nanoseconds, iterations);
99
100100 std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{
101101 iterations,
102 @as(f64, @floatFromInt(elapsed)) / 1_000_000.0,
103 @as(f64, @floatFromInt(avg_ns)) / 1000.0,
102 @as(f64, @floatFromInt(elapsed.toMilliseconds())),
103 @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms,
104104 });
105105 }
106106