Issue #12142 has been updated by Vladimir Makarov.


Yura Sokolov wrote:
> Murmur is not used for Hash, cause it is target for hashDoS - it has seed independent collisions.

I did no write that murmur is used for Hash.  It is still used internally.

> City64 also has seed independent collisions.

City64 is 2 faster than murmur so it is better to use it where murmur is used.  Ruby murmur code is also broken (bad hash quality) but looking at your changes in murmur MRI code you probably fixed it. 

> That is why SipHash were born and adopted by community.
> 
> http://emboss.github.io/blog/2012/12/14/breaking-murmur-hash-flooding-dos-reloaded/
> 
> But SipHash could be relaxed:
> 
> - currently it is SipHash24 - 2 rounds for 8byte block and 4 rounds for finalization
> - but even SipHash author confirms that for internal hash table (ie when hash sum is not exposed
>   to attacker) SipHash13 is just enough.
>   https://github.com/rust-lang/rust/issues/29754#issuecomment-156073946
> 
> Single call to st_hash in hash.c is just to combine two hashsums (calculated with SipHash) into one.
> 
> All usage of st_init_strtable are not performance critical.
> 

  Yura, I suspect you missed my point.  What I proposed is to use also City24 in cases where siphash24 is currently used and use siphash24 only when the attack is on the way.  Therefore I added a code to the tables to recognize the attack and switch to crypto-level hash function which in reality will be extremely rare.  City24 is 6 times faster than siphash24, therefore Hash will be faster and still crypto-strong.  Moreover you can use other crypto-level hash functions *without* losing the speed for real world scenarios.

> > I made the entries array is cyclical to exclude overhead of table
> compaction or/and table size change for usage the hash tables as a
> queue.
> 
> I doubt using hash table as a queue is a useful case. But I could be mistaken.
> And cyclic allocation doesn't solve LRU usecase at all :-(
> 

Still it is nice to have because it costs practically nothing (one additional mask operation whose tiny latency time will be hidden between execution of parallely executed insns on modern superscalar OOO processors).
 
> > also changed the specialized hash function (rb_num_hash_start) used for bm_hash_ident tests
> 
> I've seen you do it. Great catch!
> I also fixed it in other way (cause I don't use perturb I must be ensure all bits
> are mixed into lower bits).
> 
> > I also tried double probing
> 
> It takes me a time to realize that you mean `quadratic probing` :-)
> `double probing` may be: test slot, then test neighbor, do long jump and repeat.
> It is not wildly used technique.
> 

Sorry, my memory failed me again.  That was quadratic probing which you proposed to use for open-addressing tables.

> Do you configure with `--with-jemalloc` ?
> `trunk` is much faster with jemalloc linked, and it is hard to beat it by performance.
> 

Not yet but will do.

> Unfortunately, Redmine still doesn't work with your branch, so I cann't benchmark it.

Thanks.  I'll investigate this.  Unfortunately I am too far away from web development and have no experience with such applications but I will learn it.

The most exciting thing for me is that better hash tables can improve the performance of real applications.  Before this, I looked at the table work more as an exercise to become familiar with MRI before deciding to do a real performance work.



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

* 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)


-- 
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>