From 26c943c548b976be591bbcea1615aaafc5be38bb Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Wed, 13 Mar 2019 07:28:23 +0000 Subject: [PATCH 1/3] Optimize memory allocations --- containers/aligned_vector.c | 15 ++++++++++++++- containers/aligned_vector.h | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/containers/aligned_vector.c b/containers/aligned_vector.c index 011d0df..8fda257 100644 --- a/containers/aligned_vector.c +++ b/containers/aligned_vector.c @@ -20,15 +20,28 @@ void aligned_vector_init(AlignedVector* vector, unsigned int element_size) { vector->data = NULL; /* Reserve some initial capacity */ - aligned_vector_reserve(vector, ALIGNED_VECTOR_INITIAL_CAPACITY); + aligned_vector_reserve(vector, ALIGNED_VECTOR_CHUNK_SIZE); } + +static inline unsigned int round_to_chunk_size(unsigned int val) { + const unsigned int n = val; + const unsigned int m = ALIGNED_VECTOR_CHUNK_SIZE; + + return ((n + m - 1) / m) * m; +} + + void aligned_vector_reserve(AlignedVector* vector, unsigned int element_count) { if(element_count <= vector->capacity) { return; } unsigned int original_byte_size = vector->size * vector->element_size; + + /* We overallocate so that we don't make small allocations during push backs */ + element_count = round_to_chunk_size(element_count); + unsigned int new_byte_size = element_count * vector->element_size; unsigned char* original_data = vector->data; vector->data = (unsigned char*) memalign(0x20, new_byte_size); diff --git a/containers/aligned_vector.h b/containers/aligned_vector.h index 4318ffb..a002ece 100644 --- a/containers/aligned_vector.h +++ b/containers/aligned_vector.h @@ -12,7 +12,7 @@ typedef struct { unsigned int element_size; } AlignedVector; -#define ALIGNED_VECTOR_INITIAL_CAPACITY 256u +#define ALIGNED_VECTOR_CHUNK_SIZE 256u void aligned_vector_init(AlignedVector* vector, unsigned int element_size); void aligned_vector_reserve(AlignedVector* vector, unsigned int element_count); From 3d8fa7027b50c153ac90927255fce8c1d6dbca8f Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Wed, 13 Mar 2019 08:51:05 +0000 Subject: [PATCH 2/3] Properly forward the internal_palette_format --- GL/flush.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GL/flush.c b/GL/flush.c index e5e5274..fbb26d4 100644 --- a/GL/flush.c +++ b/GL/flush.c @@ -94,7 +94,7 @@ void APIENTRY glKosInitEx(GLdcConfig* config) { _glInitImmediateMode(); _glInitFramebuffers(); - _glSetInternalPaletteFormat(GL_RGBA4); + _glSetInternalPaletteFormat(config->internal_palette_format); _glInitTextures(); From 5da1ed732a0fbeaada49f09a822794c139463d1d Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Wed, 13 Mar 2019 08:53:35 +0000 Subject: [PATCH 3/3] Bump the default vbuf capacity --- GL/flush.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GL/flush.c b/GL/flush.c index fbb26d4..1fa552c 100644 --- a/GL/flush.c +++ b/GL/flush.c @@ -78,7 +78,7 @@ void APIENTRY glFinish() { void APIENTRY glKosInitConfig(GLdcConfig* config) { config->autosort_enabled = GL_FALSE; - config->initial_vbuf_capacity = 256; + config->initial_vbuf_capacity = 1024; config->internal_palette_format = GL_RGBA4; }