Skip to content

Commit c55ba9e

Browse files
committed
common/gossip_store: optimize case where entries are filtered out.
@whitslack complained of large CPU usage by connectd at startup; I ran perf record on connectd on my machine (which sees a little spike, only) and I see the cost of reading and discarding the entries: ``` - 95.52% 5.24% lightning_conne lightning_connectd [.] gossip_store_next - 90.28% gossip_store_next + 40.27% tal_alloc_arr_ + 22.78% tal_free + 11.74% crc32c + 9.32% fromwire_peektype + 4.10% __libc_pread64 (inlined) 1.70% be32_to_cpu ``` Much of this is caused by the search for our own gossip: keeping this separately would be even better, but this fix is minimal. Signed-off-by: Rusty Russell <[email protected]> Changelog-Fixed: connectd: reduce initial CPU load when connecting to peers.
1 parent 23d786d commit c55ba9e

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

common/gossip_store.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,16 @@ u8 *gossip_store_next(const tal_t *ctx,
7878
continue;
7979
}
8080

81-
checksum = be32_to_cpu(hdr.crc);
81+
/* Skip any timestamp filtered */
8282
timestamp = be32_to_cpu(hdr.timestamp);
83+
if (!push &&
84+
!timestamp_filter(timestamp_min, timestamp_max,
85+
timestamp)) {
86+
*off += r + msglen;
87+
continue;
88+
}
89+
90+
checksum = be32_to_cpu(hdr.crc);
8391
msg = tal_arr(ctx, u8, msglen);
8492
r = pread(*gossip_store_fd, msg, msglen, *off + r);
8593
if (r != msglen)
@@ -106,10 +114,6 @@ u8 *gossip_store_next(const tal_t *ctx,
106114
&& type != WIRE_CHANNEL_UPDATE
107115
&& type != WIRE_NODE_ANNOUNCEMENT) {
108116
msg = tal_free(msg);
109-
} else if (!push &&
110-
!timestamp_filter(timestamp_min, timestamp_max,
111-
timestamp)) {
112-
msg = tal_free(msg);
113117
} else if (!push && push_only) {
114118
msg = tal_free(msg);
115119
}

gossipd/gossip_store.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ static u32 gossip_store_compact_offline(struct routing_state *rstate)
277277
oldlen = lseek(old_fd, SEEK_END, 0);
278278
newlen = lseek(new_fd, SEEK_END, 0);
279279
append_msg(old_fd, towire_gossip_store_ended(tmpctx, newlen),
280-
0, false, &oldlen);
280+
0, true, &oldlen);
281281
close(old_fd);
282282
status_debug("gossip_store_compact_offline: %zu deleted, %zu copied",
283283
deleted, count);
@@ -565,7 +565,7 @@ bool gossip_store_compact(struct gossip_store *gs)
565565

566566
/* Write end marker now new one is ready */
567567
append_msg(gs->fd, towire_gossip_store_ended(tmpctx, len),
568-
0, false, &gs->len);
568+
0, true, &gs->len);
569569

570570
gs->count = count;
571571
gs->deleted = 0;

0 commit comments

Comments
 (0)