From 8e45a43ad2e638add15a5028a8b17b60dcdb0c77 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Mon, 13 Mar 2023 02:39:54 +0300 Subject: [PATCH] engine: common: base_cmd: alphabetically order inserts for faster lookups --- engine/common/base_cmd.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/engine/common/base_cmd.c b/engine/common/base_cmd.c index 1bb60c3c..e47a470e 100644 --- a/engine/common/base_cmd.c +++ b/engine/common/base_cmd.c @@ -122,15 +122,23 @@ Add new typed base command to hashmap */ void BaseCmd_Insert( base_command_type_e type, base_command_t *basecmd, const char *name ) { - uint hash = COM_HashKey( name, HASH_SIZE ); - base_command_hashmap_t *elem; + base_command_hashmap_t *elem, *cur, *find; + uint hash = BaseCmd_HashKey( name ); elem = Z_Malloc( sizeof( base_command_hashmap_t ) ); elem->basecmd = basecmd; elem->type = type; elem->name = name; - elem->next = hashed_cmds[hash]; - hashed_cmds[hash] = elem; + + // link the variable in alphanumerical order + for( cur = NULL, find = hashed_cmds[hash]; + find && Q_strcmp( find->name, elem->name ) < 0; + cur = find, find = find->next ); + + if( cur ) cur->next = elem; + else hashed_cmds[hash] = elem; + + elem->next = find; } /*