[zone] Make Hunk_RawAlloc the core hunk allocator

It turns out that copying just "unknown" is a significant performance
hit when doing over 100M allocations. Making Hunk_RawAlloc the core and
initializing the name field with a single 0 shaved about a second off
`qfvis gmsp3v2.bsp` (from about 39s to about 38s).
This commit is contained in:
Bill Currie 2022-06-06 13:33:54 +09:00
parent 5c94c40985
commit ef64161835

View file

@ -655,7 +655,8 @@ Hunk_Print (memhunk_t *hunk, qboolean all)
// print the single block // print the single block
if (all) { if (all) {
const int sz = sizeof (h->name); const int sz = sizeof (h->name);
Sys_Printf ("%8p :%8zd %*.*s\n", h, h->size, sz, sz, h->name); Sys_Printf ("%8p :%8zd %*.*s\n", h, h->size, sz, sz,
h->name[0] ? h->name : "unknown");
} }
// print the total // print the total
@ -664,7 +665,7 @@ Hunk_Print (memhunk_t *hunk, qboolean all)
if (!all) { if (!all) {
const int sz = sizeof (h->name); const int sz = sizeof (h->name);
Sys_Printf (" :%8i %*.*s (TOTAL)\n", Sys_Printf (" :%8i %*.*s (TOTAL)\n",
sum, sz, sz, h->name); sum, sz, sz, h->name[0] ? h->name : "unknown");
} }
count = 0; count = 0;
sum = 0; sum = 0;
@ -707,7 +708,7 @@ Hunk_HighMark (memhunk_t *hunk)
} }
VISIBLE void * VISIBLE void *
Hunk_RawAllocName (memhunk_t *hunk, size_t size, const char *name) Hunk_RawAlloc (memhunk_t *hunk, size_t size)
{ {
hunkblk_t *h; hunkblk_t *h;
@ -718,13 +719,13 @@ Hunk_RawAllocName (memhunk_t *hunk, size_t size, const char *name)
size = sizeof (hunkblk_t) + ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1)); size = sizeof (hunkblk_t) + ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1));
if (hunk->size - hunk->low_used - hunk->high_used < size) { if (hunk->size - hunk->low_used - hunk->high_used < size) {
Hunk_HighMark (hunk); Hunk_HighMark (hunk); // force free of temp hunk
Cache_FreeLRU (hunk);
} }
if (hunk->size - hunk->low_used - hunk->high_used < size) { if (hunk->size - hunk->low_used - hunk->high_used < size) {
int mem = hunk->size / (1024 * 1024); int mem = hunk->size / (1024 * 1024);
mem += 8; mem += 8;
mem &= ~7; mem &= ~7;
Hunk_Print (hunk, 1);
Cache_Profile_r (hunk); Cache_Profile_r (hunk);
Sys_Error Sys_Error
("Not enough RAM allocated. Try starting using \"-mem %d\" on " ("Not enough RAM allocated. Try starting using \"-mem %d\" on "
@ -740,11 +741,20 @@ Hunk_RawAllocName (memhunk_t *hunk, size_t size, const char *name)
h->size = size; h->size = size;
h->sentinal1 = HUNK_SENTINAL; h->sentinal1 = HUNK_SENTINAL;
h->sentinal2 = HUNK_SENTINAL; h->sentinal2 = HUNK_SENTINAL;
memccpy (h->name, name, 0, sizeof (h->name)); h->name[0] = 0;
return (void *) (h + 1); return (void *) (h + 1);
} }
VISIBLE void *
Hunk_RawAllocName (memhunk_t *hunk, size_t size, const char *name)
{
void *mem = Hunk_RawAlloc (hunk, size);
hunkblk_t *h = ((hunkblk_t *) mem) - 1;
memccpy (h->name, name, 0, sizeof (h->name));
return mem;
}
VISIBLE void * VISIBLE void *
Hunk_AllocName (memhunk_t *hunk, size_t size, const char *name) Hunk_AllocName (memhunk_t *hunk, size_t size, const char *name)
{ {
@ -754,17 +764,13 @@ Hunk_AllocName (memhunk_t *hunk, size_t size, const char *name)
return mem; return mem;
} }
VISIBLE void *
Hunk_RawAlloc (memhunk_t *hunk, size_t size)
{
return Hunk_RawAllocName (hunk, size, "unknown");
}
VISIBLE void * VISIBLE void *
Hunk_Alloc (memhunk_t *hunk, size_t size) Hunk_Alloc (memhunk_t *hunk, size_t size)
{ {
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
return Hunk_AllocName (hunk, size, "unknown"); void *mem = Hunk_RawAlloc (hunk, size);
memset (mem, 0, size);
return mem;
} }
VISIBLE size_t VISIBLE size_t