optimize(IO): reduce memory usage during reading data from files to FFstrbufs

Avoid unnecessary calls of ffStrbufEnsureFree and only allocate
necessary memory.

In most cases, this will save 30% ~ 50% of memory used by ffAppendFDBuffer.
This commit is contained in:
apocelipes
2023-12-03 10:52:39 +09:00
committed by Carter Li
parent 2979bfe4ca
commit 4868517d53
5 changed files with 113 additions and 5 deletions
+24
View File
@@ -54,6 +54,30 @@ void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free)
strbuf->allocated = allocate;
}
void ffStrbufEnsureFixedLengthFree(FFstrbuf* strbuf, uint32_t free)
{
uint32_t oldFree = ffStrbufGetFree(strbuf);
if (oldFree >= free && !(strbuf->allocated == 0 && strbuf->length > 0))
return;
uint32_t newCap = strbuf->allocated + (free - oldFree);
if(strbuf->allocated == 0)
{
newCap += strbuf->length + 1; // +1 for the NUL
char* newbuf = malloc(sizeof(*strbuf->chars) * newCap);
if(strbuf->length == 0)
*newbuf = '\0';
else
memcpy(newbuf, strbuf->chars, strbuf->length + 1);
strbuf->chars = newbuf;
}
else
strbuf->chars = realloc(strbuf->chars, sizeof(*strbuf->chars) * newCap);
strbuf->allocated = newCap;
}
void ffStrbufClear(FFstrbuf* strbuf)
{
assert(strbuf != NULL);
+1
View File
@@ -33,6 +33,7 @@ void ffStrbufInitA(FFstrbuf* strbuf, uint32_t allocate);
void ffStrbufInitVF(FFstrbuf* strbuf, const char* format, va_list arguments);
void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free);
void ffStrbufEnsureFixedLengthFree(FFstrbuf* strbuf, uint32_t free);
void ffStrbufClear(FFstrbuf* strbuf);