Most operations in StringZilla don’t assume any memory ownership.
But in addition to the read-only search-like operations StringZilla provides a minimalistic C and C++ implementations for a memory owning string “class”.
Like other efficient string implementations, it uses the Small String Optimization (SSO) to avoid heap allocations for short strings.
The owning sz_string_t container, its union layout, and the full lifecycle API are documented under String Collections and Containers; this section covers the design rationale behind that layout.
A short string can be kept on the stack, if it fits within the inline character array.
Before 2015 GCC string implementation was just 8 bytes, and could only fit 7 characters.
Different STL implementations today have different thresholds for the Small String Optimization.
Similar to GCC, StringZilla is 32 bytes in size, and similar to Clang it can fit 22 characters on stack.
Our layout might be preferential, if you want to avoid branches.
If you use a different compiler, you may want to check its SSO buffer size with a simple Gist.
libstdc++ in GCC 13
libc++ in Clang 17
StringZilla
String sizeof
32
24
32
Inner Capacity
15
22
22
This design has been since ported to many high-level programming languages.
Swift, for example, can store 15 bytes in the String instance itself.
StringZilla implements SSO at the C level, providing the sz_string_t union and a simple API for primary operations, covered with a worked example under String Collections and Containers.
Unlike the conventional C strings, the sz_string_t is allowed to contain null characters.
To safely print those, pass the unpacked length to printf as well.
This structure passes the memory allocator to those functions.
Every function takes the caller's stream, which on a GPU names the device a block is made on and the work a release waits behind; host allocators ignore it, and on the CPU it is null. The optional reallocate keeps the first min(old_bytes, new_bytes) bytes, resizing the block where it lies when it can and moving it otherwise, and leaves the block as it was on failure. Without it, a block that must change size moves through allocate and free.
True if the allocators are the same, false otherwise.
sz_allocator_init_unified_bestInitializes an allocator whose blocks both the host and the device of capabilities address: the host heap on the CPU, managed memory on CUDA and ROCm, shared buffers on Metal.
The allocator is stateless, its handle null, and each call allocates on the device of the stream it is given, so one allocator serves every device of its vendor.