Merge branch 'resize_coldpath' into 'master'

Move aligned_vector_reserve onto cold path since it should rarely be called

See merge request simulant/GLdc!132
This commit is contained in:
Luke Benstead 2024-10-05 06:36:38 +00:00
commit 5a36e93993
3 changed files with 58 additions and 41 deletions

View File

@ -14,7 +14,6 @@
extern inline void* aligned_vector_resize(AlignedVector* vector, const uint32_t element_count);
extern inline void* aligned_vector_extend(AlignedVector* vector, const uint32_t additional_count);
extern inline void* aligned_vector_reserve(AlignedVector* vector, uint32_t element_count);
extern inline void* aligned_vector_push_back(AlignedVector* vector, const void* objs, uint32_t count);
void aligned_vector_init(AlignedVector* vector, uint32_t element_size) {
@ -31,6 +30,31 @@ void aligned_vector_init(AlignedVector* vector, uint32_t element_size) {
(void) ptr;
}
void* aligned_vector_reserve(AlignedVector* vector, uint32_t element_count) {
AlignedVectorHeader* hdr = &vector->hdr;
if(element_count < hdr->capacity) {
return aligned_vector_at(vector, element_count);
}
uint32_t original_byte_size = (hdr->size * hdr->element_size);
/* We overallocate so that we don't make small allocations during push backs */
element_count = ROUND_TO_CHUNK_SIZE(element_count);
uint32_t new_byte_size = (element_count * hdr->element_size);
uint8_t* original_data = vector->data;
vector->data = (uint8_t*) memalign(0x20, new_byte_size);
assert(vector->data);
AV_MEMCPY4(vector->data, original_data, original_byte_size);
free(original_data);
hdr->capacity = element_count;
return vector->data + original_byte_size;
}
void aligned_vector_shrink_to_fit(AlignedVector* vector) {
AlignedVectorHeader* const hdr = &vector->hdr;
if(hdr->size == 0) {

View File

@ -27,6 +27,7 @@ static inline void* memalign(size_t alignment, size_t size) {
#define AV_INLINE_DEBUG AV_NO_INSTRUMENT __attribute__((always_inline))
#define AV_FORCE_INLINE static AV_INLINE_DEBUG
#endif
#define AV_NO_INLINE __attribute__((noinline))
#ifdef __DREAMCAST__
@ -86,37 +87,16 @@ typedef struct {
void aligned_vector_init(AlignedVector* vector, uint32_t element_size);
/* Resizes the backing array if necessary so that element_count elements can fit in the array */
/* (note that only capacity is potentially changed, size is never changed) */
AV_NO_INLINE void* aligned_vector_reserve(AlignedVector* vector, uint32_t element_count);
AV_FORCE_INLINE void* aligned_vector_at(const AlignedVector* vector, const uint32_t index) {
const AlignedVectorHeader* hdr = &vector->hdr;
assert(index < hdr->size);
return vector->data + (index * hdr->element_size);
}
AV_FORCE_INLINE void* aligned_vector_reserve(AlignedVector* vector, uint32_t element_count) {
AlignedVectorHeader* hdr = &vector->hdr;
if(element_count < hdr->capacity) {
return aligned_vector_at(vector, element_count);
}
uint32_t original_byte_size = (hdr->size * hdr->element_size);
/* We overallocate so that we don't make small allocations during push backs */
element_count = ROUND_TO_CHUNK_SIZE(element_count);
uint32_t new_byte_size = (element_count * hdr->element_size);
uint8_t* original_data = vector->data;
vector->data = (uint8_t*) memalign(0x20, new_byte_size);
assert(vector->data);
AV_MEMCPY4(vector->data, original_data, original_byte_size);
free(original_data);
hdr->capacity = element_count;
return vector->data + original_byte_size;
}
AV_FORCE_INLINE AlignedVectorHeader* aligned_vector_header(const AlignedVector* vector) {
return (AlignedVectorHeader*) &vector->hdr;
}

View File

@ -2,10 +2,11 @@
#include <math.h>
#include <stdlib.h>
#ifdef SDL2_BUILD
#ifndef __DREAMCAST__
#include <SDL2/SDL.h>
static SDL_Window* win_handle;
#else
#include <kos.h>
#include <GL/glkos.h>
#endif
@ -116,15 +117,36 @@ static void DrawTriFan(float y) {
glEnd();
}
int main(int argc, char *argv[]) {
#ifdef SDL2_BUILD
static void sample_init() {
#ifndef __DREAMCAST__
SDL_Init(SDL_INIT_EVERYTHING);
win_handle = SDL_CreateWindow("Shapes", 0, 0, 640, 480, SDL_WINDOW_OPENGL);
SDL_GL_CreateContext(win_handle);
#else
glKosInit();
#endif
}
static int sample_should_exit() {
#ifndef __DREAMCAST__
SDL_Event event;
while (SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) return 1;
}
return 0;
#else
maple_device_t *cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);
if (!cont) return 0;
cont_state_t *state = (cont_state_t *)maple_dev_status(cont);
if (!state) return 0;
return state->buttons & CONT_START;
#endif
}
int main(int argc, char *argv[]) {
sample_init();
glClearColor(0.5f, 0.5f, 0.5f, 1);
glViewport(0, 0, 640, 480);
@ -139,18 +161,9 @@ int main(int argc, char *argv[]) {
mat[3][3] = 1;
glLoadMatrixf(mat);
int running = 1;
while (running) {
while (!sample_should_exit()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#ifdef SDL2_BUILD
SDL_Event event;
while (SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) running = 0;
}
#endif
DrawTriStrip(300);
DrawTriFan(300);
DrawTriList(300);
@ -160,9 +173,9 @@ int main(int argc, char *argv[]) {
glPointSize(5);
DrawPoint(100);
glLineWidth(0.5f);
DrawLineLoop(100);
glLineWidth(2);
DrawLineLoop(100);
glLineWidth(4);
DrawLineStrip(100);
glLineWidth(10);
DrawLine(100);