From 30d22631a6b2e9d5a8df17e7f3f231cc6020cb30 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Mon, 30 Jan 2023 16:10:07 +0100 Subject: [PATCH] tevent: introduce DLIST_DEMOTE_SHORT() It turns out that the overhead of DLIST_DEMOTE() implemented as DLIST_REMOVE();DLIST_ADD_END(), is very high if the list contains only 1 or 2 elements. The next commits will make use of DLIST_DEMOTE_SHORT() for multiplexing multiple tevent_fd structures for a single fd and the most important and common case is a list with just one element. Signed-off-by: Stefan Metzmacher Reviewed-by: Ralph Boehme --- lib/tevent/tevent_dlinklist.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/tevent/tevent_dlinklist.h b/lib/tevent/tevent_dlinklist.h index a775e8dcdc1..49a135a23bd 100644 --- a/lib/tevent/tevent_dlinklist.h +++ b/lib/tevent/tevent_dlinklist.h @@ -156,6 +156,27 @@ do { \ DLIST_ADD_END(list, p); \ } while (0) +/* + * like DLIST_DEMOTE(), but optimized + * for short lists with 0, 1 or 2 elements + */ +#define DLIST_DEMOTE_SHORT(list, p) \ +do { \ + if ((list) == NULL) { \ + /* no reason to demote, just add */ \ + DLIST_ADD(list, p); \ + } else if ((list)->prev == (p)) { \ + /* optimize if p is last */ \ + } else if ((list) == (p)) { \ + /* optimize if p is first */ \ + (list)->prev->next = (p); \ + (list) = (p)->next; \ + (p)->next = NULL; \ + } else { \ + DLIST_DEMOTE(list, p); \ + } \ +} while (0) + /* concatenate two lists - putting all elements of the 2nd list at the end of the first list.