Search code examples
modulejrubyjrubyonrails

jruby include java class performance vs include a package


I have a jruby rails app with a ruby module in lib that is name-spacing my java objects so I don't have conflicts.

I'm wondering what the difference is between including the specific classes in that module and including the package. I've included the sample code below.

In the console, for example 1 when I say MyMod:: and hit tab, it has (for example) 101 methods and class options with MyMod::MyClass being one of them.

For example 2, when I hit MyMod:: and tab, it only has 100 method/class options and it doesn't contain MyClass. If I then go and reference MyMod::MyClass, then run that MyMod:: tab again, I now have 101 options and MyClass is listed.

Here's my question. What's the difference between referencing these classes immediately in my module à la example 1 vs having them load on demand like example 2. If I have a package with about 20 classes that I use, is it preferred to have them loaded on demand or up front, and is there any overhead to loading this on demand, à la example 2

Sample Code:

example 1

module MyMod
  MyClass = Java::my.package.MyClass
  ....
end

vs example 2

module MyMod
  include_package "my.package"
end

Solution

  • If you really are going to use a class, it is going to cost you something at some point. It doesn't matter whether loading it specifically or on-demand. I think for your 2 examples, if you do use MyClass, then there's no difference. On the other hand, if MyClass is never used, then example 1 is clearly wasting something.

    Also, include_package does not really pull the whole package in, but kind of like establishing a search scope when needing a class. Generally it is not recommended to use include_package. See JRUBY-2376 for problems it has.