sezero / uhexen2

Hexen II: Hammer of Thyrion -- A cross-platform port of Hexen II game.
https://sourceforge.net/projects/uhexen2/
76 stars 14 forks source link

move commands and cvars used by progs to the front for faster access #52

Closed BSzili closed 2 years ago

BSzili commented 2 years ago

Host_Init in the clients now moves the most frequently used prog commands and cvars to the front of the list, so they can be retrieved with the fewest lookups. Fortunately there aren't many of these, so this works well for the stock progs.

sezero commented 2 years ago

Stretching it too far ? :) Does this really help?

BSzili commented 2 years ago

These cvars are queried every single frame, and in the Amiga version it totalled in 503 string compares while traversing the list. Cvars are declared all over the place, so this also caused cache misses that add up over the time. Now it's just 6 string compares. For bf it was 102 compares, and now it's always the first in the list. This is harder to measure but I'm sure it contributed to the stutter when the progs flashed the screen for some reason.

sezero commented 2 years ago

You are literally squeezing it, you know :)

Along with registered, oem should be moved too, should it not? Does hw really not check for more cvars?

Some style clean-up:

diff --git a/engine/h2shared/cmd.c b/engine/h2shared/cmd.c
index 09d6f9b..001d23f 100644
--- a/engine/h2shared/cmd.c
+++ b/engine/h2shared/cmd.c
@@ -662,21 +662,20 @@ qboolean Cmd_CheckCommand (const char *partial)
 Cmd_MoveToFront
 ============
 */
-void Cmd_MoveToFront (const char *cmd_name)
+void Cmd_MoveToFront (const char *name)
 {
-   cmd_function_t  *cmd;
-   cmd_function_t  *cmd_next;
+   cmd_function_t  *cmd, *next;

-   for (cmd = cmd_functions ; cmd ; cmd = cmd->next)
+   for (cmd = cmd_functions; cmd; cmd = cmd->next)
    {
-       cmd_next = cmd->next;
-       if ( cmd_next && !strcmp(cmd_name, cmd_next->name) )
+       next = cmd->next;
+       if (next && !strcmp(name, next->name))
        {
            // remove from the list
-           cmd->next = cmd_next->next;
+           cmd->next = next->next;
            // move to the front
-           cmd_next->next = cmd_functions;
-           cmd_functions = cmd_next;
+           next->next = cmd_functions;
+           cmd_functions = next;
            break;
        }
    }
diff --git a/engine/h2shared/cvar.c b/engine/h2shared/cvar.c
index 9fe6dd4..0714f62 100644
--- a/engine/h2shared/cvar.c
+++ b/engine/h2shared/cvar.c
@@ -361,21 +361,20 @@ qboolean  Cvar_Command (void)
 Cvar_MoveToFront
 ============
 */
-void Cvar_MoveToFront (const char *var_name)
+void Cvar_MoveToFront (const char *name)
 {
-   cvar_t  *var;
-   cvar_t  *var_next;
+   cvar_t  *var, *next;

-   for (var = cvar_vars ; var ; var = var->next)
+   for (var = cvar_vars; var; var = var->next)
    {
-       var_next = var->next;
-       if ( var_next && !strcmp(var_name, var_next->name) )
+       next = var->next;
+       if (next && !strcmp(name, next->name))
        {
            // remove from the list
-           var->next = var_next->next;
+           var->next = next->next;
            // move to the front
-           var_next->next = cvar_vars;
-           cvar_vars = var_next;
+           next->next = cvar_vars;
+           cvar_vars = next;
            break;
        }
    }
sezero commented 2 years ago

Does hw really not check for more cvars?

It does but you concern yourself with client?

BSzili commented 2 years ago

Absolutely, if I can save some cycles without large changes I'll go for it. I left out "oem" as I didn't get any hits for that in PF_cvar during single player, the other 3 is checked every frame. In HW these cvar checks are on the server side, so I left it as is. I'm mostly concerned about the clients, the HW server will most likely run on a separate machine. I can apply the patch later.

BSzili commented 2 years ago

I applied the patch and rebased this.

sezero commented 2 years ago

OK, this is in.