Top Banner
Hopscotch hashing, a scalable, concurrent, resizable hash map implementation or, how to write a linearly scalable hash map up to 64 cores Paul ADENOT, <[email protected]> KTH, CSC Department November 18, 2011 Hopscotch hashing 1
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hopscotch

Hopscotch hashing, a scalable,concurrent, resizable hash map

implementationor, how to write a linearly scalable hash map up to 64 cores

Paul ADENOT, <[email protected]>

KTH, CSC Department

November 18, 2011

Hopscotch hashing 1

Page 2: Hopscotch

Table of contents :

Introduction

Hopscotch algorithm implementationA simplified algorithmThe real implementationConcurrency studyPerformances analysis

Conclusion

Hopscotch hashing 2

Page 3: Hopscotch

PlanIntroductionHopscotch algorithm implementationA simplified algorithmThe real implementationConcurrency studyPerformances analysis

Conclusion

Hopscotch hashing 3

Page 4: Hopscotch

In a few wordsI Maurice Herlihy, Brown University, Providence, RII Nir Shavit, Sun Microsystems, Burlington, MAI Moran Tzafrir, Tel-Aviv University, Tel-Aviv, Israel

Presented at the DISC’08 (DIStributed Computing 2008 atArcachon, France).First linearly scalable concurrent hash map, efficient bothin single and multiple thread applications.

Hopscotch hashing 4

Page 5: Hopscotch

Hash map?

I Data structure, capable of associating a key to avalue, thus implementing an associative array

I Supports four operations :I get(key) : retrieve the data associated to the keyI contains(key) : test if a key is present in the hash mapI insert(key, value) : insert a <key,value> pairI remove(key) : remove the <key,value> pair

I Widely used in software you use every day :I Resource caching (key : URL, value : resource)I Object implementation (Perl, Python, Javascript, Ruby,

etc.)I CSS rule matching in browsers rendering engineI Database indexing

Hopscotch hashing 5

Page 6: Hopscotch

Main goals?

I The immense majority of calls are contains(key) &get(key)

I Ability to be able to fetch a value from a key as fastas possible

I Several attack angle possible :I Use an appropriate hash function for a particular

datasetI Use better algorithmsI Leverage hardware specificityI Use multithreading

Hopscotch hashing 6

Page 7: Hopscotch

State of the artAt the moment of the publication of the paper, severalimplementation families exist :

I Chained HashingI Linear probingI Cuckoo hashingI . . .

Hopscotch hashing is a combination of these techniques,and avoids the limitations of these algorithms.

Hopscotch hashing 7

Page 8: Hopscotch

Hardware prerequisiteWhat is a cache line, and why does it matter?

I The minimum amount of data transferable from mainmemory to the cache (64 bytes on my Intel Core i7)

I False sharing problem (on multicore/multiprocessorCPU)

Cache access speed

I Register : 1 cycleI L1 cache : 4 cyclesI L2 cache : 10 cyclesI L3 cache : 40-75 cyclesI Memory : 60-100nsI Disk : 4msI Network : dozens of milliseconds

Hopscotch hashing 8

Page 9: Hopscotch

Chained Hashing

0x0000

0x0004

0x0008

0x000C

0x0010

0x0014

0x0018

value Bucket 0

1

2

3

4

5

6

key value

index=hash(key);

value value

value value value

value value value

value value value

value value value

value value value

I ExtensibleI Not cache friendly : closed addressingI Trivial to implementI Pointer space overheadI Need to allocate memory on insertion (or to use a

pool, . . . )Hopscotch hashing 9

Page 10: Hopscotch

Linear probing

Bucket for one item

K

V

K

V

K

V

K

V

K

V

1 - Value hashed for this bucket

3 - Actual insertion point2 - Linear probing

K

V

K

V

K

V

K

V

K

V

K

V

K

V

K

V

Already occupied

I Cache friendly : closed addressingI Inefficient when the table is rather full (over 75%, most

of the implementation reallocate and rehash thetable)

Hopscotch hashing 10

Page 11: Hopscotch

PlanIntroductionHopscotch algorithm implementationA simplified algorithmThe real implementationConcurrency studyPerformances analysis

Conclusion

Hopscotch hashing 11

Page 12: Hopscotch

Main characteristics of Hopscotch hash map

I Concurrent, and highly scalableI Cache friendly (unlike open addressing)I Good behavior when the hash table is fullI Resizable

Hopscotch hashing 12

Page 13: Hopscotch

General idea (not the real-world implementation) (1)

I Closed addressing, like the linear probingI Item are hashed into a location using a single hash

functionI Entries have a bit table of width H (H is equal to the

size of a machine word)I When inserting, and the location resulting from the

hashing is empty, insert to this location.I When inserting, and the location resulting from the

hashing is not empty, the <key, value> pair isdisplaced to another close and empty location, andthe displacement is written in the bit table

Hopscotch hashing 13

Page 14: Hopscotch

General idea (not the real-world implementation) (2)

