Skip to content

Commit 99a7550

Browse files
committed
lightningd: optimize notifications.
If nobody is subscribed, have notify_start return NULL and the caller can skip serialization. This is particularly useful for the "log" notification which can get called a lot. Signed-off-by: Rusty Russell <[email protected]>
1 parent 2353c21 commit 99a7550

File tree

3 files changed

+91
-23
lines changed

3 files changed

+91
-23
lines changed

lightningd/notification.c

+74-23
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ bool notifications_have_topic(const struct plugins *plugins, const char *topic)
3838
}
3939

4040
/* Modern notifications X contain an object X */
41-
static struct jsonrpc_notification *notify_start(const char *name)
41+
static struct jsonrpc_notification *notify_start(struct lightningd *ld,
42+
const char *name)
4243
{
4344
struct jsonrpc_notification *n;
4445

46+
/* Optimization: does anyone care? */
47+
if (!plugins_anyone_cares(ld->plugins, name))
48+
return NULL;
49+
4550
n = jsonrpc_notification_start(NULL, name);
4651
json_object_start(n->stream, name);
4752
return n;
@@ -71,7 +76,9 @@ void notify_connect(struct lightningd *ld,
7176
bool incoming,
7277
const struct wireaddr_internal *addr)
7378
{
74-
struct jsonrpc_notification *n = notify_start("connect");
79+
struct jsonrpc_notification *n = notify_start(ld, "connect");
80+
if (!n)
81+
return;
7582
connect_notification_serialize(n->stream, nodeid, incoming, addr);
7683
notify_send(ld, n);
7784
}
@@ -85,7 +92,9 @@ REGISTER_NOTIFICATION(disconnect);
8592

8693
void notify_disconnect(struct lightningd *ld, struct node_id *nodeid)
8794
{
88-
struct jsonrpc_notification *n = notify_start("disconnect");
95+
struct jsonrpc_notification *n = notify_start(ld, "disconnect");
96+
if (!n)
97+
return;
8998
disconnect_notification_serialize(n->stream, nodeid);
9099
notify_send(ld, n);
91100
}
@@ -114,7 +123,9 @@ REGISTER_NOTIFICATION(warning);
114123

115124
void notify_warning(struct lightningd *ld, struct log_entry *l)
116125
{
117-
struct jsonrpc_notification *n = notify_start("warning");
126+
struct jsonrpc_notification *n = notify_start(ld, "warning");
127+
if (!n)
128+
return;
118129
warning_notification_serialize(n->stream, l);
119130
notify_send(ld, n);
120131
}
@@ -133,7 +144,9 @@ void notify_custommsg(struct lightningd *ld,
133144
const struct node_id *peer_id,
134145
const u8 *msg)
135146
{
136-
struct jsonrpc_notification *n = notify_start("custommsg");
147+
struct jsonrpc_notification *n = notify_start(ld, "custommsg");
148+
if (!n)
149+
return;
137150
custommsg_notification_serialize(n->stream, peer_id, msg);
138151
notify_send(ld, n);
139152
}
@@ -167,7 +180,9 @@ void notify_onionmessage_forward_fail(struct lightningd *ld,
167180
const u8 *outgoing,
168181
const struct sciddir_or_pubkey *next_node)
169182
{
170-
struct jsonrpc_notification *n = notify_start("onionmessage_forward_fail");
183+
struct jsonrpc_notification *n = notify_start(ld, "onionmessage_forward_fail");
184+
if (!n)
185+
return;
171186
onionmessage_forward_fail_serialize(n->stream,
172187
source,
173188
incoming,
@@ -198,7 +213,9 @@ void notify_invoice_payment(struct lightningd *ld,
198213
const struct json_escape *label,
199214
const struct bitcoin_outpoint *outpoint)
200215
{
201-
struct jsonrpc_notification *n = notify_start("invoice_payment");
216+
struct jsonrpc_notification *n = notify_start(ld, "invoice_payment");
217+
if (!n)
218+
return;
202219
invoice_payment_notification_serialize(n->stream, amount, preimage, label, outpoint);
203220
notify_send(ld, n);
204221
}
@@ -222,7 +239,9 @@ void notify_invoice_creation(struct lightningd *ld,
222239
const struct preimage *preimage,
223240
const struct json_escape *label)
224241
{
225-
struct jsonrpc_notification *n = notify_start("invoice_creation");
242+
struct jsonrpc_notification *n = notify_start(ld, "invoice_creation");
243+
if (!n)
244+
return;
226245
invoice_creation_notification_serialize(n->stream, amount, preimage, label);
227246
notify_send(ld, n);
228247
}
@@ -249,7 +268,9 @@ void notify_channel_opened(struct lightningd *ld,
249268
const struct bitcoin_txid *funding_txid,
250269
bool channel_ready)
251270
{
252-
struct jsonrpc_notification *n = notify_start("channel_opened");
271+
struct jsonrpc_notification *n = notify_start(ld, "channel_opened");
272+
if (!n)
273+
return;
253274
channel_opened_notification_serialize(n->stream, ld, node_id, funding_sat, funding_txid, channel_ready);
254275
notify_send(ld, n);
255276
}
@@ -292,7 +313,9 @@ void notify_channel_state_changed(struct lightningd *ld,
292313
enum state_change cause,
293314
const char *message)
294315
{
295-
struct jsonrpc_notification *n = notify_start("channel_state_changed");
316+
struct jsonrpc_notification *n = notify_start(ld, "channel_state_changed");
317+
if (!n)
318+
return;
296319
channel_state_changed_notification_serialize(n->stream, peer_id, cid, scid, timestamp, old_state, new_state, cause, message);
297320
notify_send(ld, n);
298321
}
@@ -361,7 +384,9 @@ void notify_forward_event(struct lightningd *ld,
361384
u64 created_index,
362385
u64 updated_index)
363386
{
364-
struct jsonrpc_notification *n = notify_start("forward_event");
387+
struct jsonrpc_notification *n = notify_start(ld, "forward_event");
388+
if (!n)
389+
return;
365390
forward_event_notification_serialize(n->stream, in, scid_out, amount_out, state, failcode, resolved_time, forward_style, created_index, updated_index);
366391
notify_send(ld, n);
367392
}
@@ -371,7 +396,9 @@ REGISTER_NOTIFICATION(sendpay_success);
371396
void notify_sendpay_success(struct lightningd *ld,
372397
const struct wallet_payment *payment)
373398
{
374-
struct jsonrpc_notification *n = notify_start("sendpay_success");
399+
struct jsonrpc_notification *n = notify_start(ld, "sendpay_success");
400+
if (!n)
401+
return;
375402
json_add_payment_fields(n->stream, payment);
376403
notify_send(ld, n);
377404
}
@@ -407,7 +434,9 @@ void notify_sendpay_failure(struct lightningd *ld,
407434
const struct routing_failure *fail,
408435
const char *errmsg)
409436
{
410-
struct jsonrpc_notification *n = notify_start("sendpay_failure");
437+
struct jsonrpc_notification *n = notify_start(ld, "sendpay_failure");
438+
if (!n)
439+
return;
411440
sendpay_failure_notification_serialize(n->stream, payment, pay_errcode, onionreply, fail, errmsg);
412441
notify_send(ld, n);
413442
}
@@ -492,7 +521,9 @@ REGISTER_NOTIFICATION(coin_movement);
492521
void notify_coin_mvt(struct lightningd *ld,
493522
const struct coin_mvt *mvt)
494523
{
495-
struct jsonrpc_notification *n = notify_start("coin_movement");
524+
struct jsonrpc_notification *n = notify_start(ld, "coin_movement");
525+
if (!n)
526+
return;
496527
coin_movement_notification_serialize(n->stream, mvt);
497528
notify_send(ld, n);
498529
}
@@ -522,7 +553,9 @@ REGISTER_NOTIFICATION(balance_snapshot);
522553
void notify_balance_snapshot(struct lightningd *ld,
523554
const struct balance_snapshot *snap)
524555
{
525-
struct jsonrpc_notification *n = notify_start("balance_snapshot");
556+
struct jsonrpc_notification *n = notify_start(ld, "balance_snapshot");
557+
if (!n)
558+
return;
526559
balance_snapshot_serialize(n->stream, snap);
527560
notify_send(ld, n);
528561
}
@@ -539,7 +572,9 @@ REGISTER_NOTIFICATION(block_added);
539572
void notify_block_added(struct lightningd *ld,
540573
const struct block *block)
541574
{
542-
struct jsonrpc_notification *n = notify_start("block_added");
575+
struct jsonrpc_notification *n = notify_start(ld, "block_added");
576+
if (!n)
577+
return;
543578
block_added_notification_serialize(n->stream, block);
544579
notify_send(ld, n);
545580
}
@@ -558,7 +593,9 @@ void notify_openchannel_peer_sigs(struct lightningd *ld,
558593
const struct channel_id *cid,
559594
const struct wally_psbt *psbt)
560595
{
561-
struct jsonrpc_notification *n = notify_start("openchannel_peer_sigs");
596+
struct jsonrpc_notification *n = notify_start(ld, "openchannel_peer_sigs");
597+
if (!n)
598+
return;
562599
openchannel_peer_sigs_serialize(n->stream, cid, psbt);
563600
notify_send(ld, n);
564601
}
@@ -574,7 +611,9 @@ REGISTER_NOTIFICATION(channel_open_failed);
574611
void notify_channel_open_failed(struct lightningd *ld,
575612
const struct channel_id *cid)
576613
{
577-
struct jsonrpc_notification *n = notify_start("channel_open_failed");
614+
struct jsonrpc_notification *n = notify_start(ld, "channel_open_failed");
615+
if (!n)
616+
return;
578617
channel_open_failed_serialize(n->stream, cid);
579618
notify_send(ld, n);
580619
}
@@ -583,7 +622,9 @@ REGISTER_NOTIFICATION(shutdown);
583622

584623
bool notify_plugin_shutdown(struct lightningd *ld, struct plugin *p)
585624
{
586-
struct jsonrpc_notification *n = notify_start("shutdown");
625+
struct jsonrpc_notification *n = notify_start(ld, "shutdown");
626+
if (!n)
627+
return false;
587628
json_object_end(n->stream);
588629
jsonrpc_notification_end(n);
589630
return plugin_single_notify(p, take(n));
@@ -593,7 +634,9 @@ bool notify_deprecated_oneshot(struct lightningd *ld,
593634
struct plugin *p,
594635
bool deprecated_ok)
595636
{
596-
struct jsonrpc_notification *n = notify_start("deprecated_oneshot");
637+
struct jsonrpc_notification *n = notify_start(ld, "deprecated_oneshot");
638+
if (!n)
639+
return false;
597640
json_add_bool(n->stream, "deprecated_ok", deprecated_ok);
598641
json_object_end(n->stream);
599642
jsonrpc_notification_end(n);
@@ -616,7 +659,11 @@ REGISTER_NOTIFICATION(log);
616659

617660
void notify_log(struct lightningd *ld, const struct log_entry *l)
618661
{
619-
struct jsonrpc_notification *n = notify_start("log");
662+
struct jsonrpc_notification *n;
663+
664+
n = notify_start(ld, "log");
665+
if (!n)
666+
return;
620667
log_notification_serialize(n->stream, l);
621668
notify_send(ld, n);
622669
}
@@ -637,7 +684,9 @@ REGISTER_NOTIFICATION(plugin_started);
637684

638685
void notify_plugin_started(struct lightningd *ld, struct plugin *plugin)
639686
{
640-
struct jsonrpc_notification *n = notify_start("plugin_started");
687+
struct jsonrpc_notification *n = notify_start(ld, "plugin_started");
688+
if (!n)
689+
return;
641690
plugin_notification_serialize(n->stream, plugin);
642691
notify_send(ld, n);
643692
}
@@ -646,7 +695,9 @@ REGISTER_NOTIFICATION(plugin_stopped);
646695

647696
void notify_plugin_stopped(struct lightningd *ld, struct plugin *plugin)
648697
{
649-
struct jsonrpc_notification *n = notify_start("plugin_stopped");
698+
struct jsonrpc_notification *n = notify_start(ld, "plugin_stopped");
699+
if (!n)
700+
return;
650701
plugin_notification_serialize(n->stream, plugin);
651702
notify_send(ld, n);
652703
}

lightningd/plugin.c

+14
Original file line numberDiff line numberDiff line change
@@ -2447,6 +2447,20 @@ static bool plugin_subscriptions_contains(struct plugin *plugin,
24472447
return false;
24482448
}
24492449

2450+
bool plugins_anyone_cares(struct plugins *plugins, const char *method)
2451+
{
2452+
struct plugin *p;
2453+
2454+
if (!plugins)
2455+
return false;
2456+
2457+
list_for_each(&plugins->plugins, p, list) {
2458+
if (plugin_subscriptions_contains(p, method))
2459+
return true;
2460+
}
2461+
return false;
2462+
}
2463+
24502464
bool plugin_single_notify(struct plugin *p,
24512465
const struct jsonrpc_notification *n TAKES)
24522466
{

lightningd/plugin.h

+3
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ void plugins_set_builtin_plugins_dir(struct plugins *plugins,
366366
/* Is this option for a plugin? */
367367
bool is_plugin_opt(const struct opt_table *ot);
368368

369+
/* Does any plugin care about this notification? */
370+
bool plugins_anyone_cares(struct plugins *plugins, const char *method);
371+
369372
/* Add this field if this ot is owned by a plugin */
370373
void json_add_config_plugin(struct json_stream *stream,
371374
const struct plugins *plugins,

0 commit comments

Comments
 (0)