SlideShare uma empresa Scribd logo
1 de 24
Baixar para ler offline
Faster persistent data structures through
                hashing

                Johan Tibell
           johan.tibell@gmail.com



                2011-09-23
Motivating problem: Twitter data analysis



       I'm computing a communication graph from Twitter
       data and then scan it daily to allocate social capital to
       nodes behaving in a good karmic manner. The graph
       is culled from 100 million tweets and has about 3
       million nodes.

   We need a data structure that is
       fast when used with string keys, and
       doesn't use too much memory.
Persistent maps in Haskell




      Data.Map is the most commonly used map type.
      It's implemented using size balanced trees and is
      representative of the performance of other binary tree
      implementations.
      Keys can be of any type, as long as values of the type can
      be ordered.
Real world performance of Data.Map




      Good in theory: no more than O(log n) comparisons.
      Not great in practice: up to O(log n) comparisons!
      Many common types are expensive to compare e.g
      String, ByteString, and Text.
      Given a string of length k, we need O(k ∗ log n)
      comparisons to look up an entry.
Hash tables




      Hash tables perform well with string keys: O(k) amortized
      lookup time for strings of length k.
      However, we want persistent maps, not mutable hash
      tables.
Milan Straka's idea: IntMaps as arrays




      We can use hashing without using hash tables!
      Data.IntMap implements a persistent array and is much
      faster than Data.Map.
      Use hashing to derive an Int from an arbitrary key.

      class Hashable a where
        hash :: a -> Int
Collisions are easy to deal with




      IntMap implements a sparse, persistent array of size 232
      (or 264 ).
      Hashing using this many buckets makes collisions rare: for
      224 entries we expect about 32,000 single collisions.
      Implication: We can use any old collision handling strategy
      (e.g. chaining using linked lists).
HashMap implemented using an IntMap




  Naive implementation:
       newtype HashMap k v = HashMap (IntMap [(k, v)])
  By inlining (``unpacking'') the list and pair constructors we can
  save 2 words of memory per key/value pair.
Benchmark: Map vs HashMap



  Keys: 212 random 8-byte ByteStrings


                         Runtime (μs)      Runtime
                        Map HashMap      % increase
            lookup     1956        916         -53%
              insert   3543       1855         -48%
             delete    3791       1838         -52%
Can we do better?




      Imperative hash tables still perform better, perhaps there's
      room for improvement.
      We still need to perform O(min(W, log n)) Int
      comparisons, where W is the number of bits in a word.
      The memory overhead per key/value pair is still high, about
      9 words per key/value pair.
Borrowing from our neighbours




      Clojure uses a hash-array mapped trie (HAMT) data
      structure to implement persistent maps.
      Described in the paper ``Ideal Hash Trees'' by Bagwell
      (2001).
      Originally a mutable data structure implemented in C++.
      Clojure's persistent version was created by Rich Hickey.
Hash-array mapped tries




      Shallow tree with high branching factor.
      Each node, except the leaf nodes, contains an array of up
      to 32 elements.
      5 bits of the hash are used to index the array at each level.
      A clever trick, using bit population count, is used to
      represent sparse arrays.
HAMT

                  Hash




  BitmapIndexed




       Leaf
