[core] perf: buffer_copy_string_len()

buffer_copy_string_len() short-circuit common case,
but preserve blank-string initialization side-effect
if buffer is empty
personal/stbuehler/fix-fdevent
Glenn Strauss 4 years ago
parent 15277bf9e1
commit d4a93a531d
  1. 8
      src/buffer.c

@ -117,9 +117,11 @@ static void buffer_realloc(buffer *b, size_t size) {
char* buffer_string_prepare_copy(buffer *b, size_t size) {
force_assert(NULL != b);
force_assert(size + 1 > size);
buffer_alloc(b, size + 1);
if (size >= b->size) {
force_assert(size + 1 > size);
buffer_alloc(b, size + 1);
}
b->used = 1;
b->ptr[0] = '\0';
@ -186,7 +188,7 @@ void buffer_copy_string_len(buffer *b, const char *s, size_t s_len) {
force_assert(NULL != b);
force_assert(NULL != s || s_len == 0);
buffer_string_prepare_copy(b, s_len);
if (s_len >= b->size) buffer_string_prepare_copy(b, s_len);
if (0 != s_len) memcpy(b->ptr, s, s_len); /*(s might be NULL)*/
b->ptr[s_len] = '\0';

Loading…
Cancel
Save