Class DenseMapObj#
Defined in File map.h
Nested Relationships#
Nested Types#
Inheritance Relationships#
Base Type#
public tvm::ffi::MapObj
(Class MapObj)
Class Documentation#
-
class DenseMapObj : public tvm::ffi::MapObj#
A specialization of hash map that implements the idea of array-based hash map. Another reference implementation can be found [1].
A. Overview
DenseMapObj did several improvements over traditional separate chaining hash, in terms of cache locality, memory footprints and data organization.
A1. Implicit linked list. For better cache locality, instead of using linked list explicitly for each bucket, we store list data into a single array that spans contiguously in memory, and then carefully design access patterns to make sure most of them fall into a single cache line.
A2. 1-byte metadata. There is only 1 byte overhead for each slot in the array to indexing and traversal. This can be divided in 3 parts. 1) Reserved code: (0b11111111)_2 indicates a slot is empty; (0b11111110)_2 indicates protected, which means the slot is empty but not allowed to be written. 2) If not empty or protected, the highest bit is used to indicate whether data in the slot is head of a linked list. 3) The rest 7 bits are used as the “next pointer” (i.e. pointer to the next element). On 64-bit architecture, an ordinary pointer can take up to 8 bytes, which is not acceptable overhead when dealing with 16-byte ObjectRef pairs. Based on a commonly noticed fact that the lists are relatively short (length <= 3) in hash maps, we follow [1]’s idea that only allows the pointer to be one of the 126 possible values, i.e. if the next element of i-th slot is (i + x)-th element, then x must be one of the 126 pre-defined values.
A3. Data blocking. We organize the array in the way that every 16 elements forms a data block. The 16-byte metadata of those 16 elements are stored together, followed by the real data, i.e. 16 key-value pairs.
B. Implementation details
B1. Power-of-2 table size and Fibonacci Hashing. We use power-of-two as table size to avoid modulo for more efficient arithmetics. To make the hash-to-slot mapping distribute more evenly, we use the Fibonacci Hashing [2] trick.
B2. Traverse a linked list in the array. 1) List head. Assume Fibonacci Hashing maps a given key to slot i, if metadata at slot i indicates that it is list head, then we found the head; otherwise the list is empty. No probing is done in this procedure. 2) Next element. To find the next element of a non-empty slot i, we look at the last 7 bits of the metadata at slot i. If they are all zeros, then it is the end of list; otherwise, we know that the next element is (i + candidates[the-last-7-bits]).
B3. InsertMaybeReHash an element. Following B2, we first traverse the linked list to see if this element is in the linked list, and if not, we put it at the end by probing the next empty position in one of the 126 candidate positions. If the linked list does not even exist, but the slot for list head has been occupied by another linked list, we should find this intruder another place.
B4. Quadratic probing with triangle numbers. In open address hashing, it is provable that probing with triangle numbers can traverse power-of-2-sized table [3]. In our algorithm, we follow the suggestion in [1] that also use triangle numbers for “next pointer” as well as sparing for list head.
[1] skarupke/flat_hash_map [2] https://programmingpraxis.com/2018/06/19/fibonacci-hash/ [3] https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/
Public Functions
-
inline uint64_t NumSlots() const#
Return the number of usable slots for Dense layout (MSB clear => identity).
- Returns:
The number of usable slots
-
inline ~DenseMapObj()#
Destroy the DenseMapObj.
-
inline size_t count(const key_type &key) const#
- Returns:
The number of elements of the key
-
inline const mapped_type &at(const key_type &key) const#
Index value associated with a key, throw exception if the key does not exist.
- Parameters:
key – The indexing key
- Returns:
The const reference to the value
-
inline mapped_type &at(const key_type &key)#
Index value associated with a key, throw exception if the key does not exist.
- Parameters:
key – The indexing key
- Returns:
The mutable reference to the value
-
inline iterator find(const key_type &key) const#
Index value associated with a key.
- Parameters:
key – The indexing key
- Returns:
The iterator of the entry associated with the key, end iterator if not exists
-
inline void erase(const iterator &position)#
Erase the entry associated with the iterator.
- Parameters:
position – The iterator
-
inline iterator begin() const#
- Returns:
begin iterator
-
inline iterator end() const#
- Returns:
end iterator
Protected Attributes
-
uint32_t fib_shift_#
fib shift in Fibonacci Hashing
-
uint64_t iter_list_head_ = kInvalidIndex#
the head of iterator list
-
uint64_t iter_list_tail_ = kInvalidIndex#
the tail of iterator list
Protected Static Functions
-
static inline uint64_t NextProbeLocation(size_t index)#
Friends
- friend class MapObj
-
inline uint64_t NumSlots() const#