stclib / STC

A modern, user friendly, generic, type-safe and fast C99 container library: String, Vector, Sorted and Unordered Map and Set, Deque, Forward List, Smart Pointers, Bitset and Random numbers.
MIT License
1.35k stars 73 forks source link

a little less memory copy #58

Closed liigo closed 1 year ago

tylov commented 1 year ago

This is technically correct, but two reasons not to apply it:

  1. Current code emulates realloc() behaviour, i.e. retains all values in the buffer. This is useful for low level impl. Following would fail in transition from short to long string representation with this PR.
    cstr_buf b = cstr_buffer(&mystr);
    while (...) {
    if (b.len == b.cap) 
       b.str = cstr_reserve(&mystr, b.cap += b.cap);
    b.str[b.len++] input[i];
    }
    _cstr_set_size(b.len)
  2. There is no speed benefit. Copying fixed size 24 bytes results in a few mov instructions instead of a "full" memcpy() call. I will actually change to copy all 24 bytes (or 12) instead of only 23.

Btw, I am still in awe over your previous pull request! I had no idea it was possible to have the first switch case outside the block, which allowed this. Thanks again!!

liigo commented 1 year ago

Btw, I am still in awe over your previous pull request! I had no idea it was possible to have the first switch case outside the block, which allowed this. Thanks again!!

that's my honor, thank you!