From 18ac9855260244e41e6cd3d4c506ef8f3b5d7c45 Mon Sep 17 00:00:00 2001 From: Spoike Date: Sun, 22 Sep 2013 06:28:14 +0000 Subject: [PATCH] Don't realloc console lines quite so much. Should let spammy stuff like timedemos run a tiny smidge faster. git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4479 fc73d0e0-1445-4013-8a0c-d673dee63da5 --- engine/client/console.c | 32 +++++++++++++++++++++++++++----- engine/common/console.h | 3 +++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/engine/client/console.c b/engine/client/console.c index 722fe6771..85dc752b8 100644 --- a/engine/client/console.c +++ b/engine/client/console.c @@ -656,6 +656,7 @@ void Con_PrintCon (console_t *con, char *txt) conchar_t expanded[4096]; conchar_t *c; conline_t *oc; + conline_t *reuse; COM_ParseFunString(CON_WHITEMASK, txt, expanded, sizeof(expanded), false); @@ -673,6 +674,7 @@ void Con_PrintCon (console_t *con, char *txt) break; case '\n': con->cr = false; + reuse = NULL; while (con->linecount >= con_maxlines.ival) { if (con->oldest == con->current) @@ -686,7 +688,10 @@ void Con_PrintCon (console_t *con, char *txt) if (con->display == con->oldest) con->display = con->oldest->newer; con->oldest = con->oldest->newer; - Z_Free(con->oldest->older); + if (reuse) + Z_Free(con->oldest->older); + else + reuse = con->oldest->older; con->oldest->older = NULL; con->linecount--; } @@ -701,9 +706,20 @@ void Con_PrintCon (console_t *con, char *txt) TTS_SayConString((conchar_t*)(con->current+1)); #endif - con->current->newer = Z_Malloc(sizeof(conline_t) + sizeof(conchar_t)); - con->current->newer->older = con->current; - con->current = con->current->newer; + if (!reuse) + { + reuse = Z_Malloc(sizeof(conline_t) + sizeof(conchar_t)); + reuse->maxlength = 1; + } + else + { + reuse->newer = NULL; + reuse->older = NULL; + } + reuse->id = ++con->nextlineid; + reuse->older = con->current; + con->current->newer = reuse; + con->current = reuse; con->current->length = 0; o = (conchar_t *)(con->current+1)+con->current->length; *o = 0; @@ -723,7 +739,13 @@ void Con_PrintCon (console_t *con, char *txt) con->selendline = NULL; oc = con->current; - con->current = BZ_Realloc(con->current, sizeof(*con->current)+(con->current->length+2)*sizeof(conchar_t)); + if (oc->length+2 > oc->maxlength) + { + oc->maxlength = (oc->length+2)+8; + if (oc->maxlength < oc->length) + oc->length = 0; //don't crash from console line overflows. + con->current = BZ_Realloc(con->current, sizeof(*con->current)+(oc->maxlength)*sizeof(conchar_t)); + } if (con->display == oc) con->display = con->current; if (con->oldest == oc) diff --git a/engine/common/console.h b/engine/common/console.h index c9148279c..defbeb392 100644 --- a/engine/common/console.h +++ b/engine/common/console.h @@ -102,7 +102,9 @@ typedef struct conline_s { struct conline_s *older; struct conline_s *newer; unsigned short length; + unsigned short maxlength; unsigned short lines; + unsigned short id; float time; } conline_t; @@ -114,6 +116,7 @@ typedef struct conline_s { typedef struct console_s { int id; + int nextlineid; //the current line being written to. so we can rewrite links etc. char name[64]; char title[64]; int linecount;