All Projects → danielkrupinski → StringPool

danielkrupinski / StringPool

Licence: MIT License
A performant and memory efficient storage for immutable strings with C++17. Supports all standard char types: char, wchar_t, char16_t, char32_t and C++20's char8_t.

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to StringPool

utf utils
My work on high-speed conversion of UTF-8 to UTF-32/UTF-16
Stars: ✭ 45 (+136.84%)
Mutual labels:  utf-8, utf-16, utf-32
Lingo
Text encoding for modern C++
Stars: ✭ 28 (+47.37%)
Mutual labels:  utf-8, utf-16, utf-32
Cracking The Coding Interview
Solutions for Cracking the Coding Interview - 6th Edition
Stars: ✭ 35 (+84.21%)
Mutual labels:  string, strings
Golang Combinations
Golang library which provide an algorithm to generate all combinations out of a given string array.
Stars: ✭ 51 (+168.42%)
Mutual labels:  string, strings
Util
A collection of useful utility functions
Stars: ✭ 201 (+957.89%)
Mutual labels:  string, strings
Tiny Utf8
Unicode (UTF-8) capable std::string
Stars: ✭ 322 (+1594.74%)
Mutual labels:  string, utf-8
Portable Utf8
🉑 Portable UTF-8 library - performance optimized (unicode) string functions for php.
Stars: ✭ 405 (+2031.58%)
Mutual labels:  string, utf-8
Str
A fast, solid and strong typed string manipulation library with multibyte support
Stars: ✭ 199 (+947.37%)
Mutual labels:  string, utf-8
Stringz
💯 Super fast unicode-aware string manipulation Javascript library
Stars: ✭ 181 (+852.63%)
Mutual labels:  strings, utf-8
BackportCpp
Library of backported modern C++ types to work with C++11
Stars: ✭ 53 (+178.95%)
Mutual labels:  cpp20, string-view
tplv
👣 Nano string template library for modern, based on ES6 template string syntax.
Stars: ✭ 31 (+63.16%)
Mutual labels:  string, strings
nim-ustring
utf-8 string for Nim
Stars: ✭ 12 (-36.84%)
Mutual labels:  string, utf-8
indexed-string-variation
Experimental JavaScript module to generate all possible variations of strings over an alphabet using an n-ary virtual tree
Stars: ✭ 16 (-15.79%)
Mutual labels:  string, strings
Mightystring
Making Ruby Strings Powerful
Stars: ✭ 28 (+47.37%)
Mutual labels:  string, strings
Stringy
A PHP string manipulation library with multibyte support
Stars: ✭ 2,461 (+12852.63%)
Mutual labels:  strings, utf-8
Voca rs
Voca_rs is the ultimate Rust string library inspired by Voca.js, string.py and Inflector, implemented as independent functions and on Foreign Types (String and str).
Stars: ✭ 167 (+778.95%)
Mutual labels:  string, utf-8
Stringsimilarity.net
A .NET port of java-string-similarity
Stars: ✭ 242 (+1173.68%)
Mutual labels:  string, strings
Harbol
Harbol is a collection of data structure and miscellaneous libraries, similar in nature to C++'s Boost, STL, and GNOME's GLib
Stars: ✭ 18 (-5.26%)
Mutual labels:  string, memory-pool
string theory
Flexible modern C++ string library with type-safe formatting
Stars: ✭ 32 (+68.42%)
Mutual labels:  strings, utf-8
stringext
Extra string functions for OCaml
Stars: ✭ 20 (+5.26%)
Mutual labels:  string

StringPool Linux macOS Windows

A performant and memory efficient storage for immutable strings with C++17. Supports all standard char types: char, wchar_t, char16_t, char32_t and C++20's char8_t.

Motivation

Standard C++ string classes - std::string, std::wstring etc. - aren't very efficient when it comes to memory usage and allocations. Due to small string optimization a lot of space can be wasted when storing huge amounts of long strings, that don't fit the capacity of the small string buffer. A common allocation strategy that std::basic_string uses (doubling the capacity when extending the storage) can lead to almost 50% waste of memory unless std::basic_string::shrink_to_fit() is called.

StringPool was created to provide a way of storing strings that don't change throughout program execution without excessive memory usage. Furthermore, it combats memory fragmentation by storing strings together, in blocks.

StringPool doesn't do string interning

StringPool doesn't perform any string comparisons, neither it differentiates between two strings - each call to StringPool<>::add() gives you a brand new view of the string.

Use cases

  • localization (translation) strings
  • storing filenames and paths of files packed in a VPK

Getting started

  1. Include StringPool.h in your project.
  2. Create a pool object:
StringPool<char> pool;
  1. Add some strings: (don't forget to save values returned from StringPool<>::add())
std::vector<std::string_view> strings;
strings.push_back(pool.add("foo"));
strings.push_back(pool.add("bar"));

Example usage

#include <cassert>
#include <cstring>
#include <string_view>
#include <vector>

#include "StringPool.h"

int main()
{
    // StringPool of null-terminated strings
    {
        StringPool<char, true> pool;

        std::vector<std::string_view> strings;
        strings.push_back(pool.add("one"));

        // string views passed to StringPool<>::add() don't have to point to a null-terminated string
        constexpr std::string_view s{ "two three" };
        strings.push_back(pool.add(s.substr(0, 3)));
        strings.push_back(pool.add(s.substr(4)));

        // strings added to a null-terminated pool can be used with C API
        assert(std::strcmp(strings[0].data(), "one") == 0);
        assert(std::strcmp(strings[1].data(), "two") == 0);
        assert(std::strcmp(strings[2].data(), "three") == 0);

        // string_view can be skipped, and a raw const char* can be used
        const char* str = pool.add("just a pointer").data();
        assert(std::strcmp(str, "just a pointer") == 0);
    }

    // StringPool of not null-terminated strings, uses less memory (1 byte per string) by dropping C compatibility
    {
        StringPool<char, false> pool;

        std::vector<std::string_view> strings;
        strings.push_back(pool.add("one"));

        // string views passed to StringPool<>::add() don't have to point to a null-terminated string
        constexpr std::string_view s{ "two three" };
        strings.push_back(pool.add(s.substr(0, 3)));
        strings.push_back(pool.add(s.substr(4)));

        // strings added to this pool can't be passed to C functions
        assert(strings[0] == "one");
        assert(strings[1] == "two");
        assert(strings[2] == "three");
    }

    // StringPool uses default block capacity of 8192 characters, but a custom value can be specified
    {
        constexpr auto myCustomCapacity = 1'000'000;
        StringPool<wchar_t, false> bigPool{ myCustomCapacity };

        const auto something = bigPool.add(L"something");
        StringPool<wchar_t, false> tooSmallPool{ something.length() / 2 };

        // if you try to add a string exceeding default block capacity, StringPool will allocate a new block capable of storing the string
        const auto stillAdded = tooSmallPool.add(something);
        assert(stillAdded == L"something");
    }
}

C++20

StringPool supports char8_t type introduced in C++20 standard out of the box.

Thread safety

  • To add strings to a pool (StringPool<>::add()) from multiple threads you have to provide synchronization yourself.
  • Once added to pool, strings are read-only therefore can be safely accessed from multiple threads. That means you can add new strings to the pool and access existing ones in parallel.

For an example use of StringPool accross multiple threads check Examples/Threaded.cpp.

Exceptions

StringPool is exception-neutral meaning that while it doesn't throw any exception itself, exceptions may be emitted by STL algorithms or containers used in the implementation (std::bad_alloc etc.).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].