The Haskell definition of a HAMT



  data HashMap k v
      = Empty
      | BitmapIndexed !Bitmap !(Array (HashMap k v))
      | Leaf !Hash !k v
      | Full !(Array (HashMap k v))
      | Collision !Hash !(Array (Leaf k v))

  type Bitmap = Word
  type Hash = Int
  data Array a = Array (Array# a)
High performance Haskell programming




  Optimized implementation using standard techniques:
      constructor unpacking,
      GHC's new INLINABLE pragma, and
      paying careful attention to strictness.
  insert performance still bad (e.g compare to hash tables).
Optimizing insertion




      Most time in insert is spent copying small arrays.
      Array copying is implemented in Haskell and GHC doesn't
      apply enough loop optimizations to make it run fast.
      When allocating arrays GHC fills the array with dummy
      elements, which are immediately overwritten.
Optimizing insertion: copy less




      Bagwell's original formulation used a fanout of 32.
      A fanout of 16 seems to provide a better trade-off between
      lookup and insert performance in our setting.
      Improved performance by 14%
Optimizing insertion: copy faster




      Daniel Peebles and I have implemented a set of new
      primops for copying arrays in GHC.
      The implementation generates straight-line code for copies
      of statically known small size, and uses a fast memcpy
      otherwise.
      Improved performance by 20%
Optimizing insertion: common patterns


      In many cases maps are created in one go from a
      sequence of key/value pairs.
      We can optimize for this case by repeatedly mutating the
      HAMT and freezing it when we're done.

  Keys: 212 random 8-byte ByteStrings


                                    Runtime (%)
                   fromList/pure       100
                fromList/mutating       50
Optimizing lookup: Faster population count




      Tried several bit population count implementations.
      Best speed/memory-use trade-off is a lookup table based
      approach.
      Using the POPCNT SSE 4.2 instructions improves the
      performance of lookup by 12%.
Benchmark: IntMap-based vs HAMT


  Keys: 212 random 8-byte ByteStrings


                          Runtime (μs)      Runtime
                        IntMap HAMT       % increase
             lookup        916      477         -48%
               insert     1855     1998           8%
              delete      1838     2303          25%

  The benchmarks don't include the POPCNT optimization, due to
  it not being available on many architectures.
Memory usage: IntMap-based
  Total: 96 MB, tree: 66MB (220 Int entries)



           mem              43,678,549 bytes x seconds                  Thu Sep 22 13:48 2011
             bytes




            80M


                                                                   main:Data.HashMap.Common.Tip



            60M




                                                                   ghc-prim:GHC.Types.I#

            40M




                                                                   main:Data.HashMap.Common.Bin
            20M




            0M
              0.0    0.2   0.4              0.6          seconds
Memory usage: HAMT
  Total: 71MB, tree: 41MB (220 Int entries)



           mem                                            25,805,095 bytes x seconds                        Thu Sep 22 13:49 2011
             bytes




            60M

                                                                                                 ghc-prim:GHC.Types.I#




                                                                                                 main:Data.HashMap.Base.Leaf
            40M




                                                                                                 MUT_ARR_PTRS_FROZEN


            20M


                                                                                                 main:Data.HashMap.Base.BitmapIndexed




            0M
              0.0    0.1   0.1   0.2   0.2   0.2   0.3   0.3   0.4   0.4   0.5   0.5   seconds
Summary



  Keys: 212 random 8-byte ByteStrings


                        Runtime (μs)     Runtime
                         Map HAMT      % increase
             lookup     1956     477         -76%
               insert   3543   1998          -44%
              delete    3791   2303          -39%

Mais conteúdo relacionado

Mais procurados

Algorithms 101 for Data Scientists
Algorithms 101 for Data ScientistsAlgorithms 101 for Data Scientists
Algorithms 101 for Data ScientistsChristopher Conlan
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1Jatin Miglani
 
Graph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahon
Graph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahonGraph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahon
Graph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahonChristopher Conlan
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce AlgorithmsAmund Tveit
 
Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPyDevashish Kumar
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)PyData
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesAndrew Ferlitsch
 
Chunked, dplyr for large text files
Chunked, dplyr for large text filesChunked, dplyr for large text files
Chunked, dplyr for large text filesEdwin de Jonge
 
Too Much Data? - Just Sample, Just Hash, ...
Too Much Data? - Just Sample, Just Hash, ...Too Much Data? - Just Sample, Just Hash, ...
Too Much Data? - Just Sample, Just Hash, ...Andrii Gakhov
 
An adaptive algorithm for detection of duplicate records
An adaptive algorithm for detection of duplicate recordsAn adaptive algorithm for detection of duplicate records
An adaptive algorithm for detection of duplicate recordsLikan Patra
 
Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in RFlorian Uhlitz
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data AnalysisAndrew Henshaw
 
8. R Graphics with R
8. R Graphics with R8. R Graphics with R
8. R Graphics with RFAO
 
Introduction to Pandas and Time Series Analysis [PyCon DE]
Introduction to Pandas and Time Series Analysis [PyCon DE]Introduction to Pandas and Time Series Analysis [PyCon DE]
Introduction to Pandas and Time Series Analysis [PyCon DE]Alexander Hendorf
 

Mais procurados (20)

Algorithms 101 for Data Scientists
Algorithms 101 for Data ScientistsAlgorithms 101 for Data Scientists
Algorithms 101 for Data Scientists
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1
 
Graph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahon
Graph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahonGraph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahon
Graph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahon
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
 
Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPy
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Hadoop map reduce concepts
Hadoop map reduce conceptsHadoop map reduce concepts
Hadoop map reduce concepts
 
Chunked, dplyr for large text files
Chunked, dplyr for large text filesChunked, dplyr for large text files
Chunked, dplyr for large text files
 
R language introduction
R language introductionR language introduction
R language introduction
 
