Skip to content

Commit 6e2dea7

Browse files
committed
Use append-only logic in buildPullRequests
also use unmanaged struct
1 parent ac718bd commit 6e2dea7

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/gossip/service.zig

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const GossipMessageWithEndpoint = struct {
6666

6767
pub const PULL_REQUEST_RATE: Duration = .fromSecs(1);
6868
pub const PULL_RESPONSE_TIMEOUT: Duration = .fromSecs(5);
69-
pub const ACTIVE_SET_REFRESH_RATE: Duration = .fromSecs(15);
69+
pub const ACTIVE_SET_REFRESH_RATE: Duration = .fromMillis(500);
7070
pub const DATA_TIMEOUT: Duration = .fromSecs(15);
7171
pub const TABLE_TRIM_RATE: Duration = .fromSecs(10);
7272
pub const BUILD_MESSAGE_LOOP_MIN: Duration = .fromSecs(1);
@@ -928,15 +928,16 @@ pub const GossipService = struct {
928928
defer pull_req_timer.reset();
929929
// this also includes sending ping messages to other peers
930930
const now = getWallclockMs();
931-
const pull_req_packets = self.buildPullRequests(
931+
var pull_req_packets = self.buildPullRequests(
932+
self.allocator,
932933
random,
933934
pull_request.MAX_BLOOM_SIZE,
934935
now,
935936
) catch |e| {
936937
self.logger.err().logf("failed to generate pull requests: {any}", .{e});
937938
break :pull_blk;
938939
};
939-
defer pull_req_packets.deinit();
940+
defer pull_req_packets.deinit(self.allocator);
940941
for (pull_req_packets.items) |packet| {
941942
try self.packet_outgoing_channel.send(packet);
942943
}
@@ -1213,11 +1214,12 @@ pub const GossipService = struct {
12131214
/// to be sent to a random set of gossip nodes.
12141215
fn buildPullRequests(
12151216
self: *GossipService,
1217+
gpa: std.mem.Allocator,
12161218
random: std.Random,
12171219
/// the bloomsize of the pull request's filters
12181220
bloom_size: usize,
12191221
now: u64,
1220-
) !ArrayList(Packet) {
1222+
) !std.ArrayListUnmanaged(Packet) {
12211223
const zone = tracy.Zone.init(@src(), .{ .name = "gossip buildPullRequests" });
12221224
defer zone.deinit();
12231225

@@ -1280,13 +1282,13 @@ pub const GossipService = struct {
12801282
if (num_peers != 0) n_packets += filters.items.len;
12811283
if (entrypoint_index != null) n_packets += filters.items.len;
12821284

1283-
var packet_batch = try ArrayList(Packet).initCapacity(self.allocator, n_packets);
1284-
packet_batch.appendNTimesAssumeCapacity(Packet.ANY_EMPTY, n_packets);
1285-
var packet_index: usize = 0;
1285+
var packet_batch: std.ArrayListUnmanaged(Packet) = .empty;
1286+
errdefer packet_batch.deinit(gpa);
1287+
try packet_batch.ensureTotalCapacityPrecise(gpa, n_packets);
12861288

12871289
// update wallclock and sign
12881290
self.my_contact_info.wallclock = now;
1289-
const my_contact_info_value = SignedGossipData.initSigned(
1291+
const my_contact_info_value: SignedGossipData = .initSigned(
12901292
&self.my_keypair,
12911293
// safe to copy contact info since it is immediately serialized
12921294
.{ .ContactInfo = self.my_contact_info },
@@ -1304,12 +1306,10 @@ pub const GossipService = struct {
13041306
}
13051307
if (peer_contact_info.gossip_addr) |gossip_addr| {
13061308
const message: GossipMessage = .{ .PullRequest = .{ filter_i, my_contact_info_value } };
1307-
var packet = &packet_batch.items[packet_index];
1308-
1309-
const bytes = try bincode.writeToSlice(&packet.buffer, message, bincode.Params{});
1309+
const packet = try packet_batch.addOne(gpa);
1310+
const bytes = try bincode.writeToSlice(&packet.buffer, message, .standard);
13101311
packet.size = bytes.len;
13111312
packet.addr = gossip_addr;
1312-
packet_index += 1;
13131313
}
13141314
}
13151315
}
@@ -1318,10 +1318,9 @@ pub const GossipService = struct {
13181318
if (entrypoint_index) |entrypoint_idx| {
13191319
const entrypoint = self.entrypoints[entrypoint_idx];
13201320
for (filters.items) |filter| {
1321-
const packet = &packet_batch.items[packet_index];
1321+
const packet = try packet_batch.addOne(gpa);
13221322
const message: GossipMessage = .{ .PullRequest = .{ filter, my_contact_info_value } };
13231323
try packet.populateFromBincode(entrypoint.addr, message);
1324-
packet_index += 1;
13251324
}
13261325
}
13271326

@@ -2963,11 +2962,11 @@ fn testBuildPullRequests(
29632962
}
29642963
}
29652964

2966-
var packets = gossip_service.buildPullRequests(random, 2, now) catch |err| {
2965+
var packets = gossip_service.buildPullRequests(allocator, random, 2, now) catch |err| {
29672966
std.log.err("\nThe failing now time is: '{d}'\n", .{now});
29682967
return err;
29692968
};
2970-
defer packets.deinit();
2969+
defer packets.deinit(allocator);
29712970

29722971
try std.testing.expect(packets.items.len > 1);
29732972
try std.testing.expect(!std.mem.eql(u8, packets.items[0].data(), packets.items[1].data()));

0 commit comments

Comments
 (0)