4 ms·
Where is that early return you mention? I don't see the return keyword used in either examples. They both do the exact same while loop, but Linus' version adds
by jeremiep 10y ago
Where is that early return you mention? I don't see the return keyword used in either examples. They both do the exact same while loop, but Linus' version adds a level of indirection to save a conditional later on. You're still touching the same memory in both cases so they should be identical in terms of cache misses.
From my understanding, the first version has two possible branch misprediction points while Linus' version only has one. This will probably only have a visible impact if the function is called in a loop however.
But to me the biggest advantage of the 2nd variant is its simplicity. This is the only way to stay sane with a growing codebase and keep shipping robust code.
Its no use getting a fast function if you can't integrate it optimally with the rest of the codebase. Raw performance isn't at the micro level but the macro one. This is where simplicity becomes critically important.
- kilburn 10y agoI was referring to the version in this thread's ancestor, which reads: remove_list_entry(entry) { if (head == entry) { head = head->next; return; } for (prev = head; prev->next; prev = prev->next) { if (prev->next == entry) { prev->next = prev->next->next; return; } } }