Too Much Data? - Just Sample, Just Hash, ...
Too Much Data? - Just Sample, Just Hash, ...Too Much Data? - Just Sample, Just Hash, ...
Too Much Data? - Just Sample, Just Hash, ...
 
An adaptive algorithm for detection of duplicate records
An adaptive algorithm for detection of duplicate recordsAn adaptive algorithm for detection of duplicate records
An adaptive algorithm for detection of duplicate records
 
Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in R
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
NumPy
NumPyNumPy
NumPy
 
8. R Graphics with R
8. R Graphics with R8. R Graphics with R
8. R Graphics with R
 
Text encryption
Text encryptionText encryption
Text encryption
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 
Introduction to Pandas and Time Series Analysis [PyCon DE]
Introduction to Pandas and Time Series Analysis [PyCon DE]Introduction to Pandas and Time Series Analysis [PyCon DE]
Introduction to Pandas and Time Series Analysis [PyCon DE]
 
iPython
iPythoniPython
iPython
 

Semelhante a Faster persistent data structures through hashing

Structures de données exotiques
Structures de données exotiquesStructures de données exotiques
Structures de données exotiquesSamir Bessalah
 
How to Stop Worrying and Start Caching in Java
How to Stop Worrying and Start Caching in JavaHow to Stop Worrying and Start Caching in Java
How to Stop Worrying and Start Caching in Javasrisatish ambati
 
JDD2015: On-heap cache vs Off-heap cache - Radek Grębski
JDD2015: On-heap cache vs Off-heap cache - Radek GrębskiJDD2015: On-heap cache vs Off-heap cache - Radek Grębski
JDD2015: On-heap cache vs Off-heap cache - Radek GrębskiPROIDEA
 
On heap cache vs off-heap cache
On heap cache vs off-heap cacheOn heap cache vs off-heap cache
On heap cache vs off-heap cachergrebski
 
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...Spark Summit
 
Engineering fast indexes
Engineering fast indexesEngineering fast indexes
Engineering fast indexesDaniel Lemire
 
Scaling metagenome assembly
Scaling metagenome assemblyScaling metagenome assembly
Scaling metagenome assemblyc.titus.brown
 
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...Andrii Gakhov
 
Revisão: Forwarding Metamorphosis: Fast Programmable Match-Action Processing ...
Revisão: Forwarding Metamorphosis: Fast Programmable Match-Action Processing ...Revisão: Forwarding Metamorphosis: Fast Programmable Match-Action Processing ...
Revisão: Forwarding Metamorphosis: Fast Programmable Match-Action Processing ...Bruno Castelucci
 
RecSplit Minimal Perfect Hashing
RecSplit Minimal Perfect HashingRecSplit Minimal Perfect Hashing
RecSplit Minimal Perfect HashingThomas Mueller
 
A Tale of Data Pattern Discovery in Parallel
A Tale of Data Pattern Discovery in ParallelA Tale of Data Pattern Discovery in Parallel
A Tale of Data Pattern Discovery in ParallelJenny Liu
 
Data-Centric Parallel Programming
Data-Centric Parallel ProgrammingData-Centric Parallel Programming
Data-Centric Parallel Programminginside-BigData.com
 
Code GPU with CUDA - Optimizing memory and control flow
Code GPU with CUDA - Optimizing memory and control flowCode GPU with CUDA - Optimizing memory and control flow
Code GPU with CUDA - Optimizing memory and control flowMarina Kolpakova
 
The reasons why 64-bit programs require more stack memory
The reasons why 64-bit programs require more stack memoryThe reasons why 64-bit programs require more stack memory
The reasons why 64-bit programs require more stack memoryPVS-Studio
 
Enterprise Scale Topological Data Analysis Using Spark
Enterprise Scale Topological Data Analysis Using SparkEnterprise Scale Topological Data Analysis Using Spark
Enterprise Scale Topological Data Analysis Using SparkAlpine Data
 
Enterprise Scale Topological Data Analysis Using Spark
Enterprise Scale Topological Data Analysis Using SparkEnterprise Scale Topological Data Analysis Using Spark
Enterprise Scale Topological Data Analysis Using SparkSpark Summit
 
Xian He Sun Data-Centric Into
Xian He Sun Data-Centric IntoXian He Sun Data-Centric Into
Xian He Sun Data-Centric IntoSciCompIIT
 

Semelhante a Faster persistent data structures through hashing (20)

Structures de données exotiques
Structures de données exotiquesStructures de données exotiques
Structures de données exotiques
 
ISBI MPI Tutorial
ISBI MPI TutorialISBI MPI Tutorial
ISBI MPI Tutorial
 
