All Projects → rmind → Thmap

rmind / Thmap

Licence: other
Concurrent trie-hash map library

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Thmap

Rhashmap
Robin Hood hash map library
Stars: ✭ 33 (-35.29%)
Mutual labels:  algorithm, hashing, library
Ringbuf
Lock-free ring buffer (MPSC)
Stars: ✭ 227 (+345.1%)
Mutual labels:  algorithm, lock-free, library
Towel
Throw in the towel.
Stars: ✭ 333 (+552.94%)
Mutual labels:  algorithm, library
Delaunay Triangulation
C++ version the delaunay triangulation
Stars: ✭ 339 (+564.71%)
Mutual labels:  algorithm, library
Solid
🎯 A comprehensive gradient-free optimization framework written in Python
Stars: ✭ 546 (+970.59%)
Mutual labels:  algorithm, library
Bild
Image processing algorithms in pure Go
Stars: ✭ 3,431 (+6627.45%)
Mutual labels:  algorithm, concurrency
Spscqueue
A bounded single-producer single-consumer wait-free and lock-free queue written in C++11
Stars: ✭ 307 (+501.96%)
Mutual labels:  concurrency, lock-free
React Native Blurhash
🖼️ A library to show colorful blurry placeholders while your content loads.
Stars: ✭ 430 (+743.14%)
Mutual labels:  algorithm, library
optimistic lock coupling rs
🍋: A General Lock following paper "Optimistic Lock Coupling: A Scalable and Efficient General-Purpose Synchronization Method"
Stars: ✭ 21 (-58.82%)
Mutual labels:  concurrency, lock-free
Bloc
A predictable state management library that helps implement the BLoC design pattern
Stars: ✭ 8,214 (+16005.88%)
Mutual labels:  library, concurrency
Polysnap
A work in progress polygon operations library with integer snap-rounding
Stars: ✭ 14 (-72.55%)
Mutual labels:  algorithm, library
Concurrencyfreaks
Stars: ✭ 299 (+486.27%)
Mutual labels:  concurrency, lock-free
Object threadsafe
We make any object thread-safe and std::shared_mutex 10 times faster to achieve the speed of lock-free algorithms on >85% reads
Stars: ✭ 280 (+449.02%)
Mutual labels:  concurrency, lock-free
Klib
A standalone and lightweight C library
Stars: ✭ 3,442 (+6649.02%)
Mutual labels:  algorithm, library
Cuckoofilter
Substitute for bloom filter.
Stars: ✭ 270 (+429.41%)
Mutual labels:  algorithm, hashing
Crossbeam
Tools for concurrent programming in Rust
Stars: ✭ 4,180 (+8096.08%)
Mutual labels:  concurrency, lock-free
Wyhash Rs
wyhash fast portable non-cryptographic hashing algorithm and random number generator in Rust
Stars: ✭ 44 (-13.73%)
Mutual labels:  algorithm, hashing
Merkletreejs
🌱 Construct Merkle Trees and verify proofs in JavaScript.
Stars: ✭ 238 (+366.67%)
Mutual labels:  algorithm, hashing
concurrent-ll
concurrent linked list implementation
Stars: ✭ 66 (+29.41%)
Mutual labels:  concurrency, lock-free
Arabiccompetitiveprogramming
The repository contains the ENGLISH description files attached to the video series in my ARABIC algorithms channel.
Stars: ✭ 675 (+1223.53%)
Mutual labels:  algorithm, library

Concurrent trie-hash map

Build Status

Concurrent trie-hash map library -- a general purpose associative array, combining the elements of hashing and radix trie. Highlights:

  • Very competitive performance, with logarithmic time complexity on average.
  • Lookups are lock-free and inserts/deletes are using fine-grained locking.
  • Incremental growth of the data structure (no large resizing/rehashing).
  • Optional support for use with shared memory, e.g. memory-mapped file.

The implementation is written in C11 and distributed under the 2-clause BSD license.

NOTE: Delete operations (the key/data destruction) must be synchronised with the readers using some reclamation mechanism. You can use the Epoch-based Reclamation (EBR) library provided HERE.

References (some, but not all, key ideas are based on these papers):

