# Overhead using evalScriptlet

**URL:** <https://www.ruby-forum.com/t/overhead-using-evalscriptlet/153628>\
**Category:** JRuby\
**Created:** [November 19, 2008, 4:26pm UTC](https://www.ruby-forum.com/t/overhead-using-evalscriptlet/153628 "2008-11-19T16:26:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@paulf](https://www.ruby-forum.com/u/paulf)\
**Post date:** [November 19, 2008, 4:26pm UTC](https://www.ruby-forum.com/t/overhead-using-evalscriptlet/153628/1 "2008-11-19T16:26:49Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![paulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@paulf](https://www.ruby-forum.com/u/paulf)\
**Post date:** [November 19, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/overhead-using-evalscriptlet/153628/2 "2008-11-19T17:06:21Z")

</div>

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.

> 1. 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.

> 1. 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

```

---

<div class="post-metadata">

**Author:** ![paulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@paulf](https://www.ruby-forum.com/u/paulf)\
**Post date:** [November 20, 2008, 1:12am UTC](https://www.ruby-forum.com/t/overhead-using-evalscriptlet/153628/3 "2008-11-20T01:12:31Z")

</div>

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. 😛

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
> 
> > 1. Is this technique as good as any for getting jruby code into a jar?
> 
> [http://xircles.codehaus.org/manage\_email](http://xircles.codehaus.org/manage_email)

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```