How to Stop Worrying and Start Caching in Java
How to Stop Worrying and Start Caching in JavaHow to Stop Worrying and Start Caching in Java
How to Stop Worrying and Start Caching in Java
 
15 Jo P Mar 08
15 Jo P Mar 0815 Jo P Mar 08
15 Jo P Mar 08
 
JDD2015: On-heap cache vs Off-heap cache - Radek Grębski
JDD2015: On-heap cache vs Off-heap cache - Radek GrębskiJDD2015: On-heap cache vs Off-heap cache - Radek Grębski
JDD2015: On-heap cache vs Off-heap cache - Radek Grębski
 
On heap cache vs off-heap cache
On heap cache vs off-heap cacheOn heap cache vs off-heap cache
On heap cache vs off-heap cache
 
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
 
Engineering fast indexes
Engineering fast indexesEngineering fast indexes
Engineering fast indexes
 
Scaling metagenome assembly
Scaling metagenome assemblyScaling metagenome assembly
Scaling metagenome assembly
 
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...
 
Revisão: Forwarding Metamorphosis: Fast Programmable Match-Action Processing ...
Revisão: Forwarding Metamorphosis: Fast Programmable Match-Action Processing ...Revisão: Forwarding Metamorphosis: Fast Programmable Match-Action Processing ...
Revisão: Forwarding Metamorphosis: Fast Programmable Match-Action Processing ...
 
RecSplit Minimal Perfect Hashing
RecSplit Minimal Perfect HashingRecSplit Minimal Perfect Hashing
RecSplit Minimal Perfect Hashing
 
A Tale of Data Pattern Discovery in Parallel
A Tale of Data Pattern Discovery in ParallelA Tale of Data Pattern Discovery in Parallel
A Tale of Data Pattern Discovery in Parallel
 
Data-Centric Parallel Programming
Data-Centric Parallel ProgrammingData-Centric Parallel Programming
Data-Centric Parallel Programming
 
Code GPU with CUDA - Optimizing memory and control flow
Code GPU with CUDA - Optimizing memory and control flowCode GPU with CUDA - Optimizing memory and control flow
Code GPU with CUDA - Optimizing memory and control flow
 
The reasons why 64-bit programs require more stack memory
The reasons why 64-bit programs require more stack memoryThe reasons why 64-bit programs require more stack memory
The reasons why 64-bit programs require more stack memory
 
Enterprise Scale Topological Data Analysis Using Spark
Enterprise Scale Topological Data Analysis Using SparkEnterprise Scale Topological Data Analysis Using Spark
Enterprise Scale Topological Data Analysis Using Spark
 
Enterprise Scale Topological Data Analysis Using Spark
Enterprise Scale Topological Data Analysis Using SparkEnterprise Scale Topological Data Analysis Using Spark
Enterprise Scale Topological Data Analysis Using Spark
 
Xian He Sun Data-Centric Into
Xian He Sun Data-Centric IntoXian He Sun Data-Centric Into
Xian He Sun Data-Centric Into
 
Graph500
Graph500Graph500
Graph500
 