API

  • thmap_t *thmap_create(uintptr_t baseptr, const thmap_ops_t *ops, unsigned flags)

    • Construct a new trie-hash map. The optional ops parameter can used to set the custom allocate/free operations (see the description of thmap_ops_t below). In such case, the baseptr is the base (start) address of the address space mapping (it must be word-aligned). If ops is set to NULL, then malloc(3) and free(3) will be used as the default operations and baseptr should be set to zero. Currently, the supported flags are:
      • THMAP_NOCOPY: the keys on insert will not be copied and the given pointers to them will be expected to be valid and the values constant until the key is deleted; by default, the put operation will make a copy of the key.
      • THMAP_SETROOT: indicate that the root of the map will be manually set using the thmap_setroot routine; by default, the map is initialised and the root node is set on thmap_create.
  • void thmap_destroy(thmap_t *hmap)

    • Destroy the map, freeing the memory it uses.
  • void *thmap_get(thmap_t *hmap, const void *key, size_t len)

    • Lookup the key (of a given length) and return the value associated with it. Return NULL if the key is not found (see the caveats section).
  • void *thmap_put(thmap_t *hmap, const void *key, size_t len, void *val)

    • Insert the key with an arbitrary value. If the key is already present, return the already existing associated value without changing it. Otherwise, on a successful insert, return the given value. Just compare the result against val to test whether the insert was successful.
  • void *thmap_del(thmap_t *hmap, const void *key, size_t len)

    • Remove the given key. If the key was present, return the associated value; otherwise return NULL. The memory associated with the entry is not released immediately, because in the concurrent environment (e.g. multi-threaded application) the caller may need to ensure it is safe to do so. It is managed using the thmap_stage_gc and thmap_gc routines.
  • void *thmap_stage_gc(thmap_t *hmap)

    • Stage the currently pending entries (the memory not yet released after the deletion) for reclamation (G/C). This operation should be called before the synchronisation barrier.
    • Returns a reference which must be passed to thmap_gc. Not calling the G/C function for the returned reference would result in a memory leak.
  • void thmap_gc(thmap_t *hmap, void *ref)

    • Reclaim (G/C) the staged entries i.e. release any memory associated with the deleted keys. The reference must be the value returned by the call to thmap_stage_gc.
    • This function must be called after the synchronisation barrier which guarantees that there are no active readers referencing the staged entries.

If the map is created using the THMAP_SETROOT flag, then the following functions are applicable:

  • void thmap_setroot(thmap_t *thmap, uintptr_t root_offset)

    • Set the root node. The address must be relative to the base address, as if allocated by the thmap_ops_t::alloc routine. Return 0 on success and -1 on failure (if already set).
  • uintptr_t thmap_getroot(const thmap_t *thmap)

    • Get the root node address. The returned address will be relative to the base address.

The thmap_ops_t structure has the following members:

  • uintptr_t (*alloc)(size_t len)
    • Function to allocate the memory. Must return an address to the memory area of the size len. The address must be relative to the base address specified during map creation and must be word-aligned.
  • void (*free)(uintptr_t addr, size_t len)
    • Function to release the memory. Must take a previously allocated address (relative to the base) and release the memory area. The len is guaranteed to match the original allocation length.

Notes

Internally, offsets from the base pointer are used to organise the access to the data structure. This allows user to store the data structure in the shared memory, using the allocation/free functions. The keys will also be copied using the custom functions; if THMAP_NOCOPY is set, then the keys must belong to the same shared memory object.

The implementation was extensively tested on a 24-core x86 machine, see the stress test for the details on the technique.

Caveats

  • The implementation uses pointer tagging and atomic operations. This requires the base address and the allocations to provide at least word alignment.

  • While the NULL values may be inserted, thmap_get and thmap_del cannot indicate whether the key was not found or a key with a NULL value was found. If the caller needs to indicate an "empty" value, it can use a special pointer value, such as (void *)(uintptr_t)0x1.

Performance

The library has been benchmarked using different key profiles (8 to 256 bytes), set sizes (hundreds, thousands, millions) and ratio between readers and writers (from 60:40 to 90:10). In all cases it demonstrated nearly linear scalability (up to the number of cores). Here is an example result when matched with the C++ libcuckoo library:

Disclaimer: benchmark results, however, depend on many aspects (workload, hardware characteristics, methodology, etc). Ultimately, readers are encouraged to perform their own benchmarks.

Example

Simple case backed by malloc(3), which could be used in multi-threaded environment:

thmap_t *kvmap;
struct obj *obj;

kvmap = thmap_create(0, NULL);
assert(kvmap != NULL);
...
obj = obj_create();
thmap_put(kvmap, "test", sizeof("test") - 1, obj);
...
obj = thmap_get(kvmap, "test", sizeof("test") - 1);
...
thmap_destroy(kvmap);

Packages

Just build the package, install it and link the library using the -lthmap flag.

  • RPM (tested on RHEL/CentOS 7): cd pkg && make rpm
  • DEB (tested on Debian 9): cd pkg && make deb
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].