Re: [PATCH] for_each_string_list_item(): behave correctly for empty list
- From
Kaartic Sivaraam <kaarticsivaraam91196@gmail.com>
- Date
- Sep 19, 2017, 14:38 UTC
- Message-ID
- <b03c7b09-853f-a2ed-f73e-7d946c90cedb@gmail.com>
- In-Reply-To
- <b8951886-feab-a87a-9683-3c155cfa98a8@alum.mit.edu>
On Saturday 16 September 2017 09:36 AM, Michael Haggerty wrote:
Show 12 quoted lines
>> Does the following alternate fix work? I think I prefer it because >> it doesn't require introducing a new global. [...] >> #define for_each_string_list_item(item,list) \ >> - for (item = (list)->items; item < (list)->items + (list)->nr; ++item) >> + for (item = (list)->items; \ >> + (list)->items && item < (list)->items + (list)->nr; \ >> + ++item) > This is the possibility that I was referring to as "add[ing] overhead to > each iteration of the loop". I'd rather not add an extra test-and-branch > to every iteration of a loop in which `list->items` is *not* NULL, which > your solution appears to do. Or are compilers routinely able to optimize > the check out?
It seems at least 'gcc' is able to optimize this out even with a -O1 and 'clang' optimizes this out with a -O2. Taking a sneak peek at the 'Makefile' shows that our default is -O2.
For a proof, see https://godbolt.org/g/CPt73L
--- Kaartic