ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: usa@garbagecollect.jp
To: ruby-core@ruby-lang.org
Subject: [ruby-core:71973] [Ruby trunk - Feature #11788] New ISeq serialize binary format
Date: Wed, 09 Dec 2015 05:14:31 +0000	[thread overview]
Message-ID: <redmine.journal-55382.20151209051431.6e6c5e0c88ef4a40@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11788.20151208105615@ruby-lang.org

Issue #11788 has been updated by Usaku NAKAMURA.


Koichi Sasada wrote:
>  Updated.

Thanks.


>  I'm not sure how detail description is needed on an entry.

I'm not sure, too.
But I can say that if a feature introduces a known security risk, users must be informed with documentations.


----------------------------------------
Feature #11788: New ISeq serialize binary format
https://bugs.ruby-lang.org/issues/11788#change-55382

* Author: Koichi Sasada
* Status: Assigned
* Priority: Normal
* Assignee: Koichi Sasada
----------------------------------------
# Abstract

I wrote a new RubyVM::InstructionSequence (ISeq) object serializer and de-serializer binary format.
Matz had approved to introduce this feature to Ruby 2.3 as *experimental* feature.
So I'll commit them.

There are two methods to serialize and de-serialize.

* RubyVM::InstructionSequence#to_binary_format returns binary format data as String object.
* RubyVM::InstructionSequence.from_binary_format(data) de-serialize it.

The goal of this project is to provide "machine dependent" binary file to achieve:

* fast bootstrap time for big applications
* reduce memory consumption with several techniques

"Machine dependent" means you can't migrate compiled binaries to other machines.

They are not goals of this project:

* packing scripts to one package
* migrate obfuscate binary to other node to hide source code

To achieve such goals, we need to consider compatibility issues such as `__FILE__`, `__dir__`, `DATA`, and so on (for example, consider about this code: `Dir.glob(File.join(__dir__, '*.rb')`).

This proposal doesn't contain "how to store compiled binaries".
For example, Rubinius makes *.rbc file automatically.
However, Matz does not like such automatic compilation.

So that my proposal only show user storage class interface.
People can try to make your own ISeq binary storage.

For example,

* making a compiled binary files automatically in same directory of script files like Rubinius,
* store compiled binaries in some DB
* make storage data structure in your own.

I wrote several samples:

* dbm: use dbm
* fs: [default] use file system. locate compiled file in same directory of script file like Rubinius. foo.rb.yarb will be created for foo.rb.
* fs2: use file system. locate compiled file in specified directory.
* nothing: do nothing.

You can see my sample implementation:
https://github.com/ko1/ruby/blob/iseq_p1/sample/iseq_loader.rb

The key interface is `RubyVM::InstructionSequence.load_iseq(fname)`.
When MRI try to load any script named `fname`, then call this method with `fname` if defined.
The return value is an ISeq object, then MRI use this ISeq object instead of parsing/compiling `fname` file.

Note that this proposal is "experimental".
These interfaces are only for experiments.
For example, if we want to use several binary storage,
this interface doesn't support multiple storage (lack of extensibility).

# Current status

The current implementation is not matured because the binary size is very big because pointer size consumes 32/64 bits.
It is easy to reduce, but I remain this weak point.

Now, one goal "reduction of memory consumption" is not achieved because no techniques are introduced to share/unload or something.
This is future work.

# Evaluation

Several evaluation results:

## resolv.rb

Try to load resolv.rb 1,000 times (and remove Resolv class each time).

```
compile     12.360000   0.310000  12.670000 ( 13.413011)
compile     12.120000   0.300000  12.420000 ( 13.195313)
compile     12.230000   0.270000  12.500000 ( 13.242140)

eager load
load         3.750000   0.180000   3.930000 (  3.918169)
load         4.000000   0.170000   4.170000 (  4.178442)
load         4.120000   0.200000   4.320000 (  4.320233)

lazy load
load         2.410000   0.090000   2.500000 (  2.609716)
load         2.280000   0.210000   2.490000 (  2.518892)
load         2.310000   0.110000   2.420000 (  2.419687)
```

3.25 times faster than normal compilation.
If we use lazy loading technique, it is 5.2 times faster.

## fileutils.rb

Try similar to resolv.rb.

```
                 user     system      total        real
compile      8.540000   0.130000   8.670000 (  8.703615)
compile      8.540000   0.150000   8.690000 (  8.693870)
compile      8.430000   0.120000   8.550000 (  8.547480)

eager load
load         4.470000   0.150000   4.620000 (  4.659934)
load         4.500000   0.140000   4.640000 (  4.640365)
load         4.610000   0.100000   4.710000 (  4.708825)

lazy load
load         3.510000   0.140000   3.650000 (  3.694146)
load         3.470000   0.130000   3.600000 (  3.609040)
load         3.550000   0.150000   3.700000 (  3.831015)
```

Only 1.8 times faster (eager) and 2.4 times faster (lazy).
This is because the initialization of FileUtils class run long time.
It uses module_eval(str) to add methods.

## Simple rails application

run `time rails r ''` on simple Rails application (https://github.com/ko1/tracer_demo_rails_app tracers are disabled).

```
compile:
real    0m2.049s
user    0m1.601s
sys     0m0.402s

eager:
real    0m1.544s
user    0m1.094s
sys     0m0.422s

lazy:
$ time rails r ''
real    0m1.536s
user    0m1.112s
sys     0m0.388s
```

Not so impressive result. It seems there are many initialization code.




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

  parent reply	other threads:[~2015-12-09  4:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-11788.20151208105615@ruby-lang.org>
2015-12-08 10:56 ` [ruby-core:71943] [Ruby trunk - Feature #11788] [Open] New ISeq serialize binary format ko1
2015-12-08 11:45   ` [ruby-core:71945] " Eric Wong
2015-12-11  8:15   ` [ruby-core:72052] " Vít Ondruch
2015-12-11  8:26     ` [ruby-core:72053] " SASADA Koichi
2015-12-08 12:55 ` [ruby-core:71947] [Ruby trunk - Feature #11788] " ko1
2015-12-09  0:53 ` [ruby-core:71962] [Ruby trunk - Feature #11788] [Assigned] " ko1
2015-12-09  4:41 ` [ruby-core:71970] [Ruby trunk - Feature #11788] " usa
2015-12-09  5:05   ` [ruby-core:71972] " SASADA Koichi
2015-12-09  5:14 ` usa [this message]
2016-04-11  6:28 ` [ruby-core:74876] [Ruby trunk Feature#11788][Closed] " ko1

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-55382.20151209051431.6e6c5e0c88ef4a40@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).