jruby包含java类性能与包含包的性能
我有一个 jruby Rails 应用程序,lib 中有一个 ruby 模块,它是我的 java 对象的名称空间,所以我不会有冲突。
我想知道在该模块中包含特定类和包含包之间有什么区别。我已经包含了下面的示例代码。
在控制台中,对于示例 1,当我说 MyMod:: 并点击选项卡时,它有(例如)101 个方法和类选项,其中 MyMod::MyClass 就是其中之一。
对于示例 2,当我点击 MyMod:: 和选项卡时,它只有 100 个方法/类选项,并且不包含 MyClass。如果我然后去引用 MyMod::MyClass,然后再次运行该 MyMod:: 选项卡,我现在有 101 个选项,并且列出了 MyClass。
这是我的问题。在我的模块中立即引用这些类(如示例 1)与让它们按需加载(如示例 2)之间有什么区别。如果我有一个包含大约 20 个类的包,我使用的是按需加载还是预先加载它们,以及按需加载是否有任何开销,如示例 2
示例代码:
示例 1
module MyMod
MyClass = Java::my.package.MyClass
....
end
vs 示例 2
module MyMod
include_package "my.package"
end
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你真的要使用一个类,那么在某些时候你会付出一些代价。专门加载还是按需加载并不重要。我认为对于你的两个例子,如果你确实使用
MyClass
,那么没有什么区别。另一方面,如果MyClass
从未使用过,那么示例 1 显然是浪费了一些东西。另外,
include_package
并没有真正将整个包拉进来,而是有点像在需要类时建立搜索范围。一般不建议使用include_package
。请参阅 JRUBY-2376 对于它所存在的问题。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, ifMyClass
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 useinclude_package
. See JRUBY-2376 for problems it has.我得到的结果略有不同。也许您正在使用不同的自动完成器?
至于你的问题,我不太清楚。但如果我不得不猜测,我会说,由于 ruby 是动态的,这两种方法都不会预先加载任何内容。
I got slightly different results. Perhaps you're using a different auto-completer?
As to your question, I don't know for sure. But if I had to guess, d'd say that as ruby is dynamic, neither approach loads anything up-front.