Skip to content

Commit 3dc51d5

Browse files
committed
FFstrbuf: improve performance of copying static strings
1 parent 60ff89c commit 3dc51d5

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

src/util/FFstrbuf.c

-6
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,6 @@ void ffStrbufSetNS(FFstrbuf* strbuf, uint32_t length, const char* value)
225225
ffStrbufAppendNS(strbuf, length, value);
226226
}
227227

228-
void ffStrbufSet(FFstrbuf* strbuf, const FFstrbuf* value)
229-
{
230-
ffStrbufClear(strbuf);
231-
ffStrbufAppendNS(strbuf, value->length, value->chars);
232-
}
233-
234228
void ffStrbufTrimLeft(FFstrbuf* strbuf, char c)
235229
{
236230
if(strbuf->length == 0)

src/util/FFstrbuf.h

+18-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ void ffStrbufPrependC(FFstrbuf* strbuf, char c);
4949
void ffStrbufInsertNC(FFstrbuf* strbuf, uint32_t index, uint32_t num, char c);
5050

5151
void ffStrbufSetNS(FFstrbuf* strbuf, uint32_t length, const char* value);
52-
void ffStrbufSet(FFstrbuf* strbuf, const FFstrbuf* value);
5352
FF_C_PRINTF(2, 3) void ffStrbufSetF(FFstrbuf* strbuf, const char* format, ...);
5453

5554
void ffStrbufTrimLeft(FFstrbuf* strbuf, char c);
@@ -102,8 +101,13 @@ FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateA(uint32_t allocate)
102101

103102
static inline void ffStrbufInitCopy(FFstrbuf* __restrict strbuf, const FFstrbuf* __restrict src)
104103
{
105-
ffStrbufInitA(strbuf, src->allocated);
106-
ffStrbufAppend(strbuf, src);
104+
if (src->allocated == 0) // static string
105+
memcpy(strbuf, src, sizeof(FFstrbuf));
106+
else
107+
{
108+
ffStrbufInitA(strbuf, src->allocated);
109+
ffStrbufAppend(strbuf, src);
110+
}
107111
}
108112

109113
FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateCopy(const FFstrbuf* src)
@@ -209,6 +213,17 @@ static inline void ffStrbufSetS(FFstrbuf* strbuf, const char* value)
209213
ffStrbufAppendNS(strbuf, (uint32_t) strlen(value), value);
210214
}
211215

216+
static inline void ffStrbufSet(FFstrbuf* strbuf, const FFstrbuf* value)
217+
{
218+
assert(value && value != strbuf);
219+
if (strbuf->allocated == 0 && value->allocated == 0)
220+
{
221+
memcpy(strbuf, value, sizeof(FFstrbuf));
222+
return;
223+
}
224+
ffStrbufSetNS(strbuf, value->length, value->chars);
225+
}
226+
212227
static inline void ffStrbufInit(FFstrbuf* strbuf)
213228
{
214229
extern char* CHAR_NULL_PTR;

tests/strbuf.c

+42
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,48 @@ int main(void)
593593
VERIFY(ffStrbufRemoveDupWhitespaces(&strbuf) == false);
594594
VERIFY(strcmp(strbuf.chars, " ") == 0);
595595

596+
{
597+
ffStrbufSetStatic(&strbuf, "abcdef");
598+
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateCopy(&strbuf);
599+
VERIFY(newStr.allocated == 0);
600+
VERIFY(newStr.chars == strbuf.chars);
601+
}
602+
603+
{
604+
ffStrbufSetStatic(&strbuf, "abcdef");
605+
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateS("123456");
606+
ffStrbufSet(&newStr, &strbuf);
607+
VERIFY(newStr.allocated > 0);
608+
VERIFY(newStr.chars != strbuf.chars);
609+
VERIFY(ffStrbufEqualS(&newStr, "abcdef"));
610+
}
611+
612+
{
613+
ffStrbufSetStatic(&strbuf, "abcdefghijkl");
614+
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateS("123456");
615+
ffStrbufSet(&newStr, &strbuf);
616+
VERIFY(newStr.allocated > 0);
617+
VERIFY(newStr.chars != strbuf.chars);
618+
VERIFY(ffStrbufEqualS(&newStr, "abcdefghijkl"));
619+
}
620+
621+
{
622+
ffStrbufClear(&strbuf);
623+
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateCopy(&strbuf);
624+
VERIFY(newStr.allocated == 0);
625+
VERIFY(newStr.chars == strbuf.chars);
626+
VERIFY(newStr.chars[0] == '\0');
627+
}
628+
629+
{
630+
ffStrbufClear(&strbuf);
631+
FF_STRBUF_AUTO_DESTROY newStr = ffStrbufCreateS("123456");
632+
ffStrbufSet(&newStr, &strbuf);
633+
VERIFY(newStr.allocated > 0);
634+
VERIFY(newStr.chars != strbuf.chars);
635+
VERIFY(ffStrbufEqualS(&newStr, ""));
636+
}
637+
596638
//Success
597639
puts("\e[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
598640
}

0 commit comments

Comments
 (0)