Why Hashes Will Be Faster in Ruby 2.0
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Why Hashes Will Be Faster in Ruby 2.0

on

  • 1,776 views

In this micro talk, I'll review the basic theory behind hash functions and hash tables, show you the new internal data structures that Ruby 2.0 uses to save keys and values, and present some ...

In this micro talk, I'll review the basic theory behind hash functions and hash tables, show you the new internal data structures that Ruby 2.0 uses to save keys and values, and present some performance data that proves this optimization exists and how much time it will actually save you.

Statistics

Views

Total Views
1,776
Views on SlideShare
864
Embed Views
912

Actions

Likes
2
Downloads
14
Comments
0

4 Embeds 912

http://lanyrd.com 902
https://twitter.com 7
http://www.google.com 2
http://webcache.googleusercontent.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Why Hashes Will Be Faster in Ruby 2.0 Presentation Transcript

  • 1. Why Hashes Will Be Faster in Ruby 2.0Pat Shaughnessyhttp://patshaughnessy.net@pat_shaughnessyMcKinsey & Co.
  • 2. h = {}=> {}h[:key] = value=> "value"h[:key]=> "value"
  • 3. Time to retrieve 10000 values (ms) 3 2.5 2 1.5 1 0.5 1 100 10,000 1,000,000 Hash size
  • 4. 0 1 2 3 4 5 6 7 8 9 10 st_table_entry hash key record next
  • 5. func(key) = hash
  • 6. :key.hash=> 4321153767602860351"abcdef".hash=> -1182018534040789564
  • 7. hash % 11 = bin index
  • 8. 0 1 2 3 4 5 6 7 8 9 10 st_table_entry hash key record next
  • 9. 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
  • 10. 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
  • 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 st_table_entry hash key record next
  • 12. 0 1 2 3 4 5 6 7 8 9 10entry entry entry entry entry entry entry entry entry entry entryentry entry entry entry entry entry entry entry entry entry entryentry entry entry entry entry entry entry entry entry entry entryentry entry entry entry entry entry entry entryentry entry entry
  • 13. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entryentry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entryentry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entry entryentry entry entry entry entry entry entry entry
  • 14. Hashes in Ruby 2.0
  • 15. 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
  • 16. :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
  • 17. Hashes are Arrays!
  • 18. 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
  • 19. 6% improvement0 Loading 10,000 small records
  • 20. 2.6% improvementRails App: Ruby Execution Time
  • 21. 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]) endend 5?
  • 22. 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]) endend 5 50?
  • 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]) endend 50 409!
  • 24. http://patshaughnessy.net/ruby-under-a-microscope