FFstrbuf: improve performance of copying static strings

This commit is contained in:
Carter Li 2025-01-03 10:59:01 +08:00
parent 60ff89c794
commit 3dc51d5f88
3 changed files with 60 additions and 9 deletions

View File

@ -225,12 +225,6 @@ void ffStrbufSetNS(FFstrbuf* strbuf, uint32_t length, const char* value)
ffStrbufAppendNS(strbuf, length, value);
}
void ffStrbufSet(FFstrbuf* strbuf, const FFstrbuf* value)
{
ffStrbufClear(strbuf);
ffStrbufAppendNS(strbuf, value->length, value->chars);
}
void ffStrbufTrimLeft(FFstrbuf* strbuf, char c)
{
if(strbuf->length == 0)

View File

@ -49,7 +49,6 @@ void ffStrbufPrependC(FFstrbuf* strbuf, char c);
void ffStrbufInsertNC(FFstrbuf* strbuf, uint32_t index, uint32_t num, char c);
void ffStrbufSetNS(FFstrbuf* strbuf, uint32_t length, const char* value);
void ffStrbufSet(FFstrbuf* strbuf, const FFstrbuf* value);
FF_C_PRINTF(2, 3) void ffStrbufSetF(FFstrbuf* strbuf, const char* format, ...);
void ffStrbufTrimLeft(FFstrbuf* strbuf, char c);
@ -102,8 +101,13 @@ FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateA(uint32_t allocate)
static inline void ffStrbufInitCopy(FFstrbuf* __restrict strbuf, const FFstrbuf* __restrict src)
{
ffStrbufInitA(strbuf, src->allocated);
ffStrbufAppend(strbuf, src);
if (src->allocated == 0) // static string
memcpy(strbuf, src, sizeof(FFstrbuf));
else
{
ffStrbufInitA(strbuf, src->allocated);
ffStrbufAppend(strbuf, src);
}
}
FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateCopy(const FFstrbuf* src)
@ -209,6 +213,17 @@ static inline void ffStrbufSetS(FFstrbuf* strbuf, const char* value)
ffStrbufAppendNS(strbuf, (uint32_t) strlen(value), value);
}
static inline void ffStrbufSet(FFstrbuf* strbuf, const FFstrbuf* value)
{
assert(value && value != strbuf);
if (strbuf->allocated == 0 && value->allocated == 0)
{
memcpy(strbuf, value, sizeof(FFstrbuf));
return;
}
ffStrbufSetNS(strbuf, value->length, value->chars);
}
static inline void ffStrbufInit(FFstrbuf* strbuf)
{
extern char* CHAR_NULL_PTR;

View File

@ -593,6 +593,48 @@ int main(void)
VERIFY(ffStrbufRemoveDupWhitespaces(&strbuf) == false);
VERIFY(strcmp(strbuf.chars, " ") == 0);
{
ffStrbufSetStatic(&strbuf, "abcdef");
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateCopy(&strbuf);
VERIFY(newStr.allocated == 0);
VERIFY(newStr.chars == strbuf.chars);
}
{
ffStrbufSetStatic(&strbuf, "abcdef");
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateS("123456");
ffStrbufSet(&newStr, &strbuf);
VERIFY(newStr.allocated > 0);
VERIFY(newStr.chars != strbuf.chars);
VERIFY(ffStrbufEqualS(&newStr, "abcdef"));
}
{
ffStrbufSetStatic(&strbuf, "abcdefghijkl");
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateS("123456");
ffStrbufSet(&newStr, &strbuf);
VERIFY(newStr.allocated > 0);
VERIFY(newStr.chars != strbuf.chars);
VERIFY(ffStrbufEqualS(&newStr, "abcdefghijkl"));
}
{
ffStrbufClear(&strbuf);
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateCopy(&strbuf);
VERIFY(newStr.allocated == 0);
VERIFY(newStr.chars == strbuf.chars);
VERIFY(newStr.chars[0] == '\0');
}
{
ffStrbufClear(&strbuf);
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateS("123456");
ffStrbufSet(&newStr, &strbuf);
VERIFY(newStr.allocated > 0);
VERIFY(newStr.chars != strbuf.chars);
VERIFY(ffStrbufEqualS(&newStr, ""));
}
//Success
puts("\e[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
}