I If there is no close and empty location, we find anitem y which hashes between the first empty location(j), and the current entry (i), but within H − 1 elements.We displace y to j, thus we create an empty locationclose to i. If no solution exist, resize and rehash thetable (unlikely, but possible, about Θ(1/H!) ofprobability, that is 3.8 × 10−36 for H = 32 and a goodhashing function)

I When searching, hash the key, and lookup the entryI If the entry found does not match the key, follow the

displacement sequence until we get to the valueI If we can’t find the value at the end of the

displacement sequence, return false

Hopscotch hashing 14

Page 15: Hopscotch

Example

Hopscotch hashing 15

Page 16: Hopscotch

How concurrency is handledThe strategy used is rather simple

I Fine grained locks are protecting the buckets fromconcurrent mutation

I A lock is therefore mapped to each bucketI contains(key) is obstruction-free, and relies on

timestamps to handle concurrency

Hopscotch hashing 16

Page 17: Hopscotch

The real Hopscotch hashing implementation

I In place of the displacement in the array, we useregular pointers and a linked list, still having thelocality constraint (i.e. location of linked-list node areenforced to be within H = hop_range of the initialnode)

I The remove(key) method tries to optimize for cacheline alignment, to rely on a minimal number of cacheline, to avoid having to fetch from main memory.

Hopscotch hashing 17

Page 18: Hopscotch

Handling concurrency, finding the hot spotAnalysis of the data structure utilisation :

I Most of the operations are read-only (contains(key),search(key))

I Each group of buckets (called Segment) has atimestamp field to easily handle concurrent reads andwrites (i.e. contains(key) and remove(key))

I Each Segment also has a lock, to prevent concurrentmutation of data structure.

Hopscotch hashing 18

Page 19: Hopscotch

Linearization pointsadd(pair) and remove(key) use locks, and aredeadlock-free, but not livelock-free, contains(key) isobstruction-freeadd(pair) : linearized when finding the key when it exists,

when adding the bucket to the list of bucketsin the linked list (updating the pointers)

remove(key) : linearized when failing at finding the key (ifthe key does in fact no exist), or when the keystable entry is overwritten

contains(key) : linearized when it finds the key, or when itreaches the end of the list (on an unsuccessfulcontains(key)); and the timestamp isunchanged.

Hopscotch hashing 19

Page 20: Hopscotch

Pseudocode for the obstruction-free contains(key) :� �1 bool contains(KeyType key) {2 int hash = hash(key);3 Segment segment = get_segment(hash);4 Bucket current_bucket = get_bucket(segment, hash);5 int start_timestamp;6 do {7 start_timestamp = segment.timestamp[hash];8 short next_delta = current_bucket.first_delta;9 while (NULL != next_delta) {

10 current_bucket.first_delta += next_delta;11 if (key == current_bucket.key) {12 return true;13 }14 next_delta = current_bucket.next_delta15 }16 } while (start_timestamp != bucket.timestamp[hash])17 return false;18 }� �

Hopscotch hashing 20

Page 21: Hopscotch

Performances analysis

I Most important property : expected constant timeperformance

I In the common case, there is very few items in thebuckets, which is : (α is the density, <= 0)

Number of items in a bucket = 1 +e2α − 1 − 2α

4

I add(pair), remove(key), contains(key) complete inO(1)

I resize() completes in O(n) (n being the number ofelements in the hash map)

Hopscotch hashing 21

Page 22: Hopscotch

Performances, mainly contains(key)

Hopscotch hashing 22

Page 23: Hopscotch

Performances, various operation

Hopscotch hashing 23

Page 24: Hopscotch

A beginning of an explanation?

Hopscotch hashing 24

Page 25: Hopscotch

PlanIntroductionHopscotch algorithm implementationA simplified algorithmThe real implementationConcurrency studyPerformances analysis

Conclusion

Hopscotch hashing 25

Page 26: Hopscotch

ConclusionI Always have hardware consideration when designing

a data structureI The rather low concurrency guarantees can lead to

very good performancesI Simple is not always betterI A 300 line data structure implementation can bring a

tremendous speedup to a program

Hopscotch hashing 26

Page 27: Hopscotch

Questions?

Slides available at http://paul.cx/public/hopscotch.pdf

Hopscotch hashing 27

Page 28: Hopscotch

ReferencesThe paper itself : http:

//www.springerlink.com/content/u710121187m65436/

Obstruction-free introduction paper : Herlihy, M.; Luchangco, V.; Moir,M.,, Distributed Computing Systems, 2003,

Conference from Intel : http://www.gdcvault.com/play/1014645/-SPONSORED-Hotspots-FLOPS-and

Wikipedia pages : CPU cache, Locality of reference, Openaddressing, Hash table.

Master thesis : Sae-eung, Suntorn, "Analysis of False Cache LineSharing Effects on Multicore CPUs" (2010). Master’sProjects. Paper 2.http://scholarworks.sjsu.edu/etd_projects/2

The course’s book

Hopscotch hashing 28