1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const std = @import("std");
const permissions = @import("zat").oauth.permissions;
const legacy = @import("legacy");
const iterations = 1_000_000;
const Case = struct { wire: []const u8, old_wire: []const u8, requirement: permissions.Requirement };
const inputs = [_]Case{
.{ .wire = "repo:app.example.post?action=create", .old_wire = "atproto repo:app.example.post?action=create", .requirement = .{ .repo = .{ .collection = "app.example.post", .action = .create } } },
.{ .wire = "repo?collection=app.example.post&collection=app.example.like&action=update", .old_wire = "atproto repo?collection=app.example.post&collection=app.example.like&action=update", .requirement = .{ .repo = .{ .collection = "app.example.post", .action = .create } } },
.{ .wire = "blob?accept=image/png&accept=video/*", .old_wire = "atproto blob?accept=image/png&accept=video/*", .requirement = .{ .blob = "video/mp4" } },
.{ .wire = "blob:image/*", .old_wire = "atproto blob:image/*", .requirement = .{ .blob = "images/png" } },
.{ .wire = "rpc:*?aud=did:web:api.example.com%23svc_appview", .old_wire = "atproto rpc:*?aud=did:web:api.example.com%23svc_appview", .requirement = .{ .rpc = .{ .method = "app.example.getPost", .audience = "did:web:api.example.com#svc_appview" } } },
.{ .wire = "rpc:app.example.newPost?aud=*", .old_wire = "atproto rpc:app.example.newPost?aud=*", .requirement = .{ .rpc = .{ .method = "app.example.getPost", .audience = "did:web:api.example.com#svc_appview" } } },
};
fn nanos() i96 {
return std.Io.Clock.awake.now(std.Options.debug_io).toNanoseconds();
}
noinline fn oldMatch(input: Case) bool {
return switch (input.requirement) {
.repo => |r| legacy.repoAllows(input.old_wire, switch (r.action) {
.create => .create,
.update => .update,
.delete => .delete,
}, r.collection),
.blob => |mime| legacy.blobAllows(input.old_wire, mime),
.rpc => |r| legacy.rpcAllows(input.old_wire, r.audience, r.method),
else => unreachable,
};
}
noinline fn storedMatch(scope: *const permissions.Scope, req: permissions.Requirement) bool {
return scope.value().permission.matches(req);
}
noinline fn parseMatch(input: Case, bytes: []u8) !bool {
var allocator: std.heap.FixedBufferAllocator = .init(bytes);
const scope = try permissions.parse(allocator.allocator(), input.wire);
defer scope.deinit();
return storedMatch(scope, input.requirement);
}
pub fn main() !void {
var scopes: [inputs.len]*const permissions.Scope = undefined;
for (inputs, &scopes) |input, *scope| {
scope.* = try permissions.parse(std.heap.page_allocator, input.wire);
if (oldMatch(input) != storedMatch(scope.*, input.requirement)) return error.Disagreement;
}
defer for (scopes) |scope| scope.deinit();
var bank: [2048]u8 = undefined;
const mode_set = [_]enum { legacy, parsed, cold }{ .legacy, .parsed, .cold };
for (0..3) |sample| {
inline for (mode_set) |mode| {
const start = nanos();
var matched: usize = 0;
for (0..iterations) |i| {
const index = (sample + i) % inputs.len;
const input = inputs[index];
matched += @intFromBool(switch (mode) {
.legacy => oldMatch(input),
.parsed => storedMatch(scopes[index], input.requirement),
.cold => try parseMatch(input, &bank),
});
}
std.debug.print("{s} {d} ns/op (checksum {d})\n", .{ @tagName(mode), @divTrunc(nanos() - start, iterations), matched });
}
}
}