Better linked-list traversal in BPF
Better linked-list traversal in BPF
Posted Mar 8, 2024 17:48 UTC (Fri) by epa (subscriber, #39769)In reply to: Better linked-list traversal in BPF by willy
Parent article: Better linked-list traversal in BPF
Perhaps a useful optimization would be to define a magic pointer value which means “next one in memory”. You could even use null for that if you have some other way to signal end of list. Then your code would be like
if (curr->next == MAGIC) ++curr;
else curr = curr->next;
If the linked list is laid out mostly continuously in memory most of the time (perhaps not completely contiguous all of the time, because then you’d be using an array or vector instead) then the branch predictor should be able to learn the first case is more common and the code can run almost as fast as an ordinary loop over an array.
