3232#include "lookup-table.h"
3333
3434// converts upper to lower case, and leaves other characters unchanged
35+ // Optimized version using pointer arithmetic for better performance
3536void strtolower (char * str )
3637{
37- int i = 0 ;
38- while ( str [ i ]){ str [ i ] = tolower (str [ i ]); i ++ ; }
38+ for (; * str ; ++ str )
39+ * str = tolower (* str );
3940}
4041
4142/**
@@ -54,7 +55,7 @@ void strtolower(char *str)
5455 */
5556static uint32_t __attribute__ ((pure )) hashStr (const char * s )
5657{
57- // Jenkins' One-at-a-Time hash
58+ // Jenkins' One-at-a-Time hash (optimized version)
5859 // (http://www.burtleburtle.net/bob/hash/doobs.html)
5960 uint32_t hash = 0 ;
6061 for (; * s ; ++ s )
@@ -64,6 +65,7 @@ static uint32_t __attribute__ ((pure)) hashStr(const char *s)
6465 hash ^= hash >> 6 ;
6566 }
6667
68+ // Final mixing to ensure good distribution
6769 hash += hash << 3 ;
6870 hash ^= hash >> 11 ;
6971 hash += hash << 15 ;
@@ -997,16 +999,19 @@ const char * __attribute__ ((pure)) get_blocked_statuslist(void)
997999 unsigned int first = 0 ;
9981000 // Open parenthesis
9991001 blocked_list [0 ] = '(' ;
1002+ size_t pos = 1 ; // Track current position instead of calling strlen repeatedly
10001003 for (enum query_status status = 0 ; status < QUERY_STATUS_MAX ; status ++ )
10011004 if (is_blocked (status ))
1002- snprintf (blocked_list + strlen (blocked_list ),
1003- sizeof (blocked_list ) - strlen (blocked_list ),
1004- "%s%d" , first ++ < 1 ? "" : "," , status );
1005+ {
1006+ int written = snprintf (blocked_list + pos , sizeof (blocked_list ) - pos ,
1007+ "%s%d" , first ++ < 1 ? "" : "," , status );
1008+ if (written > 0 )
1009+ pos += written ;
1010+ }
10051011
10061012 // Close parenthesis
1007- const size_t len = strlen (blocked_list );
1008- blocked_list [len ] = ')' ;
1009- blocked_list [len + 1 ] = '\0' ;
1013+ blocked_list [pos ] = ')' ;
1014+ blocked_list [pos + 1 ] = '\0' ;
10101015 return blocked_list ;
10111016}
10121017
@@ -1020,16 +1025,19 @@ const char * __attribute__ ((pure)) get_cached_statuslist(void)
10201025 unsigned int first = 0 ;
10211026 // Open parenthesis
10221027 cached_list [0 ] = '(' ;
1028+ size_t pos = 1 ; // Track current position instead of calling strlen repeatedly
10231029 for (enum query_status status = 0 ; status < QUERY_STATUS_MAX ; status ++ )
10241030 if (is_cached (status ))
1025- snprintf (cached_list + strlen (cached_list ),
1026- sizeof (cached_list ) - strlen (cached_list ),
1027- "%s%d" , first ++ < 1 ? "" : "," , status );
1031+ {
1032+ int written = snprintf (cached_list + pos , sizeof (cached_list ) - pos ,
1033+ "%s%d" , first ++ < 1 ? "" : "," , status );
1034+ if (written > 0 )
1035+ pos += written ;
1036+ }
10281037
10291038 // Close parenthesis
1030- const size_t len = strlen (cached_list );
1031- cached_list [len ] = ')' ;
1032- cached_list [len + 1 ] = '\0' ;
1039+ cached_list [pos ] = ')' ;
1040+ cached_list [pos + 1 ] = '\0' ;
10331041 return cached_list ;
10341042}
10351043
@@ -1043,16 +1051,19 @@ const char * __attribute__ ((pure)) get_permitted_statuslist(void)
10431051 unsigned int first = 0 ;
10441052 // Open parenthesis
10451053 permitted_list [0 ] = '(' ;
1054+ size_t pos = 1 ; // Track current position instead of calling strlen repeatedly
10461055 for (enum query_status status = 0 ; status < QUERY_STATUS_MAX ; status ++ )
10471056 if (!is_blocked (status ))
1048- snprintf (permitted_list + strlen (permitted_list ),
1049- sizeof (permitted_list ) - strlen (permitted_list ),
1050- "%s%d" , first ++ < 1 ? "" : "," , status );
1057+ {
1058+ int written = snprintf (permitted_list + pos , sizeof (permitted_list ) - pos ,
1059+ "%s%d" , first ++ < 1 ? "" : "," , status );
1060+ if (written > 0 )
1061+ pos += written ;
1062+ }
10511063
10521064 // Close parenthesis
1053- const size_t len = strlen (permitted_list );
1054- permitted_list [len ] = ')' ;
1055- permitted_list [len + 1 ] = '\0' ;
1065+ permitted_list [pos ] = ')' ;
1066+ permitted_list [pos + 1 ] = '\0' ;
10561067 return permitted_list ;
10571068}
10581069
0 commit comments