antirez /sds

Simple Dynamic Strings library for C

★ 5,593⑂ 512👁 133⑃ 110● 44▤ 0
CBSD-2-ClauseCreated 2014-02-06
+1
日增长
5,593
stars
512
forks
133
watchers
110
prs
44
issues
0
releases

README

Simple Dynamic Strings
===

Notes about version 2: this is an updated version of SDS in an attempt
to finally unify Redis, Disque, Hiredis, and the stand alone SDS versions.
This version is NOT binary compatible with SDS verison 1, but the API
is 99% compatible so switching to the new lib should be trivial.

Note that this version of SDS may be a slower with certain workloads, but
uses less memory compared to V1 since header size is dynamic and depends to
the string to alloc.

Moreover it includes a few more API functions, notably sdscatfmt which
is a faster version of sdscatprintf that can be used for the simpler
cases in order to avoid the libc printf family functions performance
penalty.

How SDS strings work
===

SDS is a string library for C designed to augment the limited libc string
handling functionalities by adding heap allocated strings that are:

 Simpler to use.
 Binary safe.
 Computationally more efficient.
 But yet... Compatible with normal C string functions.

This is achieved using an alternative design in which instead of using a C
structure to represent a string, we use a binary prefix that is stored
before the actual pointer to the string that is returned by SDS to the user.

    +--------+-------------------------------+-----------+
    | Header | Binary safe C alike string... | Null term |
    +--------+-------------------------------+-----------+
             |
             - Pointer returned to the user.

Because of meta data stored before the actual returned pointer as a prefix,
and because of every SDS string implicitly adding a null term at the end of
the string regardless of the actual content of the string, SDS strings work
well together with C strings and the user is free to use them interchangeably
with other std C string functions that access the string in read-only.

SDS was a C string I developed in the past for my everyday C programming needs,
later it was moved into Redis where it is used extensively and where it was
modified in order to be suitable for high performance operations. Now it was
extracted from Redis and forked as a stand alone project.

Because of its many years life inside Redis, SDS provides both higher level
functions for easy strings manipulation in C, but also a set of low level
functions that make it possible to write high performance code without paying
a penalty for using an higher level string library.

Advantages and disadvantages of SDS
===

Normally dynamic string libraries for C are implemented using a structure
that defines the string. The structure has a pointer field that is managed
by the string function, so it looks like this:

c
struct yourAverageStringLibrary {
    char buf;
    size_t len;
    ... possibly more fields here ...
};


SDS strings as already mentioned don't follow this schema, and are instead
a single allocation with a prefix that lives before the address actually
returned for the string.

There are advantages and disadvantages with this approach over the tradition

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON