Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib/plist.c: avoid worst case scenario in plist_add

Worst case scenario of plist_add() happens when the priority of the
inserted plist_node is going to be the largest after the insertion is
done. The cost is going to be more significant when the original plist is
longer, because the iterator is going to traverse the whole plist to find
the correct position to insert the new node.

The situation can be avoided by using a reverse iterator at the same time,
doing so the maximum possible number of iteration is going to shrink from
N to N/2.

The proposed change of plist_add pasts the test in lib/plist.c to validate
its correctness, also add the worst case scenario test for plist_add() in
plist_test().

The worst case test are tested with the size of test_data and test_node
growing from 200 to 1000. The result are showned in the following table,
in which we can observed that the proposed change of plist_add performs
better than the original version, and the difference between these two
implementations are more significant with the size of N growing.

The random case test [1], and best case test [2] are also provided, with
result showing the proposed change performs slightly better in random case
test while the original implementation performs slightly better in best
case test, while the difference in both test are minor, we can see them as
even in those two situations.

-----------------------------------------------------------
| Test size | 200 | 400 | 600 | 800 | 1000 |
-----------------------------------------------------------
| new_plist_add | 140911| 548681| 1220512| 2048493| 3763755|
-----------------------------------------------------------
| old_plist_add | 188198| 774222| 1643547| 3008929| 4947435|
-----------------------------------------------------------

Link: https://lkml.kernel.org/r/20240614154603.65203-1-richard120310@gmail.com
Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
Signed-off-by: Ching-Chun (Jim) Huang <jserv@ccns.ncku.edu.tw>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

I Hsin Cheng and committed by
Andrew Morton
c8dab79f abd8ac05

+37 -1
+37 -1
lib/plist.c
··· 72 72 */ 73 73 void plist_add(struct plist_node *node, struct plist_head *head) 74 74 { 75 - struct plist_node *first, *iter, *prev = NULL; 75 + struct plist_node *first, *iter, *prev = NULL, *last, *reverse_iter; 76 76 struct list_head *node_next = &head->node_list; 77 77 78 78 plist_check_head(head); ··· 83 83 goto ins_node; 84 84 85 85 first = iter = plist_first(head); 86 + last = reverse_iter = list_entry(first->prio_list.prev, struct plist_node, prio_list); 86 87 87 88 do { 88 89 if (node->prio < iter->prio) { 89 90 node_next = &iter->node_list; 90 91 break; 92 + } else if (node->prio >= reverse_iter->prio) { 93 + prev = reverse_iter; 94 + iter = list_entry(reverse_iter->prio_list.next, 95 + struct plist_node, prio_list); 96 + if (likely(reverse_iter != last)) 97 + node_next = &iter->node_list; 98 + break; 91 99 } 92 100 93 101 prev = iter; 94 102 iter = list_entry(iter->prio_list.next, 103 + struct plist_node, prio_list); 104 + reverse_iter = list_entry(reverse_iter->prio_list.prev, 95 105 struct plist_node, prio_list); 96 106 } while (iter != first); 97 107 ··· 265 255 } 266 256 267 257 printk(KERN_DEBUG "end plist test\n"); 258 + 259 + /* Worst case test for plist_add() */ 260 + unsigned int test_data[241]; 261 + 262 + for (i = 0; i < ARRAY_SIZE(test_data); i++) 263 + test_data[i] = i; 264 + 265 + ktime_t start, end, time_elapsed = 0; 266 + 267 + plist_head_init(&test_head); 268 + 269 + for (i = 0; i < ARRAY_SIZE(test_node); i++) { 270 + plist_node_init(test_node + i, 0); 271 + test_node[i].prio = test_data[i]; 272 + } 273 + 274 + for (i = 0; i < ARRAY_SIZE(test_node); i++) { 275 + if (plist_node_empty(test_node + i)) { 276 + start = ktime_get(); 277 + plist_add(test_node + i, &test_head); 278 + end = ktime_get(); 279 + time_elapsed += (end - start); 280 + } 281 + } 282 + 283 + pr_debug("plist_add worst case test time elapsed %lld\n", time_elapsed); 268 284 return 0; 269 285 } 270 286