Why Hashes Will Be          Faster in Ruby 2.0Pat Shaughnessyhttp://patshaughnessy.net@pat_shaughnessyMcKinsey & Co.
h = {}=> {}h[:key] = value=> "value"h[:key]=> "value"
Time to retrieve 10000 values (ms)    3                                     2.5                                      2    ...
0   1   2         3          4   5   6   7   8   9   10            st_table_entry                 hash                  ke...
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                  ke...
0   1   2         3          4   5   6         7          8   9   10            st_table_entry               st_table_entr...
0         1          2         3          4   5   6         7          8   9   10    st_table_entry       st_table_entry  ...
0         1          2         3          4   5   6         7          8   9   10    st_table_entry       st_table_entry  ...
0       1       2       3       4       5       6       7       8       9      10entry   entry   entry   entry   entry   e...
0       1       2       3       4       5       6       7       8       9      10      11      12      13      14      15 ...
Hashes in Ruby 2.0
0   1    2         3          4   5   6   7   8   9   10             malloc             st_table_entry                  ha...
:key =>       :key2 =>       :key3 =>       :key4 =>   :key5 =>         :key6 => value         value2         value3      ...
Hashes are Arrays!
Time to add 1 element 10,000 times (ms)   14                                          13                                  ...
6% improvement0    Loading 10,000 small records
2.6% improvementRails App: Ruby Execution Time
How many hashes?describe "GET index" do  it "assigns all people as @people" do    person = Person.create! valid_attributes...
How many hashes?describe "GET index" do  it "assigns all people as @people" do    person = Person.create! valid_attributes...
How many hashes?describe "GET index" do  it "assigns all people as @people" do    person = Person.create! valid_attributes...
http://patshaughnessy.net/ruby-under-a-microscope
Why Hashes Will Be Faster in Ruby 2.0
Why Hashes Will Be Faster in Ruby 2.0
Upcoming SlideShare
Loading in...5
×

Why Hashes Will Be Faster in Ruby 2.0

1,708

Published on

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.

Published in: Technology, Sports
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,708
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
18
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \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

    1. 1. Why Hashes Will Be Faster in Ruby 2.0Pat Shaughnessyhttp://patshaughnessy.net@pat_shaughnessyMcKinsey & Co.
    2. 2. h = {}=> {}h[:key] = value=> "value"h[:key]=> "value"
    3. 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. 4. 0 1 2 3 4 5 6 7 8 9 10 st_table_entry hash key record next
    5. 5. func(key) = hash
    6. 6. :key.hash=> 4321153767602860351"abcdef".hash=> -1182018534040789564
    7. 7. hash % 11 = bin index
    8. 8. 0 1 2 3 4 5 6 7 8 9 10 st_table_entry hash key record next
    9. 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. 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. 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. 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. 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. 14. Hashes in Ruby 2.0
    15. 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. 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. 17. Hashes are Arrays!
    18. 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. 19. 6% improvement0 Loading 10,000 small records
    20. 20. 2.6% improvementRails App: Ruby Execution Time
    21. 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. 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. 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. 24. http://patshaughnessy.net/ruby-under-a-microscope
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×