Skip to content

Commit 974d3af

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 2d7f268 commit 974d3af

File tree

3 files changed

+91
-23
lines changed

3 files changed

+91
-23
lines changed

lightningd/notification.c

Lines changed: 74 additions & 23 deletions
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
}
@@ -296,7 +317,9 @@ void notify_channel_state_changed(struct lightningd *ld,
296317
enum state_change cause,
297318
const char *message)
298319
{
299-
struct jsonrpc_notification *n = notify_start("channel_state_changed");
320+
struct jsonrpc_notification *n = notify_start(ld, "channel_state_changed");
321+
if (!n)
322+
return;
300323
channel_state_changed_notification_serialize(n->stream, ld, peer_id, cid, scid, timestamp, old_state, new_state, cause, message);
301324
notify_send(ld, n);
302325
}
@@ -365,7 +388,9 @@ void notify_forward_event(struct lightningd *ld,
365388
u64 created_index,
366389
u64 updated_index)
367390
{
368-
struct jsonrpc_notification *n = notify_start("forward_event");
391+
struct jsonrpc_notification *n = notify_start(ld, "forward_event");
392+
if (!n)
393+
return;
369394
forward_event_notification_serialize(n->stream, in, scid_out, amount_out, state, failcode, resolved_time, forward_style, created_index, updated_index);
370395
notify_send(ld, n);
371396
}
@@ -375,7 +400,9 @@ REGISTER_NOTIFICATION(sendpay_success);
375400
void notify_sendpay_success(struct lightningd *ld,
376401
const struct wallet_payment *payment)
377402
{
378-
struct jsonrpc_notification *n = notify_start("sendpay_success");
403+
struct jsonrpc_notification *n = notify_start(ld, "sendpay_success");
404+
if (!n)
405+
return;
379406
json_add_payment_fields(n->stream, payment);
380407
notify_send(ld, n);
381408
}
@@ -411,7 +438,9 @@ void notify_sendpay_failure(struct lightningd *ld,
411438
const struct routing_failure *fail,
412439
const char *errmsg)
413440
{
414-
struct jsonrpc_notification *n = notify_start("sendpay_failure");
441+
struct jsonrpc_notification *n = notify_start(ld, "sendpay_failure");
442+
if (!n)
443+
return;
415444
sendpay_failure_notification_serialize(n->stream, payment, pay_errcode, onionreply, fail, errmsg);
416445
notify_send(ld, n);
417446
}
@@ -496,7 +525,9 @@ REGISTER_NOTIFICATION(coin_movement);
496525
void notify_coin_mvt(struct lightningd *ld,
497526
const struct coin_mvt *mvt)
498527
{
499-
struct jsonrpc_notification *n = notify_start("coin_movement");
528+
struct jsonrpc_notification *n = notify_start(ld, "coin_movement");
529+
if (!n)
530+
return;
500531
coin_movement_notification_serialize(n->stream, mvt);
501532
notify_send(ld, n);
502533
}
@@ -526,7 +557,9 @@ REGISTER_NOTIFICATION(balance_snapshot);
526557
void notify_balance_snapshot(struct lightningd *ld,
527558
const struct balance_snapshot *snap)
528559
{
529-
struct jsonrpc_notification *n = notify_start("balance_snapshot");
560+
struct jsonrpc_notification *n = notify_start(ld, "balance_snapshot");
561+
if (!n)
562+
return;
530563
balance_snapshot_serialize(n->stream, snap);
531564
notify_send(ld, n);
532565
}
@@ -543,7 +576,9 @@ REGISTER_NOTIFICATION(block_added);
543576
void notify_block_added(struct lightningd *ld,
544577
const struct block *block)
545578
{
546-
struct jsonrpc_notification *n = notify_start("block_added");
579+
struct jsonrpc_notification *n = notify_start(ld, "block_added");
580+
if (!n)
581+
return;
547582
block_added_notification_serialize(n->stream, block);
548583
notify_send(ld, n);
549584
}
@@ -562,7 +597,9 @@ void notify_openchannel_peer_sigs(struct lightningd *ld,
562597
const struct channel_id *cid,
563598
const struct wally_psbt *psbt)
564599
{
565-
struct jsonrpc_notification *n = notify_start("openchannel_peer_sigs");
600+
struct jsonrpc_notification *n = notify_start(ld, "openchannel_peer_sigs");
601+
if (!n)
602+
return;
566603
openchannel_peer_sigs_serialize(n->stream, cid, psbt);
567604
notify_send(ld, n);
568605
}
@@ -578,7 +615,9 @@ REGISTER_NOTIFICATION(channel_open_failed);
578615
void notify_channel_open_failed(struct lightningd *ld,
579616
const struct channel_id *cid)
580617
{
581-
struct jsonrpc_notification *n = notify_start("channel_open_failed");
618+
struct jsonrpc_notification *n = notify_start(ld, "channel_open_failed");
619+
if (!n)
620+
return;
582621
channel_open_failed_serialize(n->stream, cid);
583622
notify_send(ld, n);
584623
}
@@ -587,7 +626,9 @@ REGISTER_NOTIFICATION(shutdown);
587626

588627
bool notify_plugin_shutdown(struct lightningd *ld, struct plugin *p)
589628
{
590-
struct jsonrpc_notification *n = notify_start("shutdown");
629+
struct jsonrpc_notification *n = notify_start(ld, "shutdown");
630+
if (!n)
631+
return false;
591632
json_object_end(n->stream);
592633
jsonrpc_notification_end(n);
593634
return plugin_single_notify(p, take(n));
@@ -597,7 +638,9 @@ bool notify_deprecated_oneshot(struct lightningd *ld,
597638
struct plugin *p,
598639
bool deprecated_ok)
599640
{
600-
struct jsonrpc_notification *n = notify_start("deprecated_oneshot");
641+
struct jsonrpc_notification *n = notify_start(ld, "deprecated_oneshot");
642+
if (!n)
643+
return false;
601644
json_add_bool(n->stream, "deprecated_ok", deprecated_ok);
602645
json_object_end(n->stream);
603646
jsonrpc_notification_end(n);
@@ -620,7 +663,11 @@ REGISTER_NOTIFICATION(log);
620663

621664
void notify_log(struct lightningd *ld, const struct log_entry *l)
622665
{
623-
struct jsonrpc_notification *n = notify_start("log");
666+
struct jsonrpc_notification *n;
667+
668+
n = notify_start(ld, "log");
669+
if (!n)
670+
return;
624671
log_notification_serialize(n->stream, l);
625672
notify_send(ld, n);
626673
}
@@ -641,7 +688,9 @@ REGISTER_NOTIFICATION(plugin_started);
641688

642689
void notify_plugin_started(struct lightningd *ld, struct plugin *plugin)
643690
{
644-
struct jsonrpc_notification *n = notify_start("plugin_started");
691+
struct jsonrpc_notification *n = notify_start(ld, "plugin_started");
692+
if (!n)
693+
return;
645694
plugin_notification_serialize(n->stream, plugin);
646695
notify_send(ld, n);
647696
}
@@ -650,7 +699,9 @@ REGISTER_NOTIFICATION(plugin_stopped);
650699

651700
void notify_plugin_stopped(struct lightningd *ld, struct plugin *plugin)
652701
{
653-
struct jsonrpc_notification *n = notify_start("plugin_stopped");
702+
struct jsonrpc_notification *n = notify_start(ld, "plugin_stopped");
703+
if (!n)
704+
return;
654705
plugin_notification_serialize(n->stream, plugin);
655706
notify_send(ld, n);
656707
}

lightningd/plugin.c

Lines changed: 14 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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)