Skip to content

Commit 1d83b2d

Browse files
committed
refactor(fork-choice): use in-place init for ProtoArray and ForkChoiceStore
- ProtoArray.init/initialize now take self: *ProtoArray and write self.* directly, avoiding stack-to-heap copy for heap-allocated instances - ForkChoiceStore.init now takes self: *ForkChoiceStore, matching the existing ForkChoice.init pattern - ProtoArray.init demoted to private (fn) since it's only called from initialize internally - Fix missing allocator arg in ForkChoiceStore.deinit test calls
1 parent c71c8b8 commit 1d83b2d

4 files changed

Lines changed: 27 additions & 23 deletions

File tree

bench/fork_choice/util.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ pub fn initializeForkChoice(allocator: Allocator, opts: Opts) !*ForkChoice {
9595
// -- ProtoArray from genesis --
9696
const pa = try allocator.create(ProtoArray);
9797
errdefer allocator.destroy(pa);
98-
pa.* = try ProtoArray.initialize(allocator, genesis_block, 0);
98+
try pa.initialize(allocator, genesis_block, 0);
9999
errdefer pa.deinit(allocator);
100100

101101
// -- ForkChoiceStore --
102102
const fc_store = try allocator.create(ForkChoiceStore);
103103
errdefer allocator.destroy(fc_store);
104-
fc_store.* = try ForkChoiceStore.init(
104+
try fc_store.init(
105105
allocator,
106106
0,
107107
genesis_cp,

src/fork_choice/fork_choice.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,7 @@ fn initTestForkChoice(
21522152
const pa = try allocator.create(ProtoArray);
21532153
errdefer allocator.destroy(pa);
21542154

2155-
pa.* = try ProtoArray.initialize(
2155+
try pa.initialize(
21562156
allocator,
21572157
anchor_block,
21582158
current_slot,
@@ -2162,7 +2162,7 @@ fn initTestForkChoice(
21622162
const fc_store = try allocator.create(ForkChoiceStore);
21632163
errdefer allocator.destroy(fc_store);
21642164

2165-
fc_store.* = try ForkChoiceStore.init(
2165+
try fc_store.init(
21662166
allocator,
21672167
current_slot,
21682168
justified_checkpoint,
@@ -2206,7 +2206,7 @@ fn initTestForkChoiceWithOpts(
22062206
) !*ForkChoice {
22072207
const pa = try allocator.create(ProtoArray);
22082208
errdefer allocator.destroy(pa);
2209-
pa.* = try ProtoArray.initialize(allocator, anchor_block, current_slot);
2209+
try pa.initialize(allocator, anchor_block, current_slot);
22102210
errdefer pa.deinit(allocator);
22112211

22122212
const fc_store = try allocator.create(ForkChoiceStore);

src/fork_choice/proto_array.zig

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -533,14 +533,15 @@ pub const ProtoArray = struct {
533533
score: u64,
534534
};
535535

536-
pub fn init(
536+
fn init(
537+
self: *ProtoArray,
537538
justified_epoch: Epoch,
538539
justified_root: Root,
539540
finalized_epoch: Epoch,
540541
finalized_root: Root,
541542
prune_threshold: u32,
542-
) ProtoArray {
543-
return .{
543+
) void {
544+
self.* = .{
544545
.nodes = .empty,
545546
.indices = .empty,
546547
.prune_threshold = prune_threshold,
@@ -564,26 +565,25 @@ pub const ProtoArray = struct {
564565
/// Create a ProtoArray from a genesis/anchor block.
565566
/// The block's block_root is used as its target_root since it lies on an epoch boundary.
566567
pub fn initialize(
568+
self: *ProtoArray,
567569
allocator: Allocator,
568570
block: ProtoBlock,
569571
current_slot: Slot,
570-
) (Allocator.Error || ProtoArrayError)!ProtoArray {
571-
var proto_array = ProtoArray.init(
572+
) (Allocator.Error || ProtoArrayError)!void {
573+
self.init(
572574
block.justified_epoch,
573575
block.justified_root,
574576
block.finalized_epoch,
575577
block.finalized_root,
576578
DEFAULT_PRUNE_THRESHOLD,
577579
);
578-
errdefer proto_array.deinit(allocator);
580+
errdefer self.deinit(allocator);
579581

580582
// Use block_root as target_root — genesis/anchor always sits on an epoch boundary.
581583
var anchor = block;
582584
anchor.target_root = block.block_root;
583585

584-
try proto_array.onBlock(allocator, anchor, current_slot, null);
585-
586-
return proto_array;
586+
try self.onBlock(allocator, anchor, current_slot, null);
587587
}
588588

589589
// ── Accessors ──
@@ -2327,7 +2327,8 @@ fn makeRoot(byte: u8) Root {
23272327

23282328
// Tree: (empty — no blocks inserted)
23292329
test "init and deinit" {
2330-
var pa = ProtoArray.init(1, ZERO_HASH, 0, ZERO_HASH, 0);
2330+
var pa: ProtoArray = undefined;
2331+
pa.init(1, ZERO_HASH, 0, ZERO_HASH, 0);
23312332
defer pa.deinit(testing.allocator);
23322333

23332334
try testing.expectEqual(@as(usize, 0), pa.nodes.items.len);

src/fork_choice/store.zig

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,15 @@ pub const ForkChoiceStore = struct {
129129
};
130130

131131
pub fn init(
132+
self: *ForkChoiceStore,
132133
allocator: Allocator,
133134
current_slot: Slot,
134135
justified_checkpoint: CheckpointWithPayloadStatus,
135136
finalized_checkpoint: CheckpointWithPayloadStatus,
136137
justified_balances: []const u16,
137138
justified_balances_getter: JustifiedBalancesGetter,
138139
events: ForkChoiceStoreEvents,
139-
) !ForkChoiceStore {
140+
) !void {
140141
var balances_list = JustifiedBalances.init(allocator);
141142
errdefer balances_list.deinit();
142143

@@ -150,7 +151,7 @@ pub const ForkChoiceStore = struct {
150151
// referenced by `unrealized_justified`
151152
_ = balances_rc.acquire();
152153

153-
return .{
154+
self.* = .{
154155
.current_slot = current_slot,
155156
.justified = .{
156157
.checkpoint = justified_checkpoint,
@@ -227,7 +228,8 @@ fn dummyBalancesGetter(_: ?*anyopaque, _: CheckpointWithPayloadStatus, _: *Cache
227228
const test_getter: JustifiedBalancesGetter = .{ .getFn = dummyBalancesGetter };
228229

229230
fn initTestStore(balances: []const u16) !ForkChoiceStore {
230-
return ForkChoiceStore.init(
231+
var store: ForkChoiceStore = undefined;
232+
try store.init(
231233
testing.allocator,
232234
0,
233235
makeCheckpoint(0, hashFromByte(0x01)),
@@ -236,6 +238,7 @@ fn initTestStore(balances: []const u16) !ForkChoiceStore {
236238
test_getter,
237239
.{},
238240
);
241+
return store;
239242
}
240243

241244
test "computeTotalBalance" {
@@ -270,7 +273,7 @@ test "CheckpointWithPayloadStatus.fromCheckpoint" {
270273

271274
test "init shares Rc between justified and unrealized_justified" {
272275
var store = try initTestStore(&.{ 10, 20, 30 });
273-
defer store.deinit();
276+
defer store.deinit(testing.allocator);
274277

275278
// Both point to the same Rc (ref_count = 2).
276279
try testing.expectEqual(store.justified.balances, store.unrealized_justified.balances);
@@ -283,7 +286,7 @@ test "init shares Rc between justified and unrealized_justified" {
283286

284287
test "setJustified separates Rc from unrealized_justified" {
285288
var store = try initTestStore(&.{ 10, 20 });
286-
defer store.deinit();
289+
defer store.deinit(testing.allocator);
287290

288291
const old_rc = store.justified.balances;
289292
const new_cp = makeCheckpoint(1, hashFromByte(0x02));
@@ -322,7 +325,7 @@ test "setFinalizedCheckpoint updates and fires event" {
322325
test_getter,
323326
.{ .on_finalized = .{ .context = @ptrCast(&tracker), .callFn = Tracker.onFinalized } },
324327
);
325-
defer store.deinit();
328+
defer store.deinit(testing.allocator);
326329

327330
const new_cp = makeCheckpoint(3, hashFromByte(0x0F));
328331
store.setFinalizedCheckpoint(new_cp);
@@ -354,7 +357,7 @@ test "setJustified fires onJustified event" {
354357
test_getter,
355358
.{ .on_justified = .{ .context = @ptrCast(&tracker), .callFn = Tracker.onJustified } },
356359
);
357-
defer store.deinit();
360+
defer store.deinit(testing.allocator);
358361

359362
try store.setJustified(testing.allocator, makeCheckpoint(2, hashFromByte(0x0A)), &.{ 7, 8 });
360363

@@ -365,5 +368,5 @@ test "setJustified fires onJustified event" {
365368
test "deinit releases all Rc without leak" {
366369
// If deinit leaks, testing.allocator will report it.
367370
var store = try initTestStore(&.{ 1, 2, 3 });
368-
store.deinit();
371+
store.deinit(testing.allocator);
369372
}

0 commit comments

Comments
 (0)