Skip to content

Commit 94be4be

Browse files
committed
ccutil/ambigs: Optimize tesseract::UnicharIdArrayUtils::compare
The compare method is called very often, so even small improvements are important. The new code avoids one comparison in each loop iteration. This results in smaller code (60 bytes for x86_64, gcc). Signed-off-by: Stefan Weil <[email protected]>
1 parent a461592 commit 94be4be

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

ccutil/ambigs.h

+12-10
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,19 @@ class UnicharIdArrayUtils {
5959
// less than length of array2, if any array1[i] is less than array2[i].
6060
// Returns 0 if the arrays are equal, 1 otherwise.
6161
// The function assumes that the arrays are terminated by INVALID_UNICHAR_ID.
62-
static inline int compare(const UNICHAR_ID array1[],
63-
const UNICHAR_ID array2[]) {
64-
const UNICHAR_ID *ptr1 = array1;
65-
const UNICHAR_ID *ptr2 = array2;
66-
while (*ptr1 != INVALID_UNICHAR_ID && *ptr2 != INVALID_UNICHAR_ID) {
67-
if (*ptr1 != *ptr2) return *ptr1 < *ptr2 ? -1 : 1;
68-
++ptr1;
69-
++ptr2;
62+
static inline int compare(const UNICHAR_ID *ptr1,
63+
const UNICHAR_ID *ptr2) {
64+
for (;;) {
65+
const UNICHAR_ID val1 = *ptr1++;
66+
const UNICHAR_ID val2 = *ptr2++;
67+
if (val1 != val2) {
68+
if (val1 == INVALID_UNICHAR_ID) return -1;
69+
if (val2 == INVALID_UNICHAR_ID) return 1;
70+
if (val1 < val2) return -1;
71+
return 1;
72+
}
73+
if (val1 == INVALID_UNICHAR_ID) return 0;
7074
}
71-
if (*ptr1 == INVALID_UNICHAR_ID && *ptr2 == INVALID_UNICHAR_ID) return 0;
72-
return *ptr1 == INVALID_UNICHAR_ID ? -1 : 1;
7375
}
7476

7577
// Look uid in the vector of uids. If found, the index of the matched

0 commit comments

Comments
 (0)