Último

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Último (20)

Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Faster persistent data structures through hashing

  • 1. Faster persistent data structures through hashing Johan Tibell johan.tibell@gmail.com 2011-09-23
  • 2. Motivating problem: Twitter data analysis I'm computing a communication graph from Twitter data and then scan it daily to allocate social capital to nodes behaving in a good karmic manner. The graph is culled from 100 million tweets and has about 3 million nodes. We need a data structure that is fast when used with string keys, and doesn't use too much memory.
  • 3. Persistent maps in Haskell Data.Map is the most commonly used map type. It's implemented using size balanced trees and is representative of the performance of other binary tree implementations. Keys can be of any type, as long as values of the type can be ordered.
  • 4. Real world performance of Data.Map Good in theory: no more than O(log n) comparisons. Not great in practice: up to O(log n) comparisons! Many common types are expensive to compare e.g String, ByteString, and Text. Given a string of length k, we need O(k ∗ log n) comparisons to look up an entry.
  • 5. Hash tables Hash tables perform well with string keys: O(k) amortized lookup time for strings of length k. However, we want persistent maps, not mutable hash tables.
  • 6. Milan Straka's idea: IntMaps as arrays We can use hashing without using hash tables! Data.IntMap implements a persistent array and is much faster than Data.Map. Use hashing to derive an Int from an arbitrary key. class Hashable a where hash :: a -> Int
  • 7. Collisions are easy to deal with IntMap implements a sparse, persistent array of size 232 (or 264 ). Hashing using this many buckets makes collisions rare: for 224 entries we expect about 32,000 single collisions. Implication: We can use any old collision handling strategy (e.g. chaining using linked lists).
  • 8. HashMap implemented using an IntMap Naive implementation: newtype HashMap k v = HashMap (IntMap [(k, v)]) By inlining (``unpacking'') the list and pair constructors we can save 2 words of memory per key/value pair.
  • 9. Benchmark: Map vs HashMap Keys: 212 random 8-byte ByteStrings Runtime (μs) Runtime Map HashMap % increase lookup 1956 916 -53% insert 3543 1855 -48% delete 3791 1838 -52%
  • 10. Can we do better? Imperative hash tables still perform better, perhaps there's room for improvement. We still need to perform O(min(W, log n)) Int comparisons, where W is the number of bits in a word. The memory overhead per key/value pair is still high, about 9 words per key/value pair.
  • 11. Borrowing from our neighbours Clojure uses a hash-array mapped trie (HAMT) data structure to implement persistent maps. Described in the paper ``Ideal Hash Trees'' by Bagwell (2001). Originally a mutable data structure implemented in C++. Clojure's persistent version was created by Rich Hickey.
  • 12. Hash-array mapped tries Shallow tree with high branching factor. Each node, except the leaf nodes, contains an array of up to 32 elements. 5 bits of the hash are used to index the array at each level. A clever trick, using bit population count, is used to represent sparse arrays.
  • 13. HAMT Hash BitmapIndexed Leaf
  • 14. The Haskell definition of a HAMT data HashMap k v = Empty | BitmapIndexed !Bitmap !(Array (HashMap k v)) | Leaf !Hash !k v | Full !(Array (HashMap k v)) | Collision !Hash !(Array (Leaf k v)) type Bitmap = Word type Hash = Int data Array a = Array (Array# a)
  • 15. High performance Haskell programming Optimized implementation using standard techniques: constructor unpacking, GHC's new INLINABLE pragma, and paying careful attention to strictness. insert performance still bad (e.g compare to hash tables).
  • 16. Optimizing insertion Most time in insert is spent copying small arrays. Array copying is implemented in Haskell and GHC doesn't apply enough loop optimizations to make it run fast. When allocating arrays GHC fills the array with dummy elements, which are immediately overwritten.
  • 17. Optimizing insertion: copy less Bagwell's original formulation used a fanout of 32. A fanout of 16 seems to provide a better trade-off between lookup and insert performance in our setting. Improved performance by 14%
  • 18. Optimizing insertion: copy faster Daniel Peebles and I have implemented a set of new primops for copying arrays in GHC. The implementation generates straight-line code for copies of statically known small size, and uses a fast memcpy otherwise. Improved performance by 20%
  • 19. Optimizing insertion: common patterns In many cases maps are created in one go from a sequence of key/value pairs. We can optimize for this case by repeatedly mutating the HAMT and freezing it when we're done. Keys: 212 random 8-byte ByteStrings Runtime (%) fromList/pure 100 fromList/mutating 50
  • 20. Optimizing lookup: Faster population count Tried several bit population count implementations. Best speed/memory-use trade-off is a lookup table based approach. Using the POPCNT SSE 4.2 instructions improves the performance of lookup by 12%.
  • 21. Benchmark: IntMap-based vs HAMT Keys: 212 random 8-byte ByteStrings Runtime (μs) Runtime IntMap HAMT % increase lookup 916 477 -48% insert 1855 1998 8% delete 1838 2303 25% The benchmarks don't include the POPCNT optimization, due to it not being available on many architectures.
  • 22. Memory usage: IntMap-based Total: 96 MB, tree: 66MB (220 Int entries) mem 43,678,549 bytes x seconds Thu Sep 22 13:48 2011 bytes 80M main:Data.HashMap.Common.Tip 60M ghc-prim:GHC.Types.I# 40M main:Data.HashMap.Common.Bin 20M 0M 0.0 0.2 0.4 0.6 seconds
  • 23. Memory usage: HAMT Total: 71MB, tree: 41MB (220 Int entries) mem 25,805,095 bytes x seconds Thu Sep 22 13:49 2011 bytes 60M ghc-prim:GHC.Types.I# main:Data.HashMap.Base.Leaf 40M MUT_ARR_PTRS_FROZEN 20M main:Data.HashMap.Base.BitmapIndexed 0M 0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 seconds
  • 24. Summary Keys: 212 random 8-byte ByteStrings Runtime (μs) Runtime Map HAMT % increase lookup 1956 477 -76% insert 3543 1998 -44% delete 3791 2303 -39%