The Design of a Scalable Hashtable George V. Reilly

Slides:



Advertisements
Similar presentations
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Advertisements

1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
External Memory Hashing. Model of Computation Data stored on disk(s) Minimum transfer unit: a page = b bytes or B records (or block) N records -> N/B.
CS4432: Database Systems II Hash Indexing 1. Hash-Based Indexes Adaptation of main memory hash tables Support equality searches No range searches 2.
File Organizations Sept. 2012Yangjun Chen ACS Outline: Hashing (5.9, 5.10, 3 rd. ed.; 13.8, 4 th, 5 th ed.; 17.8, 6 th ed.) external hashing static.
Hash-based Indexes CS 186, Spring 2006 Lecture 7 R &G Chapter 11 HASH, x. There is no definition for this word -- nobody knows what hash is. Ambrose Bierce,
CPSC 335 Dr. Marina Gavrilova Computer Science University of Calgary Canada.
CSCE 3400 Data Structures & Algorithm Analysis
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
DBMS 2001Notes 4.2: Hashing1 Principles of Database Management Systems 4.2: Hashing Techniques Pekka Kilpeläinen (after Stanford CS245 slide originals.
Theory I Algorithm Design and Analysis (5 Hashing) Prof. Th. Ottmann.
Copyright 2003Curt Hill Hash indexes Are they better or worse than a B+Tree?
Data Structures Using C++ 2E
Index tuning Hash Index. overview Introduction Hash-based indexes are best for equality selections. –Can efficiently support index nested joins –Cannot.
Hashing21 Hashing II: The leftovers. hashing22 Hash functions Choice of hash function can be important factor in reducing the likelihood of collisions.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
1 Hash Tables Gordon College CS Hash Tables Recall order of magnitude of searches –Linear search O(n) –Binary search O(log 2 n) –Balanced binary.
Lecture 10 Sept 29 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Hash Tables and Associative Containers CS-212 Dick Steflik.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #8.
FALL 2004CENG 3511 Hashing Reference: Chapters: 11,12.
Hash Tables1 Part E Hash Tables  
The Most Commonly-used Data Structures
Design and Analysis of Algorithms - Chapter 71 Hashing b A very efficient method for implementing a dictionary, i.e., a set with the operations: – insert.
HASH TABLES Malathi Mansanpally CS_257 ID-220. Agenda: Extensible Hash Tables Insertion Into Extensible Hash Tables Linear Hash Tables Insertion Into.
Lecture 10: Search Structures and Hashing
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
Hashing General idea: Get a large array
E.G.M. PetrakisHashing1 Hashing on the Disk  Keys are stored in “disk pages” (“buckets”)  several records fit within one page  Retrieval:  find address.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
COSC 2007 Data Structures II
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
CSC 427: Data Structures and Algorithm Analysis
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Lecture 17 April 11, 11 Chapter 5, Hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
David Luebke 1 11/26/2015 Hash Tables. David Luebke 2 11/26/2015 Hash Tables ● Motivation: Dictionaries ■ Set of key/value pairs ■ We care about search,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
File Organizations Jan. 2008Yangjun Chen ACS Outline: Hashing (5.9, 5.10, 3 rd. ed.; 13.8, 4 th ed.) external hashing static hashing & dynamic hashing.
The Hash Table Data Structure Mugurel Ionu Andreica Spring 2012.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
1 the hash table. hash table A hash table consists of two major components …
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 5 Record Storage and Primary File Organizations
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
DS.H.1 Hashing Chapter 5 Overview The General Idea Hash Functions Separate Chaining Open Addressing Rehashing Extendible Hashing Application Example: Geometric.
Fundamental Structures of Computer Science II
CSCE 3110 Data Structures & Algorithm Analysis
Hash Tables and Associative Containers
static hashing & dynamic hashing hash function
Database Design and Programming
Collision Handling Collisions occur when different elements are mapped to the same cell.
Presentation transcript:

The Design of a Scalable Hashtable George V. Reilly

 LKRhash invented at Microsoft in 1997  Paul (Per-Åke) Larson — Microsoft Research  Murali R. Krishnan — (then) Internet Information Server  George V. Reilly — (then) IIS

 Linear Hashing—smooth resizing  Cache-friendly data structures  Fine-grained locking

 Unordered collection of keys (and values)  hash(key) → int  Bucket address ≡ hash(key) modulo #buckets  O(1) find, insert, delete  Collision strategies foo nod cat bar try sap the ear

 Unless you already know cardinality  Too big—wastes memory  Too small—long chains degenerate to O(n) accesses

 20-bucket table, 400 insertions from random shuffle

 4 buckets initially; doubles when load factor > 3.0  Horrible worst-case performance

 4 buckets initially; load factor = 3.0  Grows to 400/3 buckets, 1 split every 3 insertions

 Incrementally adjust table size as records are inserted and deleted  Fast and stable performance regardless of  actual table size  how much table has grown or shrunk  Original idea from 1978  Applied to in-memory tables in 1988 by Paul Larson in CACM paper

C p Insert 0 into bucket 0 4 buckets, desired load factor = 3.0 p = 0, N = 12 Insert B 16 into bucket 3 Split bucket 0 into buckets 0 and 4 5 buckets, p = 1, N = 13 h = K mod B(B = 4) if h < p then h = K mod 2B B = 2 L ; here L = 2 ⇒ B = 2 2 = 4 2 A E p 2 A E C 4 B ⇒ Keys are hexadecimal

Insert D 16 into bucket 1 p = 1, N = p 2 A E C 4 BD ⇒ Insert 9 into bucket 1 p = 1, N = p 2 A E C 4 BD 9 h = K mod B(B = 4) if h < p then h = K mod 2B

As previously p = 1, N = p 2 A E C 4 BD ⇒ Insert F 16 into bucket 3 Split bucket 1 into buckets 1 and 5 6 buckets, p = 2, N = p 2 A E C 4 B 9 5 F 5 D h = K mod B(B = 4) if h < p then h = K mod 2B

Segment 0 Segment 1 Segment 2 HashTable Directory Array segments s buckets per Segment Bucket b ≡ Segment[ b / s ] → bucket[ b % s ]

43, Male Fred 37, Male Jim 47, Female Sheila class User { int age; Gender gender; const char* name; User* nextHashLink; }

 Extrinsic links  Hash signatures  Clump several pointer–signature pairs  Inline head clump

Jack, male, 1980 Jill, female, Signature Pointer Bucket 0Bucket 1Bucket 2

 Spread records over multiple subtables (by hashing, of course)  One lock per subtable + one lock per bucket  Restructure algorithms to reduce lock time  Use simple, bounded spinlocks

 CRITICAL_SECTION much too large for per-bucket locks  Custom 4-byte lock  State, lower 16 bits: > 0 ⇒ #readers; -1 ⇒ writer  Writer Count, upper 16 bits: 1 owner, N-1 waiters  InterlockedCompareExchange to update  Spin briefly, then Sleep & test in a loop

class ReaderWriterLock { DWORD WritersAndState; }; class NodeClump { DWORD sigs[NODES_PER_CLUMP]; NodeClump* nextClump; const void* nodes[NODES_PER_CLUMP]; }; // NODES_PER_CLUMP = 7 on Win32, 5 on Win64 => sizeof(Bucket) = 64 bytes class Bucket { ReaderWriterLock lock; NodeClump firstClump; }; class Segment { Bucket buckets[BUCKETS_PER_SEGMENT]; };

 Typesafe template wrapper  Records ( void* ) have an embedded key ( DWORD_PTR ), which is a pointer or a number  Need user-provided callback functions to  Extract a key from a record  Hash a key  Compare two keys for equality  Increment/decrement record ’ s ref-count

Table::InsertRecord(const void* pvRecord) { DWORD_PTR pnKey = userExtractKey(pvRecord); DWORD signature = userCalcHash(pnKey); size_t sub = Scramble(hashval) % numSubTables; return subTables[sub].InsertRecord(pvRecord, signature); }

SubTable::InsertRecord(const void* pvRecord, DWORD signature) { TableWriteLock(); ++numRecords; Bucket* pBucket = FindBucket(signature); pBucket->WriteLock(); TableWriteUnlock(); for (pnc = &pBucket->firstClump; pnc != NULL; pnc = pnc->nextClump) { for (i = 0; i < NODES_PER_CLUMP; ++i) { if (pnc->nodes[i] == NULL) { pnc->nodes[i] = pvRecord; pnc->sigs[i] = signature; break; } } } userAddRefRecord(pvRecord, +1); pBucket->WriteUnlock(); while (numRecords > loadFactor * numActiveBuckets) SplitBucket(); }

SubTable::SplitBucket() { TableWriteLock(); ++numActiveBuckets; if (++splitIndex == (1 << level)) { ++level; mask = (mask << 1) | 1; splitIndex = 0; } Bucket* pOldBucket = FindBucket(splitIndex); Bucket* pNewBucket = FindBucket((1 << level) | splitIndex); pOldBucket->WriteLock(); pNewBucket->WriteLock(); TableWriteUnlock(); result = SplitRecordClump(pOldBucket, pNewBucket); pOldBucket->WriteUnlock(); pNewBucket->WriteUnlock(); return result }

SubTable::FindKey(DWORD_PTR pnKey, DWORD signature, const void** ppvRecord) { TableReadLock(); Bucket* pBucket = FindBucket(signature); pBucket->ReadLock(); TableReadUnlock(); LK_RETCODE lkrc = LK_NO_SUCH_KEY; for (pnc = &pBucket->firstClump; pnc != NULL; pnc = pnc->nextClump) { for (i = 0; i < NODES_PER_CLUMP; ++i) { if (pnc->sigs[i] == signature && userEqualKeys(pnKey, userExtractKey(pnc->nodes[i]))) { *ppvRecord = pnc->nodes[i]; userAddRefRecord(*ppvRecord, +1); lkrc = LK_SUCCESS; goto Found; } } } Found: pBucket->ReadUnlock(); return lkrc; }

 Patent  Closed Source

 Scaleable hash table for shared-memory multiprocessor system

 Hoping that Microsoft will make LKRhash available on CodePlex

 P.-Å. Larson, “Dynamic Hash Tables”, Communications of the ACM, Vol 31, No 4, pp. 446–457Dynamic Hash Tables  1.pdf 1.pdf

 Cliff Click’s Non-Blocking HashtableNon-Blocking Hashtable  Facebook’s AtomicHashMap: video, GithubvideoGithub  Intel’s tbb::concurrent_hash_maptbb::concurrent_hash_map  Hash Table Performance Tests (not MT) Hash Table Performance Tests