Issue #14460 has been updated by tenderlovemaking (Aaron Patterson).


Hi,

I didn't see that ticket, but I made a comparison now:

![Updated heap comparison](https://user-images.githubusercontent.com/3124/36166473-7c734e52-10a7-11e8-92c9-63fb7cfee6ab.png)

They are very close.  My patch ends slightly larger 56867086 bytes vs 56502412 bytes.  I like you patch much more.  I think Matz offered you commit access in that ticket.  Are you going to accept it?  If so, I think you should apply that patch.  If not, I will apply your patch.

Thanks!

----------------------------------------
Feature #14460: Speed up `require` and reduce memory usage
https://bugs.ruby-lang.org/issues/14460#change-70315

* Author: tenderlovemaking (Aaron Patterson)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Hi,

I've attached a patch that speeds up require and reduces memory usage.  Before this patch, we could copy copy keys using `rb_strdup` for the `loaded_features_index` table.  This patch changes the hash population functions to just index in to the Ruby strings without copying them.  Some of the keys for the index require that the file names do not have a file extension.  For that case, I create a new string (so that it's NULL padded), then index in to that string.  Rather than coping the string for each `/` in the path, this patch just indexes in to the string.  It relies on the loaded features array and the new array I introduced for liveness.  `get_loaded_features_index` handles cache invalidation, so I believe it is safe to rely on these two arrays for liveness of the `loaded_features_index` keys.

I tested memory usage by booting a Rails application in production mode (the same test I'm doing with the ISeq patches), and found that it saves about 4.2% memory (59580203 bytes in trunk, 57057395 bytes with this patch).  I also found that this patch eliminated 76212 system calls (presumably calls to malloc / free for the hash table keys).  Finally, I found that this patch sped up `require` by about 35%.

Here is a graph of the memory usage:

![Memory usage during boot](https://user-images.githubusercontent.com/3124/36056091-677a0b56-0db6-11e8-8a98-304e9450883e.png)

Each sample is a call to malloc, so you can see from the graph that this patch reduces memory usage and system calls.

I ran the `bm_require.rb` benchmark to compare times:

~~~
[aaron@TC-3 ruby (no-dup-string)]$ make benchmark OPTS='-p bm_require.rb'
/Users/aaron/.rbenv/shims/ruby --disable=gems ./benchmark/driver.rb -v \
	            --executables="/Users/aaron/.rbenv/shims/ruby --disable=gems -I./lib -I. -I.ext/common --disable-gem; built-ruby::./miniruby -I./lib -I. -I.ext/common  --disable-gem" \
	            --pattern='bm_' --directory=./benchmark -p bm_require.rb
2018-02-09 16:25:37 -0800
target 0: ruby 2.6.0dev (2018-02-07 trunk 62271) [x86_64-darwin17] at "/Users/aaron/.rbenv/shims/ruby --disable=gems -I./lib -I. -I.ext/common --disable-gem"
target 1: built-ruby (ruby 2.6.0dev (2018-02-07 trunk 62271) [x86_64-darwin17]) at "./miniruby -I./lib -I. -I.ext/common  --disable-gem"
measure target: real

-----------------------------------------------------------
require

$:.push File.join(File.dirname(__FILE__), "bm_require.data")

1.upto(10000) do |i|
  require "c#{i}"
end

$:.pop

ruby 2.6.0dev (2018-02-07 trunk 62271) [x86_64-darwin17]	3.043203999997786
built-ruby	1.9892130000007455

-----------------------------------------------------------
raw data:

[["require", [[3.043203999997786], [1.9892130000007455]]]]

Elapsed time: 5.036855 (sec)
-----------------------------------------------------------
benchmark results:
Execution time (sec)
name	ruby 2.6.0dev (2018-02-07 trunk 62271) [x86_64-darwin17]	built-ruby
require	                                                   3.043	     1.989

Speedup ratio: compare with the result of `ruby 2.6.0dev (2018-02-07 trunk 62271) [x86_64-darwin17]' (greater is better)
name	built-ruby
require	     1.530

Log file: bmlog-20180209-162537.60174.tsv
~~~

---Files--------------------------------
0001-Use-shared-substrings-in-feature-index-cache-hash.patch (6.05 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>