On Saturday 16 September 2017 09:36 AM, Michael Haggerty wrote:
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