This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 28454dee2818dabee29053b80279d90006479be1 Author: ouyangxiangzhen <[email protected]> AuthorDate: Wed Oct 28 14:44:30 2026 +0800 sched/hrtimer: Take the new cached head from RB_NEXT on removal When the removed timer is the queue head, the cached head was recomputed with RB_MIN after the unlink, descending the whole left spine of the tree. The in-order successor of the minimum is the new minimum, so capture it with RB_NEXT before the node is unlinked and hand it over as the new cached head (NULL empties the queue). For the leftmost node this is typically a single parent hop, and at worst the same left descent RB_MIN would take - never more. This is the same pattern as Linux's rb_erase_cached(). Measured on qemu-intel64 (TSC deadline, -O3, CONFIG_HRTIMER_TREE), restarting the head of a 64-timer queue (pure remove-head + insert head, no hardware reprogram): 54 -> 52 cycles (~4%). Small queues are within noise; the saving grows with tree depth since RB_MIN always walks the full height while RB_NEXT is usually O(1). Assisted-by: mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <[email protected]> --- sched/hrtimer/hrtimer.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h index 947f455c1b1..ce1e7f1557d 100644 --- a/sched/hrtimer/hrtimer.h +++ b/sched/hrtimer/hrtimer.h @@ -366,12 +366,12 @@ RB_PROTOTYPE(hrtimer_tree_s, hrtimer_s, node, hrtimer_compare); static inline_function void hrtimer_remove(FAR hrtimer_t *hrtimer) { #ifdef CONFIG_HRTIMER_TREE - RB_REMOVE(hrtimer_tree_s, &g_hrtimer_tree, hrtimer); - if (g_hrtimer_head == hrtimer) { - g_hrtimer_head = RB_MIN(hrtimer_tree_s, &g_hrtimer_tree); + g_hrtimer_head = RB_NEXT(hrtimer_tree_s, &g_hrtimer_tree, hrtimer); } + + RB_REMOVE(hrtimer_tree_s, &g_hrtimer_tree, hrtimer); #else list_delete_fast(&hrtimer->node); #endif
