From ffc381b8695b8ad75e89cf42086cf620d301df54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Sun, 12 Oct 2014 13:01:32 +0200 Subject: [PATCH] Improved performance of twc_list_get. Walk the list backwards if we're getting an index closer to the end than the beginning. --- src/twc-list.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/twc-list.c b/src/twc-list.c index 973c044..a81820d 100644 --- a/src/twc-list.c +++ b/src/twc-list.c @@ -174,11 +174,20 @@ twc_list_get(struct t_twc_list *list, size_t index) size_t current_index; struct t_twc_list_item *item; - twc_list_foreach(list, current_index, item) + if (list->count - index > index / 2) { - if (current_index == index) + twc_list_foreach(list, current_index, item) { - return item; + if (current_index == index) + return item; + } + } + else + { + twc_list_foreach_reverse(list, current_index, item) + { + if (current_index == index) + return item; } }