mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 16:26:39 +01:00
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:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user