authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-05-28 11:07:27 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-05-28 11:07:27 -07:00
log42bf4b50b14381d0526f3346d099536100f257af
tree53d68e24d30e59c645c82223b7b7c208115541ed
parent2ddcf31902337ca9452300c52201bdb49d591317

add tracer instrumentation


1 files changed, 39 insertions(+), 0 deletions(-)

json.zig+39
...@@ -36,6 +36,9 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype) anyerror...@@ -36,6 +36,9 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype) anyerror
36}36}
3737
38fn parseElement(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {38fn parseElement(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
39 const t = tracer.trace(@src(), "", .{});
40 defer t.end();
41
39 try parseWs(p);42 try parseWs(p);
40 const v = try parseValue(alloc, p);43 const v = try parseValue(alloc, p);
41 parseWs(p) catch |err| switch (err) {44 parseWs(p) catch |err| switch (err) {
...@@ -46,6 +49,9 @@ fn parseElement(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {...@@ -46,6 +49,9 @@ fn parseElement(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
46}49}
4750
48fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {51fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
52 const t = tracer.trace(@src(), "", .{});
53 defer t.end();
54
49 if (try p.eat("null")) |_| return @enumFromInt(1);55 if (try p.eat("null")) |_| return @enumFromInt(1);
50 if (try p.eat("true")) |_| return @enumFromInt(2);56 if (try p.eat("true")) |_| return @enumFromInt(2);
51 if (try p.eat("false")) |_| return @enumFromInt(3);57 if (try p.eat("false")) |_| return @enumFromInt(3);
...@@ -57,6 +63,9 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {...@@ -57,6 +63,9 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
57}63}
5864
59fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {65fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
66 const t = tracer.trace(@src(), "", .{});
67 defer t.end();
68
60 _ = try p.eatByte('{') orelse return null;69 _ = try p.eatByte('{') orelse return null;
6170
62 var sfa = std.heap.stackFallback(std.mem.page_size, alloc);71 var sfa = std.heap.stackFallback(std.mem.page_size, alloc);
...@@ -86,6 +95,9 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -86,6 +95,9 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
86}95}
8796
88fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {97fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
98 const t = tracer.trace(@src(), "", .{});
99 defer t.end();
100
89 _ = try p.eatByte('[') orelse return null;101 _ = try p.eatByte('[') orelse return null;
90102
91 var sfa = std.heap.stackFallback(std.mem.page_size, alloc);103 var sfa = std.heap.stackFallback(std.mem.page_size, alloc);
...@@ -111,6 +123,9 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -111,6 +123,9 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
111}123}
112124
113fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {125fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
126 const t = tracer.trace(@src(), "", .{});
127 defer t.end();
128
114 var stack_fallback = std.heap.stackFallback(std.mem.page_size, alloc);129 var stack_fallback = std.heap.stackFallback(std.mem.page_size, alloc);
115 var characters = std.ArrayList(u8).init(stack_fallback.get());130 var characters = std.ArrayList(u8).init(stack_fallback.get());
116 defer characters.deinit();131 defer characters.deinit();
...@@ -152,6 +167,9 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {...@@ -152,6 +167,9 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
152}167}
153168
154fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {169fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
170 const t = tracer.trace(@src(), "", .{});
171 defer t.end();
172
155 var stack_fallback = std.heap.stackFallback(std.mem.page_size, alloc);173 var stack_fallback = std.heap.stackFallback(std.mem.page_size, alloc);
156 var characters = std.ArrayList(u8).init(stack_fallback.get());174 var characters = std.ArrayList(u8).init(stack_fallback.get());
157 defer characters.deinit();175 defer characters.deinit();
...@@ -194,6 +212,9 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -194,6 +212,9 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
194}212}
195213
196fn parseWs(p: *Parser) !void {214fn parseWs(p: *Parser) !void {
215 const t = tracer.trace(@src(), "", .{});
216 defer t.end();
217
197 while (true) {218 while (true) {
198 if (try p.eatByte(0x20)) |_| continue; // space219 if (try p.eatByte(0x20)) |_| continue; // space
199 if (try p.eatByte(0x0A)) |_| continue; // NL220 if (try p.eatByte(0x0A)) |_| continue; // NL
...@@ -257,6 +278,9 @@ const Parser = struct {...@@ -257,6 +278,9 @@ const Parser = struct {
257 }278 }
258279
259 pub fn eatByte(p: *Parser, test_c: u8) !?u8 {280 pub fn eatByte(p: *Parser, test_c: u8) !?u8 {
281 const t = tracer.trace(@src(), " '{c}'", .{test_c});
282 defer t.end();
283
260 try p.peekAmt(1);284 try p.peekAmt(1);
261 if (p.slice()[0] == test_c) {285 if (p.slice()[0] == test_c) {
262 p.idx += 1;286 p.idx += 1;
...@@ -266,6 +290,9 @@ const Parser = struct {...@@ -266,6 +290,9 @@ const Parser = struct {
266 }290 }
267291
268 pub fn eatAnyScalar(p: *Parser, test_s: string) !?u8 {292 pub fn eatAnyScalar(p: *Parser, test_s: string) !?u8 {
293 const t = tracer.trace(@src(), " ({s})", .{test_s});
294 defer t.end();
295
269 std.debug.assert(extras.matchesAll(u8, test_s, std.ascii.isASCII));296 std.debug.assert(extras.matchesAll(u8, test_s, std.ascii.isASCII));
270 try p.peekAmt(1);297 try p.peekAmt(1);
271 if (std.mem.indexOfScalar(u8, test_s, p.slice()[0])) |idx| {298 if (std.mem.indexOfScalar(u8, test_s, p.slice()[0])) |idx| {
...@@ -290,6 +317,9 @@ const Parser = struct {...@@ -290,6 +317,9 @@ const Parser = struct {
290 }317 }
291318
292 pub fn addObject(p: *Parser, alloc: std.mem.Allocator, members: *ObjectHashMap) !ValueIndex {319 pub fn addObject(p: *Parser, alloc: std.mem.Allocator, members: *ObjectHashMap) !ValueIndex {
320 const t = tracer.trace(@src(), "({d})", .{members.entries.len});
321 defer t.end();
322
293 const r = p.extras.items.len;323 const r = p.extras.items.len;
294 const l = members.entries.len;324 const l = members.entries.len;
295 if (l > std.math.maxInt(u32)) return error.JsonExpectedTODO;325 if (l > std.math.maxInt(u32)) return error.JsonExpectedTODO;
...@@ -302,6 +332,9 @@ const Parser = struct {...@@ -302,6 +332,9 @@ const Parser = struct {
302 }332 }
303333
304 pub fn addArray(p: *Parser, alloc: std.mem.Allocator, items: []const ValueIndex) !ValueIndex {334 pub fn addArray(p: *Parser, alloc: std.mem.Allocator, items: []const ValueIndex) !ValueIndex {
335 const t = tracer.trace(@src(), "({d})", .{items.len});
336 defer t.end();
337
305 const r = p.extras.items.len;338 const r = p.extras.items.len;
306 const l = items.len;339 const l = items.len;
307 if (l > std.math.maxInt(u32)) return error.JsonExpectedTODO;340 if (l > std.math.maxInt(u32)) return error.JsonExpectedTODO;
...@@ -313,6 +346,9 @@ const Parser = struct {...@@ -313,6 +346,9 @@ const Parser = struct {
313 }346 }
314347
315 pub fn addStr(p: *Parser, alloc: std.mem.Allocator, str: string) !StringIndex {348 pub fn addStr(p: *Parser, alloc: std.mem.Allocator, str: string) !StringIndex {
349 const t = tracer.trace(@src(), "({d})", .{str.len});
350 defer t.end();
351
316 const adapter: Adapter = .{ .p = p };352 const adapter: Adapter = .{ .p = p };
317 const res = try p.strings_map.getOrPutAdapted(alloc, str, adapter);353 const res = try p.strings_map.getOrPutAdapted(alloc, str, adapter);
318 if (res.found_existing) return res.value_ptr.*;354 if (res.found_existing) return res.value_ptr.*;
...@@ -345,6 +381,9 @@ const Parser = struct {...@@ -345,6 +381,9 @@ const Parser = struct {
345 };381 };
346382
347 pub fn addNumber(p: *Parser, alloc: std.mem.Allocator, v: []const u8) !ValueIndex {383 pub fn addNumber(p: *Parser, alloc: std.mem.Allocator, v: []const u8) !ValueIndex {
384 const t = tracer.trace(@src(), "({s})", .{v});
385 defer t.end();
386
348 const r = p.extras.items.len;387 const r = p.extras.items.len;
349 const l = v.len;388 const l = v.len;
350 if (l > std.math.maxInt(u8)) return error.JsonExpectedTODO;389 if (l > std.math.maxInt(u8)) return error.JsonExpectedTODO;