antirez/sds logo

antirez/sds

Free

Simple Dynamic Strings library for C

FreeFree tier
Type
Open Source

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

Binary safe: handles strings with null bytes and arbitrary binary data
Compatible with standard C string functions (read-only usage)
Heap-allocated strings with automatic memory management
Dynamic header size in version 2 reduces memory usage
Fast concatenation via sdscatfmt, avoiding printf family overhead
Low-level functions for high-performance scenarios
Well-tested and used extensively in Redis, Disque, and Hiredis

Pros & Cons

Pros
  • 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
Cons
  • 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

Best For

General C programming needing safe string manipulationHigh-performance systems where dynamic strings are requiredEmbedded in Redis, Disque, and Hiredis for string handlingProjects that require a lightweight alternative to GLib or other string libraries

FAQ

What is SDS?
SDS (Simple Dynamic Strings) is a C library for heap-allocated, binary-safe strings that are compatible with standard C string functions.
How is SDS different from other C string libraries?
Instead of using a struct with a buffer pointer, SDS stores metadata as a prefix before the pointer returned to the user, allowing compatibility with C functions and dynamic resizing.
Where is SDS used?
SDS is used in Redis, Disque, Hiredis, and as a standalone library. It was originally developed by antirez for Redis.
Is SDS version 2 compatible with version 1?
No, version 2 is not binary compatible with version 1, but the API is 99% compatible, making switching trivial.