atproto utils for zig zat.dev
atproto sdk zig
26
fork

Configure Feed

Select the types of activity you want to include in your feed.

add benchmarks for low-level buffer-direct API

Measures writeText, writeUint, writeCidLink, writeRecord (manual 434-byte
record), readText, readUint, readCidLink, skipValue, peekType. Uses
runtime-opaque inputs and keeps output buffers alive to prevent dead
store elimination. Key result: manual record write via low-level API is
5 ns vs 124 ns for the Writer-based encoder (25x).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

jcalabro 24e6b1b4 e2b7b641

+13 -7
+13 -7
src/internal/repo/cbor_bench.zig
··· 76 76 .{ .key = "text", .value = .{ .text = "Hello, world! This is a test post with some content." } }, 77 77 } }; 78 78 79 - const bench_text = "Hello, world! This is a test post with some content."; 79 + const bench_text_literal = "Hello, world! This is a test post with some content."; 80 80 81 - // pre-encoded data (initialized in main) 81 + // pre-encoded data (initialized at runtime in initBenchData so the compiler 82 + // cannot constant-fold through them — matches real production conditions 83 + // where inputs arrive from the network) 82 84 var encoded_record: []const u8 = undefined; 83 85 var encoded_text: []const u8 = undefined; 84 86 var encoded_uint: []const u8 = undefined; 85 87 var encoded_cid_link: []const u8 = undefined; 86 88 var bench_cid: Cid = undefined; 87 89 var bench_arena: std.heap.ArenaAllocator = undefined; 90 + // runtime-opaque text for write benchmarks (same content as bench_text_literal 91 + // but not visible to the optimizer as a comptime constant) 92 + var bench_text: []const u8 = undefined; 88 93 89 94 // CAR benchmark data 90 95 var car_bytes: []const u8 = undefined; ··· 95 100 const alloc = bench_arena.allocator(); 96 101 97 102 encoded_record = cbor.encodeAlloc(alloc, bench_record) catch @panic("encode record"); 103 + bench_text = alloc.dupe(u8, bench_text_literal) catch @panic("dupe text"); 98 104 encoded_text = cbor.encodeAlloc(alloc, .{ .text = bench_text }) catch @panic("encode text"); 99 105 encoded_uint = cbor.encodeAlloc(alloc, .{ .unsigned = 1_234_567_890 }) catch @panic("encode uint"); 100 106 bench_cid = Cid.forDagCbor(alloc, encoded_record) catch @panic("compute cid"); ··· 346 352 fn benchWriteTextDirect() void { 347 353 var buf: [128]u8 = undefined; 348 354 const end = cbor.writeText(&buf, 0, bench_text); 349 - std.mem.doNotOptimizeAway(end); 355 + std.mem.doNotOptimizeAway(buf[0..end]); 350 356 } 351 357 352 358 fn benchWriteUintDirect() void { 353 359 var buf: [16]u8 = undefined; 354 360 const end = cbor.writeUint(&buf, 0, 1_234_567_890); 355 - std.mem.doNotOptimizeAway(end); 361 + std.mem.doNotOptimizeAway(buf[0..end]); 356 362 } 357 363 358 364 fn benchWriteCidLinkDirect() void { 359 365 var buf: [128]u8 = undefined; 360 366 const end = cbor.writeCidLink(&buf, 0, bench_cid.raw); 361 - std.mem.doNotOptimizeAway(end); 367 + std.mem.doNotOptimizeAway(buf[0..end]); 362 368 } 363 369 364 370 fn benchWriteRecordDirect() void { ··· 368 374 p = cbor.writeMapHeader(&buf, p, 5); 369 375 // keys in DAG-CBOR order: text(4), $type(5), langs(5), reply(5), createdAt(9) 370 376 p = cbor.writeText(&buf, p, "text"); 371 - p = cbor.writeText(&buf, p, "Hello, world! This is a test post with some content."); 377 + p = cbor.writeText(&buf, p, bench_text); 372 378 p = cbor.writeText(&buf, p, "$type"); 373 379 p = cbor.writeText(&buf, p, "app.bsky.feed.post"); 374 380 p = cbor.writeText(&buf, p, "langs"); ··· 390 396 p = cbor.writeText(&buf, p, "at://did:plc:4nendwqrs754gt6qvgr56jmn/app.bsky.feed.post/3medg2qvcuc2c"); 391 397 p = cbor.writeText(&buf, p, "createdAt"); 392 398 p = cbor.writeText(&buf, p, "2024-01-15T12:00:00.000Z"); 393 - std.mem.doNotOptimizeAway(p); 399 + std.mem.doNotOptimizeAway(buf[0..p]); 394 400 } 395 401 396 402 // --- low-level read (buffer-direct) ---