Giter Club home page Giter Club logo

libstringintern's Introduction

Build Status Coverage Status License

libstringintern

A thread safe lock free C++ library for interning strings to save heap space. Smaller heaps generally mean faster applications due to cache locality and less swapping to disk. The other upside is you can store more stuff in RAM.

Use case

You have an in-memory database with 100 million customer records with associated address details. You therefore have 100 million state and country strings associated with those records. That will use a lot of memory.

When strings are interned you only ever have one instance of the string plus a reference instance per record which is 4 bytes in size.

Let's do the math(s)

  • 32bit: 100m * 'Mississippi\0' = 1144MB, 100m * 4B = 381MB, total = 1525MB
  • 64bit: 100m * 'Mississippi\0' = 1144MB, 100m * 8B = 762MB, total = 1907MB
  • Interned: 1 * 'Mississippi\0' = 12B, 100m * 4B = 381MB

This example is for a fairly short string, but the saving is still valuable. Larger strings have an even bigger pay-off.

How it works

  • Interned strings are stored in pages, grouped by string size, a bit like tcmalloc
  • Pages are 2MB in size to take advantage of THP
  • Each string is hashed using XX64 with the low order bytes used as an index into the page
  • If the index refers to a location in the page that is free the string and hash are committed to the page and the caller gets a reference to the string
  • If the index is already taken then we compare the hashes and if they match we return a reference to the entry
  • If the hashes did not match then allocate a new page and add the string and hash returning the string reference
  • The host application turns the reference into a real pointer to use the string, a bit like std::weak_ptr
  • References are 32bit values made up from PAGE(16):INDEX(16). On 64bit systems these use half the storage space of a pointer.
  • Interned strings are immutable

Code example

  StringIntern intern;
  auto ref1 = intern.Add("Mississippi");
  std::cout << intern.ToString(ref1) << std::endl;
  
  auto ref2 = intern.Add("Mississippi");
  std::cout << ref1 == ref2 ? "Match" : "Doesn't match" << std::endl;

Testing

We have tested extensively on Intel, PPC and ARMv7. If you have another system you'd like us to test with please let us know.

libstringintern's People

Contributors

craigminihan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.