From: Timo Sirainen Date: Wed, 10 Feb 2021 17:47:13 +0000 (+0200) Subject: lib: array_foreach_elem() - Small optimization X-Git-Tag: 2.3.16~208 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=db41d8aef3ab8ea7d9020e25e1de3c8ee07fd808;p=thirdparty%2Fdovecot%2Fcore.git lib: array_foreach_elem() - Small optimization Avoid having to recalculate the end of the array after each loop. This makes the resulting assembler output similar to array_foreach(). --- diff --git a/src/lib/array.h b/src/lib/array.h index 85eca3a1a8..ee95f1b45e 100644 --- a/src/lib/array.h +++ b/src/lib/array.h @@ -84,14 +84,15 @@ 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) \