3

Ruby 2.0.0 has made a big difference for us in load time. I was wondering if we could get even better load times by caching interpreted ruby code data via c-extension. The idea came to me while reading pickaxe section "Embedding a Ruby Interpreter". Here is a snippet from there with some caching pseudocode added.

#include "ruby.h"

main() {
  /* ... our own application stuff ... */
  ruby_init();
  ruby_script("embedded");

  /* HERE IS THE PSEUDOCODE FOR CACHING */
  if (ruby_file_previously_loaded_and_cached())
      load_marshalled_ruby_data();
  else
      rb_load_file("start.rb");
      cache_all_the_ruby_data();
  end

  while (1) {
    if (need_to_do_ruby) {
      ruby_run();
    }
    /* ... run our app stuff */
  }
}

Is something like this possible?

btd
  • 404
  • 3
  • 12
  • Didn't find any such caching method described here: https://github.com/ruby/ruby/blob/trunk/README.EXT. Search continues... – btd Jun 20 '13 at 00:08

1 Answers1

0

You can't cache "interpreted code". You could only cache the "bytecode". I don't believe it would make a big difference though. Judging from how many files there are in Rails codebase, and it gets loaded pretty quickly, it doesn't seem like it is particularly slow to translate ruby code into bytecode.

I made an mruby "compiler" (https://github.com/mrbrdo/mruby_cc) and it is not significantly faster than the usual interpreted version. This is basically as far as you can go with "caching".

mrbrdo
  • 7,968
  • 4
  • 32
  • 36