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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

size of string type

polijan opened this issue · comments

The README says cstr is the size of a pointer.
I just wanted to check:

  • I don't think that's anymore the case with the SSO optimisation.
  • But I assume cstr type is still meant to stay small and thus I should continue to pass strings by values in my user code, is that correct?

Hi! The size is now 24 bytes (c++ std::string is 32 bytes in most implementations, but has only 15 chars SS repr. vs 23 in cstr). So, yes 24 bytes are still very fast to copy and passed-by-value variables are easier to optimize for the compiler.

That said, I don't pass cstr as parameters at all. I either use const char* or csview, and use cstr for storing strings only. If you have a func. with cstr args, users must construct a cstr to call the func if they only have a const char* or csview. That means they must also destruct it after the call (not needed for cstr with len < 24, but don't let code rely on that). This is the same problem in C++ , but is not obvious because strings are automatically constructed and destructed during the call.
Add: for internal API's, when you know cstr are available, you SHOULD pass cstr (either by value or ptr - does not matter)!

thanks.