From fc017286a63e29a064729c897800bc55bd34d89d Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Wed, 22 Jan 2025 05:24:33 +0300 Subject: [PATCH] engine: base_cmd: slightly faster search based on assumption that list is always ordered --- engine/common/base_cmd.c | 82 ++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/engine/common/base_cmd.c b/engine/common/base_cmd.c index 9a7914f3..3c75ff09 100644 --- a/engine/common/base_cmd.c +++ b/engine/common/base_cmd.c @@ -43,11 +43,27 @@ Find base command in bucket */ static base_command_hashmap_t *BaseCmd_FindInBucket( base_command_hashmap_t *bucket, base_command_type_e type, const char *name ) { - base_command_hashmap_t *i = bucket; - for( ; i && ( i->type != type || Q_stricmp( name, i->name ) ); // filter out - i = i->next ); + base_command_hashmap_t *i; - return i; + for( i = bucket; i != NULL; i = i->next ) + { + int cmp; + + if( i->type != type ) + continue; + + cmp = Q_stricmp( i->name, name ); + + if( cmp < 0 ) + continue; + + if( cmp > 0 ) + break; + + return i; + } + + return NULL; } /* @@ -91,27 +107,31 @@ void BaseCmd_FindAll( const char *name, base_command_t **cmd, base_command_t **a base_command_hashmap_t *base = BaseCmd_GetBucket( name ); base_command_hashmap_t *i = base; - ASSERT( cmd && alias && cvar ); - *cmd = *alias = *cvar = NULL; for( ; i; i = i->next ) { - if( !Q_stricmp( i->name, name ) ) + int cmp = Q_stricmp( i->name, name ); + + if( cmp < 0 ) + continue; + + if( cmp > 0 ) + break; + + switch( i->type ) { - switch( i->type ) - { - case HM_CMD: - *cmd = i->basecmd; - break; - case HM_CMDALIAS: - *alias = i->basecmd; - break; - case HM_CVAR: - *cvar = i->basecmd; - break; - default: break; - } + case HM_CMD: + *cmd = i->basecmd; + break; + case HM_CMDALIAS: + *alias = i->basecmd; + break; + case HM_CVAR: + *cvar = i->basecmd; + break; + default: + break; } } } @@ -136,7 +156,7 @@ void BaseCmd_Insert( base_command_type_e type, base_command_t *basecmd, const ch // link the variable in alphanumerical order for( cur = NULL, find = hashed_cmds[hash]; - find && Q_strcmp( find->name, elem->name ) < 0; + find && Q_stricmp( find->name, elem->name ) < 0; cur = find, find = find->next ); if( cur ) cur->next = elem; @@ -157,9 +177,23 @@ void BaseCmd_Remove( base_command_type_e type, const char *name ) uint hash = BaseCmd_HashKey( name ); base_command_hashmap_t *i, *prev; - for( prev = NULL, i = hashed_cmds[hash]; i && - ( Q_strcmp( i->name, name ) || i->type != type); // filter out - prev = i, i = i->next ); + for( prev = NULL, i = hashed_cmds[hash]; i != NULL; prev = i, i = i->next ) + { + int cmp; + + if( i->type != type ) + continue; + + cmp = Q_stricmp( i->name, name ); + + if( cmp < 0 ) + continue; + + if( cmp > 0 ) + i = NULL; + + break; + } if( !i ) {