Issue #12142 has been updated by Vladimir Makarov.

File st-march31.patch added

Eric Wong wrote:
> vmakarov / redhat.com wrote:
>  > I think I'll have a few patches when I am done with the hash
>  > tables: the hash table itself, hash functions, code for
>  > recognizing a denial attack and switching to stronger hash
>  > functions.  I am not sure that all (or any) will be finally
>  > accepted.
>  
>  I look forward to these and would also like to introduce some
>  new APIs for performance.
> 

Thank you for sharing this.  I am still working on the hash tables when I have spare time.

I already implemented variable indexes (8-, 16-, 32-, 64-bits).  It gave about 3% average improvement on MRI hash benchmarks.  I tried a lot and did some other changes too.  __builting_prefetch gave a good improvement while __builtin_expect did nothing.  So the current average improvement on MRI hash benchmarks is close to 40% on Intel Haswell and >55% on ARMv7 (using the right comparison of one miniruby vs. another miniruby).
 
>  One feature would be the ability to expose and reuse calculated
>  hash values between different tables to reduce hash function
>  overheads.
>  
>  This can be useful in fstring and symbol tables where the same
>  strings are repeatedly reused as hash keys.
>  
>  For example, I was disappointed we needed to revert r43870 (git
>  commit 0c3b3e9237e8) in [Misc #9188] which deduplicated all
>  string keys for Hash#[]=:
>  
>  	https://bugs.ruby-lang.org/issues/9188
>  
>  So, perhaps being able to reuse hash values between different
>  tables can improve performance to the point where we can dedupe
>  all string hash keys to save memory.
>  

The hash reuse would be nice.  Hash calculation can be expensive.  May be it even will permit to remove hash from the entries at least w/o losing performance.  I actually tried to remove hash and recalculate it when it is necessary but it gave worse performance (i also tried to put smaller hashes into bins to decrease cache misses -- it worked better but still worse than storing full hash in entries).

>  I am also holding off on committing a patch to dedupe keys
>  from Marshal.load [Feature #12017] because I hope to resurrect
>  r43870 before Ruby 2.4:
>  
>  	https://bugs.ruby-lang.org/issues/12017
>  
>  I had another feature in mind, but can't remember it at the
>  moment :x
>  
>  I doubt I'll have time to work on any of this until you're done
>  with your work.  My time for ruby-core is limited for the next
>  2-3 months due to vacations and other projects.

This project is holding me back too (it can be quite addicted but I feel I am already repeating the same ideas with small variations).  I'd like to move on to another project.  So I'll try to submit a major patch before the end of April in order you have time to work on the new code.

I am sending the current overall patch.  I think it is pretty close to what I'd like to see finally.  I hope the patch will help with your work.

The patch is relative to trunk at 7c14876b2fbbd61bda95e42dea82c954fa9d0182 which was the head about 1 month ago.



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

* 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--------------------------------
0001-st.c-use-array-for-storing-st_table_entry.patch (46.7 KB)
0001-st.c-change-st_table-implementation.patch (59.4 KB)
st-march31.patch (114 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>