Issue #12142 has been updated by Vladimir Makarov.


Vladimir Makarov wrote:
> Koichi's investigation reported that small hash tables are majority during
> work of Ruby on Rails:
> 
> https://gist.github.com/ko1/dda8243134bc40f7fc05e293abc5f4b2#file-report-md
> 
> Memory footprint of cloud applications like Ruby on Rails is important
> topic.
> 
> Since Koichi's report some changes have been done in my
> (https://github.com/vnmakarov/ruby/tree/hash_tables_with_open_addressing)
> and Yura's
> (https://github.com/funny-falcon/ruby/tree/st_table_with_array3)
> implementations.
> 
> Here are memory consumption graphs.  The data produced by Koichi's
> script
> 
> https://gist.githubusercontent.com/ko1/dda8243134bc40f7fc05e293abc5f4b2/raw/dfccc2cc5e6f4f749c8a9390eae3104da84eb32f/stbench.rb
> 
> on 4.2 GHz i7-4790K under FC24.
> 
> ![](https://bugs.ruby-lang.org/attachments/download/6220/size16.png)
> ![](https://bugs.ruby-lang.org/attachments/download/6221/size256.png)
> ![](https://bugs.ruby-lang.org/attachments/download/6222/size60000.png)
> 
> On these graphs
> 
> * 'vlad current' means my current variant (https://github.com/vnmakarov/ruby.git,
>   branch hash_tables_with open addressing,
>   f4d2bf3ab78e59118e5dd89bcc5c3d8c4371ed35).
> 
> * 'vlad april' means my April variant without the last two commits to
>   st.c.
> 
> * trunk means the current trunk (03c9bc2)
> 
> * yura means latest Yura's variant
>   (https://github.com/funny-falcon/ruby.git, branch
>   st_table_with_array3, 39d9b2c3658c285fbe4b82edc1c7aebaabec6aaf) of
>   tables with open addressing.  I used a 64-bit variant, there is no
>   practically difference in performance and memory consumption with
>   32-bit variant.
> 
> Here are the tables speed improvements relative to the trunk on MRI hash benchmarks:
> 
> 
> ```
>                 Yura    My April  My current
> bighash         1.716   1.509     1.443
> hash_aref_dsym  1.019   1.018     0.994
> hash_aref_dsym_l1.394   1.414     1.377
> hash_aref_fix   0.984   1.041     1.015
> hash_aref_flo   1.713   1.832     1.828
> hash_aref_miss  1.117   1.191     1.131
> hash_aref_str   1.177   1.225     1.222
> hash_aref_sym   0.959   1.015     1.012
> hash_aref_sym_l 1.047   1.066     1.042
> hash_flatten    1.165   1.095     1.087
> hash_ident_flo  1.005   0.949     0.958
> hash_ident_num  0.932   0.979     0.954
> hash_ident_obj  0.886   0.934     0.919
> hash_ident_str  0.919   0.942     0.919
> hash_ident_sym  0.983   1.000     0.967
> hash_keys       3.024   3.039     3.073
> hash_long       0.751   1.483     1.486
> hash_shift      1.363   1.371     1.346
> hash_shift_u16  1.388   1.392     1.359
> hash_shift_u24  1.344   1.345     1.310
> hash_shift_u32  1.330   1.326     1.290
> hash_small2     0.918   1.016     1.043
> hash_small4     0.928   1.074     1.048
> hash_small8     1.581   2.268     1.894
> hash_to_proc    1.024   1.067     1.051
> hash_values     2.801   2.833     2.822
> vm2_bighash*    2.669   3.261     3.001
> Average         1.33841 1.43278 1.39226
> ```
> 
> The data are obtained by running the following script on the same machine
> used exclusively for benchmarking:
> 
> ```
> ruby ../ruby/benchmark/driver.rb -p hash -r 3 -e trunk::trunk/miniruby -e yura::yura/miniruby -e yura::yura64/miniruby -e current::./miniruby 2>/dev/n\
> ull|awk 'NF==4 && /hash/ {s1+=$2;s2+=$3;s3+=$4;n++;print} END{print s1/n, s2/n, s3/n}'
> ```
> 
> Conclusions:
> 
> * My current variant requires less memory than April one. The current
>   variant is close to average memory consumption of Yura's one (see
>   the integral, a square of areas under the curves).
> 
> * Yura's curve is more smooth because of more frequent and smaller
>   rebuilding but probably it results in the speed decrease.
> 
> * There is trade-off in my implementation (and probably Yura's ones)
>   in table speed and its size.
> 
> * More size reduction in my tables by increasing table fill rate
>   (e.g. from 0.5 to 3/4) is not worth it as one bin size is 1/24 -
>   1/12 of one entry size for tables with less 2^15 elements.
> 
> * Unfortunately, it is not known what part of the overall footprint
>   belongs to the tables in Rails.  Hash tables might be a small part
>   in comparison with other Rails data.  Even if the tables are the
>   lion part, their elements and keys might require more memory than
>   table entries.
> 
> * Therefore it is hard to say what trade-off in performance/table size
>   should be for Rails.  I personally still prefer my April variant.



----------------------------------------
Feature #12142: Hash tables with open addressing
https://bugs.ruby-lang.org/issues/12142#change-61254

* Author: Vladimir Makarov
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
~~~
 Hello, the following patch contains a new implementation of hash
tables (major files st.c and include/ruby/st.h).

  Modern processors have several levels of cache.  Usually,the CPU
reads one or a few lines of the cache from memory (or another level of
cache).  So CPU is much faster at reading data stored close to each
other.  The current implementation of Ruby hash tables does not fit
well to modern processor cache organization, which requires better
data locality for faster program speed.

The new hash table implementation achieves a better data locality
mainly by

  o switching to open addressing hash tables for access by keys.
    Removing hash collision lists lets us avoid *pointer chasing*, a
    common problem that produces bad data locality.  I see a tendency
    to move from chaining hash tables to open addressing hash tables
    due to their better fit to modern CPU memory organizations.
    CPython recently made such switch
    (https://hg.python.org/cpython/file/ff1938d12240/Objects/dictobject.c).
    PHP did this a bit earlier
    https://nikic.github.io/2014/12/22/PHPs-new-hashtable-implementation.html.
    GCC has widely-used such hash tables
    (https://gcc.gnu.org/svn/gcc/trunk/libiberty/hashtab.c) internally
    for more than 15 years.

  o removing doubly linked lists and putting the elements into an array
    for accessing to elements by their inclusion order.  That also
    removes pointer chaising on the doubly linked lists used for
    traversing elements by their inclusion order.

A more detailed description of the proposed implementation can be
found in the top comment of the file st.c.

The new implementation was benchmarked on 21 MRI hash table benchmarks
for two most widely used targets x86-64 (Intel 4.2GHz i7-4790K) and ARM
(Exynos 5410 - 1.6GHz Cortex-A15):

make benchmark-each ITEM=bm_hash OPTS='-r 3 -v' COMPARE_RUBY='<trunk ruby>'

Here the results for x86-64:

hash_aref_dsym       1.094
hash_aref_dsym_long          1.383
hash_aref_fix        1.048
hash_aref_flo        1.860
hash_aref_miss       1.107
hash_aref_str        1.107
hash_aref_sym        1.191
hash_aref_sym_long           1.113
hash_flatten         1.258
hash_ident_flo       1.627
hash_ident_num       1.045
hash_ident_obj       1.143
hash_ident_str       1.127
hash_ident_sym       1.152
hash_keys            2.714
hash_shift           2.209
hash_shift_u16       1.442
hash_shift_u24       1.413
hash_shift_u32       1.396
hash_to_proc         2.831
hash_values          2.701

The average performance improvement is more 50%.  ARM results are
analogous -- no any benchmark performance degradation and about the
same average improvement.

The patch can be seen as

https://github.com/vnmakarov/ruby/compare/trunk...hash_tables_with_open_addressing.patch

or in a less convenient way as pull request changes

https://github.com/ruby/ruby/pull/1264/files


This is my first patch for MRI and may be my proposal and
implementation have pitfalls.  But I am keen to learn and work on
inclusion of this code into MRI.

~~~

---Files--------------------------------
st-march31.patch (114 KB)
base.patch (93.8 KB)
hash.patch (4.48 KB)
strong_hash.patch (8.08 KB)
city.patch (19.4 KB)
new-hash-table-benchmarks.patch (1.34 KB)
size16.png (6.91 KB)
size256.png (6.95 KB)
size60000.png (7.59 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>