SlideShare a Scribd company logo
1 of 26
Why Hashes Will Be
          Faster in Ruby 2.0
Pat Shaughnessy
http://patshaughnessy.net
@pat_shaughnessy
McKinsey & Co.
h = {}
=> {}


h[:key] = 'value'
=> "value"


h[:key]
=> "value"
Time to retrieve 10000 values (ms)    3


                                     2.5


                                      2


                                     1.5


                                      1


                                     0.5



                                           1   100       10,000   1,000,000

                                                 Hash size
0   1   2         3          4   5   6   7   8   9   10


            st_table_entry

                 hash

                  key

                record

                 next
func(key) = hash
:key.hash
=> 4321153767602860351

"abcdef".hash
=> -1182018534040789564
hash % 11 = bin index
0   1   2         3          4   5   6   7   8   9   10


            st_table_entry

                 hash

                  key

                record

                 next
0   1   2         3          4   5   6         7          8   9   10


            st_table_entry               st_table_entry

                 hash                         hash

                  key                          key

                record                       record

                 next                         next
0         1          2         3          4   5   6         7          8   9   10


    st_table_entry       st_table_entry               st_table_entry

         hash                 hash                         hash

          key                  key                          key

        record               record                       record

         next                 next                         next
0         1          2         3          4   5   6         7          8   9   10


    st_table_entry       st_table_entry               st_table_entry

         hash                 hash                         hash

          key                  key                          key

        record               record                       record

         next                 next                         next




                         st_table_entry

                              hash

                               key

                             record

                              next
0       1       2       3       4       5       6       7       8       9      10

entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry




entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry




entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry




entry           entry   entry   entry           entry   entry   entry           entry



entry                           entry                           entry
0       1       2       3       4       5       6       7       8       9      10      11      12      13      14      15      16      17      18

entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry




entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry




entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry   entry




entry           entry   entry   entry                   entry   entry           entry                   entry
Hashes in Ruby 2.0
0   1    2         3          4   5   6   7   8   9   10




             malloc


             st_table_entry

                  hash

                   key

                 record

                  next




        Ruby 1.8, Ruby 1.9
:key =>       :key2 =>       :key3 =>       :key4 =>   :key5 =>         :key6 =>
 value         value2         value3         value4     value5           value6


          st_table_entry   st_table_entry              st_table_entry

               hash             hash                        hash

                key              key                         key

              record           record                      record

               next             next                        next




               Ruby 2.0 - first 6
                  elements
Hashes are
 Arrays!
Time to add 1 element 10,000 times (ms)   14

                                          13

                                          12

                                          11

                                          10

                                          9

                                          8

                                          7

                                          6

                                          5

                                          4

                                          3

                                          2

                                          1

                                          0
                                               0   1   2   3   4   5   6   7   8   9   10 11 12 13 14 15 16 17 18 19 20

                                                                           Hash size
6% improvement




0




    Loading 10,000 small records
2.6% improvement




Rails App: Ruby Execution Time
How many hashes?
describe "GET index" do
  it "assigns all people as @people" do
    person = Person.create! valid_attributes
    get :index, {}, valid_session
    assigns(:people).should eq([person])
  end
end

                    5?
How many hashes?
describe "GET index" do
  it "assigns all people as @people" do
    person = Person.create! valid_attributes
    get :index, {}, valid_session
    assigns(:people).should eq([person])
  end
end

                 5 50?
How many hashes?
describe "GET index" do
  it "assigns all people as @people" do
    person = Person.create! valid_attributes
    get :index, {}, valid_session
    assigns(:people).should eq([person])
  end
end

               50 409!
http://patshaughnessy.net/ruby-under-a-microscope

More Related Content

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Why Hashes Will Be Faster in Ruby 2.0

  • 1. Why Hashes Will Be Faster in Ruby 2.0 Pat Shaughnessy http://patshaughnessy.net @pat_shaughnessy McKinsey & Co.
  • 2. h = {} => {} h[:key] = 'value' => "value" h[:key] => "value"
  • 3.
  • 4. Time to retrieve 10000 values (ms) 3 2.5 2 1.5 1 0.5 1 100 10,000 1,000,000 Hash size
  • 5. 0 1 2 3 4 5 6 7 8 9 10 st_table_entry hash key record next
  • 8. hash % 11 = bin index
  • 9. 0 1 2 3 4 5 6 7 8 9 10 st_table_entry hash key record next
  • 10. 0 1 2 3 4 5 6 7 8 9 10 st_table_entry st_table_entry hash hash key key record record next next
  • 11. 0 1 2 3 4 5 6 7 8 9 10 st_table_entry st_table_entry st_table_entry hash hash hash key key key record record record next next next
  • 12. 0 1 2 3 4 5 6 7 8 9 10 st_table_entry st_table_entry st_table_entry hash hash hash key key key record record record next next next st_table_entry hash key record next
  • 13. 0 1 2 3 4 5 6 7 8 9 10 entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry
  • 14. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry
  • 16. 0 1 2 3 4 5 6 7 8 9 10 malloc st_table_entry hash key record next Ruby 1.8, Ruby 1.9
  • 17. :key => :key2 => :key3 => :key4 => :key5 => :key6 => value value2 value3 value4 value5 value6 st_table_entry st_table_entry st_table_entry hash hash hash key key key record record record next next next Ruby 2.0 - first 6 elements
  • 19.
  • 20. Time to add 1 element 10,000 times (ms) 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Hash size
  • 21. 6% improvement 0 Loading 10,000 small records
  • 22. 2.6% improvement Rails App: Ruby Execution Time
  • 23. How many hashes? describe "GET index" do it "assigns all people as @people" do person = Person.create! valid_attributes get :index, {}, valid_session assigns(:people).should eq([person]) end end 5?
  • 24. How many hashes? describe "GET index" do it "assigns all people as @people" do person = Person.create! valid_attributes get :index, {}, valid_session assigns(:people).should eq([person]) end end 5 50?
  • 25. How many hashes? describe "GET index" do it "assigns all people as @people" do person = Person.create! valid_attributes get :index, {}, valid_session assigns(:people).should eq([person]) end end 50 409!

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n