antirez/sds
FreeSimple Dynamic Strings library for C
About antirez/sds
SDS (Simple Dynamic Strings) is a C string library designed to augment the limited libc string handling by providing heap-allocated strings that are simpler to use, binary safe, and computationally more efficient. Unlike traditional C string libraries that use a structure with a buffer pointer, SDS stores metadata as a binary prefix before the actual string pointer returned to the user. This design allows SDS strings to be compatible with standard C string functions while offering dynamic resizing and built-in length tracking. Originally developed by Salvatore Sanfilippo (antirez) for Redis, SDS has been extracted as a standalone library and is used extensively in Redis, Disque, and Hiredis. Version 2 introduces a dynamic header size for reduced memory usage and additional API functions like sdscatfmt for faster formatted concatenation.
Key Features
Pros & Cons
- Simpler and safer than manual C string management
- Binary safe, allowing arbitrary data storage
- Works seamlessly with existing C string functions
- Proven reliability through extensive use in Redis
- Memory efficient due to dynamic header sizing in V2
- Includes both high-level convenience and low-level performance APIs
- Version 2 is not binary compatible with version 1
- May be slower with certain workloads compared to V1 (offset by lower memory usage)
- Many API functions return a new string pointer, requiring reassignment (e.g., s = sdscat(s, ...))
- Not an AI tool; strictly a C programming library