Announcement (2017-05-07): www.ruby-forum.com is now read-only since I
unfortunately do not have the time to support and maintain the forum any
more. Please see rubyonrails.org/community and ruby-lang.org/en/community
for other Rails- und Ruby-related community platforms.

1) When using jruby code in a java project by calling
"evalScriptlet" are there any significant overheads involved in the
call?
2) Is it practical and efficient to use only a java main class with
the rest of the project being a large jruby code base?
3) Is this technique as good as any for getting jruby code into a jar?
Thanks
Paul F.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email

Paul, this is how I run all of my JRuby code. It's embedded in a
larger system that calls out to the ruby code via a Java main. Answers
inline below.
On Nov 19, 2008, at 9:19 AM, Paul F. wrote:
> 1) When using jruby code in a java project by calling> "evalScriptlet" are there any significant overheads involved in> the call?
I've been through the source a number of times and don't really see
too much overhead. The JRuby developers have provided a reasonable API
for calling ruby scripts from java. The basics are like so:
runtime = JavaEmbedUtils.initialize(loadPaths); // where
loadPaths is where your ruby code is found
RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();
rootRubyObject = evaler.eval(runtime, bootstrap); // where bootstrap
is a small ruby wrapper for calling in to your main ruby code
// invoke a ruby method via the rootRubyObject setup prior
public Object call(String method_name, Object obj) {
Object return_obj = null;
return_obj = JavaEmbedUtils.invokeMethod(runtime,
rootRubyObject, "execute_with_param", new Object[] {method_name, obj},
null);
return return_obj;
}
I used the RubyLauncher code available on the wiki as a starting point
and did a little customization. Check the wiki for the details.
There are two "overhead" issues to be aware of when calling from java
into ruby.
One, any parameters passed in to #invokeMethod have to be converted to
ruby objects before they can be passed on. This could be significant
if you are passing a lot of data, so I usually structure my code so
all calls in to ruby contain zero parameters.
Two, any data returned from the ruby method call has to be converted
from a ruby object back to a java object. As a performance tweak, my
methods do not return anything (they all return nil). Try to structure
your code similarly to avoid these object conversions.
Luckily the JRuby API includes multiple #invokeMethod overloads (and
similar other methods) that do varying amounts of work depending on
the parameters passed. Look through the JRuby source for guidance.
> 2) Is it practical and efficient to use only a java main class> with the rest of the project being a large jruby code base?
Yes, it is very practical. My one caution concerns profiling your ruby
code. I haven't found a good way to profile my ruby code's
performance. Java profilers will profile the JRuby java code that is
generated from your ruby code but it doesn't give a lot of hints for
improving the source ruby. Similarly, you can use the "profiler"
library included with ruby but it slows down execution so much that
you need to be careful with it. Most of the "low overhead" ruby
performance profilers contain a lot of C code so they can't be used;
you need to stick with a pure ruby profiler unless someone ports the C
profiler code to java.
> 3) Is this technique as good as any for getting jruby code into a> jar?
All of my code is packaged into jars and distributed. It works fine.
Hope this helps.
cr
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email

Hi Chuck,
Thank you for an excellent reply and coverage of the subject.
It seemed to me, that as this method is used in Rawr & Monkeybars, it
should be a good approach and you have confirmed this.
In the past I have concentrated on trying to work from the JRuby project
side, but I think I have now seen the light. :-P
Regards
Paul F.
Chuck R. wrote:
> I've been through the source a number of times and don't really see> public Object call(String method_name, Object obj) {> There are two "overhead" issues to be aware of when calling from java> your code similarly to avoid these object conversions.> code. I haven't found a good way to profile my ruby code's>> 3) Is this technique as good as any for getting jruby code into a jar?>> http://xircles.codehaus.org/manage_email>>>
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email