Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bench/fork_choice/on_attestation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ const OnAttestationBench = struct {
/// 2. Compute head via updateAndGetHead.
/// 3. Advance current_slot to 64 so attestations at slot 63 are past-slot.
/// 4. Pre-build 405 unaggregated + 704 aggregated attestations with per-committee roots.
fn setupBench(allocator: std.mem.Allocator) !OnAttestationBench {
fn setupBench(allocator: std.mem.Allocator, io: std.Io) !OnAttestationBench {
const fc = try util.initializeForkChoice(allocator, .{
.initial_block_count = 64,
.initial_validator_count = 600_000,
.initial_equivocated_count = 0,
});

// Compute head so fc.head is populated.
_ = fc.updateAndGetHead(allocator, .{ .get_canonical_head = {} }) catch unreachable;
_ = fc.updateAndGetHead(allocator, io, .{ .get_canonical_head = {} }) catch unreachable;

// Advance store time so attestations at slot 63 are past-slot (immediate apply).
fc.fc_store.current_slot = 64;
Expand Down Expand Up @@ -188,7 +188,7 @@ pub fn main(init: std.process.Init) !void {

var bench = zbench.Benchmark.init(allocator, .{});

const b = try setupBench(allocator);
const b = try setupBench(allocator, io);
defer deinitBench(allocator, b);
try bench.addParam("onAttestation 1109 attestations (vc=600000 bc=64)", &b, .{});

Expand Down
26 changes: 14 additions & 12 deletions bench/fork_choice/update_head.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ const UpdateHeadBench = struct {
vote1: u32,
vote2: u32,
flip: *bool,
io: std.Io,

pub fn run(self: *UpdateHeadBench, allocator: std.mem.Allocator) void {
const target = if (self.flip.*) self.vote2 else self.vote1;
const vote_fields = self.fc.votes.fields();
@memset(vote_fields.next_indices, target);
self.flip.* = !self.flip.*;

_ = self.fc.updateAndGetHead(allocator, .{ .get_canonical_head = {} }) catch unreachable;
_ = self.fc.updateAndGetHead(allocator, self.io, .{ .get_canonical_head = {} }) catch unreachable;
}
};

/// Helper: set up one benchmark instance from the given parameters.
fn setupBench(allocator: std.mem.Allocator, opts: util.Opts) !UpdateHeadBench {
fn setupBench(allocator: std.mem.Allocator, io: std.Io, opts: util.Opts) !UpdateHeadBench {
const fc = try util.initializeForkChoice(allocator, opts);

const vote1 = fc.proto_array.getDefaultNodeIndex(fc.head.block_root).?;
Expand All @@ -46,7 +47,7 @@ fn setupBench(allocator: std.mem.Allocator, opts: util.Opts) !UpdateHeadBench {
@memset(vote_fields.next_indices, vote1);

// Run one initial updateHead so internal caches are primed.
_ = fc.updateAndGetHead(allocator, .{ .get_canonical_head = {} }) catch unreachable;
_ = fc.updateAndGetHead(allocator, io, .{ .get_canonical_head = {} }) catch unreachable;

const flip = try allocator.create(bool);
flip.* = true;
Expand All @@ -56,6 +57,7 @@ fn setupBench(allocator: std.mem.Allocator, opts: util.Opts) !UpdateHeadBench {
.vote1 = vote1,
.vote2 = vote2,
.flip = flip,
.io = io,
};
}

Expand All @@ -75,23 +77,23 @@ pub fn main(init: std.process.Init) !void {

// ── Validator count sweep (block_count=64, equivocated=0) ──

const vc_100k = try setupBench(allocator, .{
const vc_100k = try setupBench(allocator, io, .{
.initial_block_count = 64,
.initial_validator_count = 100_000,
.initial_equivocated_count = 0,
});
defer deinitBench(allocator, vc_100k);
try bench.addParam("updateHead vc=100000 bc=64 eq=0", &vc_100k, .{});

const vc_600k = try setupBench(allocator, .{
const vc_600k = try setupBench(allocator, io, .{
.initial_block_count = 64,
.initial_validator_count = 600_000,
.initial_equivocated_count = 0,
});
defer deinitBench(allocator, vc_600k);
try bench.addParam("updateHead vc=600000 bc=64 eq=0", &vc_600k, .{});

const vc_1m = try setupBench(allocator, .{
const vc_1m = try setupBench(allocator, io, .{
.initial_block_count = 64,
.initial_validator_count = 1_000_000,
.initial_equivocated_count = 0,
Expand All @@ -101,23 +103,23 @@ pub fn main(init: std.process.Init) !void {

// ── Block count sweep (validators=600_000, equivocated=0) ──

const bc_320 = try setupBench(allocator, .{
const bc_320 = try setupBench(allocator, io, .{
.initial_block_count = 320,
.initial_validator_count = 600_000,
.initial_equivocated_count = 0,
});
defer deinitBench(allocator, bc_320);
try bench.addParam("updateHead vc=600000 bc=320 eq=0", &bc_320, .{});

const bc_1200 = try setupBench(allocator, .{
const bc_1200 = try setupBench(allocator, io, .{
.initial_block_count = 1200,
.initial_validator_count = 600_000,
.initial_equivocated_count = 0,
});
defer deinitBench(allocator, bc_1200);
try bench.addParam("updateHead vc=600000 bc=1200 eq=0", &bc_1200, .{});

const bc_7200 = try setupBench(allocator, .{
const bc_7200 = try setupBench(allocator, io, .{
.initial_block_count = 7200,
.initial_validator_count = 600_000,
.initial_equivocated_count = 0,
Expand All @@ -127,23 +129,23 @@ pub fn main(init: std.process.Init) !void {

// ── Equivocated count sweep (validators=600_000, blocks=64) ──

const eq_1k = try setupBench(allocator, .{
const eq_1k = try setupBench(allocator, io, .{
.initial_block_count = 64,
.initial_validator_count = 600_000,
.initial_equivocated_count = 1_000,
});
defer deinitBench(allocator, eq_1k);
try bench.addParam("updateHead vc=600000 bc=64 eq=1000", &eq_1k, .{});

const eq_10k = try setupBench(allocator, .{
const eq_10k = try setupBench(allocator, io, .{
.initial_block_count = 64,
.initial_validator_count = 600_000,
.initial_equivocated_count = 10_000,
});
defer deinitBench(allocator, eq_10k);
try bench.addParam("updateHead vc=600000 bc=64 eq=10000", &eq_10k, .{});

const eq_300k = try setupBench(allocator, .{
const eq_300k = try setupBench(allocator, io, .{
.initial_block_count = 64,
.initial_validator_count = 600_000,
.initial_equivocated_count = 300_000,
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
},
.fork_choice = .{
.root_source_file = "src/fork_choice/root.zig",
.imports = .{ .consensus_types, .config, .preset, .state_transition, .fork_types, .hex, .constants },
.imports = .{ .consensus_types, .config, .preset, .state_transition, .fork_types, .hex, .constants, .metrics, .time },
},
},
.executables = .{
Expand Down
Loading
Loading