From 1a43aec5622a8614db74bb9530e0602444fdf882 Mon Sep 17 00:00:00 2001 From: dongshengyuan <545258830@qq.com> Date: Wed, 5 Aug 2026 15:54:25 +0800 Subject: [PATCH] sd-netlink: append multipart messages in O(1) Commit 52ceba53d3 made multipart replies append in receive order, but each append walked the chain from the head. Cache the tail on the chain head, so new fragments can be linked directly. The visible next-pointer chain and order stay unchanged. Follow-up for: 52ceba53d3ccd3b57dcb795612de7886aaf928ca --- src/libsystemd/sd-netlink/netlink-internal.h | 1 + src/libsystemd/sd-netlink/netlink-socket.c | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libsystemd/sd-netlink/netlink-internal.h b/src/libsystemd/sd-netlink/netlink-internal.h index c214792147..c7d4cd70d4 100644 --- a/src/libsystemd/sd-netlink/netlink-internal.h +++ b/src/libsystemd/sd-netlink/netlink-internal.h @@ -128,6 +128,7 @@ typedef struct sd_netlink_message { bool sealed:1; sd_netlink_message *next; /* next in a chain of multi-part messages */ + sd_netlink_message *tail; /* last message in a multi-part chain */ } sd_netlink_message; int message_new_empty(sd_netlink *nl, sd_netlink_message **ret); diff --git a/src/libsystemd/sd-netlink/netlink-socket.c b/src/libsystemd/sd-netlink/netlink-socket.c index 385d92a64f..113d999c1e 100644 --- a/src/libsystemd/sd-netlink/netlink-socket.c +++ b/src/libsystemd/sd-netlink/netlink-socket.c @@ -411,9 +411,11 @@ int socket_read_message(sd_netlink *nl) { if (existing) { /* This is the continuation of the previously read messages. * Let's append this message at the end. */ - while (existing->next) - existing = existing->next; - existing->next = TAKE_PTR(m); + if (existing->tail) + existing->tail->next = m; + else + existing->next = m; + existing->tail = TAKE_PTR(m); } else { /* This is the first message. Put it into the queue for partially * received messages. */