From: funny.falcon@... Date: 2016-03-15T13:30:09+00:00 Subject: [ruby-core:74340] [Ruby trunk Feature#12142] Hash tables with open addressing Issue #12142 has been updated by Yura Sokolov. File 0001-st.c-change-st_table-implementation.patch added Good day, everyone. I'm presenting my (pre)final version of patch. It passes all tests and redmine works on it without issues. Vladimir, I must admit your original idea about rebuilding st_table_entries were correct, so there is no need in double-linked list. Ruby is not for huge hashes in low latency applications, and GC still ought to traverse whole hash with marking referenced objects. Differences from Vladimir's approach: - closed addressing, with fill factor close to 1, (so no need to keep counter for deleted entries and rebuild main hash array). - 32bit hashes and indices on 64bit platform - size is stored as index to static table, so allocation is not always 2x, but instead it is 1.5x and 1.33x - single allocation both for `entries` and `bins`, pointer points into middle of allocaction, `bins` array is on one side, and `entries` on other. Patch url https://github.com/ruby/ruby/compare/trunk...funny-falcon:st_table_with_array2.patch Also attached to ticket. Results for Redmine. Unfortunately, Redmine works with mistake under current Vladimir's branch, so there is no results for :-( There is 2-3% performance improvement. When configured with `--without-jemalloc`, patched version uses 12% less memory. When configured with `--with-jemalloc`, only 5% less memory used. (webserver is `puma`, database `postresql`, 5 issues in a list, 1 issue with 6 comments) ```` $ # trunk without jemalloc $ ab -n 1000 -c 10 http://localhost:3000/projects/general/issues Requests per second: 23.88 [#/sec] (mean) $ ab -n 1000 -c 10 http://localhost:3000/issues/1 Requests per second: 24.33 [#/sec] (mean) $ ps aux | grep puma yura 17709 60.6 1.4 1453416 242636 pts/31 Sl+ 15:44 1:33 puma 3.1.0 (tcp://0.0.0.0:3000) [redmine-trunk] $ # trunk + jemalloc $ ab -n 1000 -c 10 http://localhost:3000/projects/general/issues Requests per second: 28.25 [#/sec] (mean) $ ab -n 1000 -c 10 http://localhost:3000/issues/1 Requests per second: 28.27 [#/sec] (mean) $ ps aux | grep puma yura 32297 69.9 1.7 490356 285568 pts/31 Sl+ 15:09 2:23 puma 3.1.0 (tcp://0.0.0.0:3000) [redmine-trunk] $ # patch without jemalloc $ ab -n 1000 -c 10 http://localhost:3000/projects/general/issues Requests per second: 24.60 [#/sec] (mean) $ ab -n 1000 -c 10 http://localhost:3000/issues/1 Requests per second: 24.82 [#/sec] (mean) $ ps aux | grep puma yura 29489 60.5 1.3 1378392 213956 pts/34 Sl+ 15:52 2:02 puma 3.1.0 (tcp://0.0.0.0:3000) [redmine-st] $ # patch + jemalloc $ ab -n 1000 -c 10 http://localhost:3000/projects/general/issues Requests per second: 29.10 [#/sec] (mean) $ ab -n 1000 -c 10 http://localhost:3000/issues/1 Requests per second: 28.91 [#/sec] (mean) $ ps aux | grep puma yura 31774 45.6 1.6 482164 270936 pts/34 Sl+ 15:02 1:10 puma 3.1.0 (tcp://0.0.0.0:3000) [redmine-st] ```` ---------------------------------------- Feature #12142: Hash tables with open addressing https://bugs.ruby-lang.org/issues/12142#change-57461 * 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>