]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: array_foreach_elem() - Small optimization
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 10 Feb 2021 17:47:13 +0000 (19:47 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Fri, 7 May 2021 10:09:35 +0000 (10:09 +0000)
Avoid having to recalculate the end of the array after each loop.
This makes the resulting assembler output similar to array_foreach().

src/lib/array.h

index 85eca3a1a81f01caafa607ad6269beb5cb996382..ee95f1b45e06bbb1ecc72432355edbe191bb577b 100644 (file)
    Latter is impossible if we want to use the variable name as the base for the other variable names
 */
 #  define array_foreach_elem(array, elem) \
-       for (unsigned int _foreach_offset = ARRAY_TYPE_CHECK(array, &elem) + \
-                               COMPILE_ERROR_IF_TRUE(sizeof(elem) > 16)\
+       for (const void *_foreach_end = \
+               CONST_PTR_OFFSET(*(array)->v, (array)->arr.buffer->used), \
+            *_foreach_ptr = CONST_PTR_OFFSET(*(array)->v, ARRAY_TYPE_CHECK(array, &elem) + \
+               COMPILE_ERROR_IF_TRUE(sizeof(elem) > 16)) \
                     ;                                                  \
-            (_foreach_offset < (array)->arr.buffer->used) &&           \
-            (memcpy(&elem, CONST_PTR_OFFSET(*(array)->v, _foreach_offset), sizeof(elem)), TRUE) \
+            (_foreach_ptr != _foreach_end &&           \
+            (memcpy(&elem, _foreach_ptr, sizeof(elem)), TRUE)) \
                ;                                                       \
-            _foreach_offset += sizeof(elem)                            \
-               )
+            _foreach_ptr = CONST_PTR_OFFSET(_foreach_ptr, sizeof(elem)))
 
 #else
 #  define array_foreach(array, elem) \