Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: try to load class from multiple class-loaders #202

Merged
merged 4 commits into from
Aug 28, 2024
Merged

Conversation

gudzpoz
Copy link
Owner

@gudzpoz gudzpoz commented Aug 26, 2024

Basically, we borrowed ClassUtils from Spring, which prioritizes Thread::getContextClassLoader over ClassUtils.class.getClassLoader(). It is fine in most situations. But sometimes, the library is loaded with an isolated classloader, yet provided with a system classloader in Thread::getContextClassLoader, which causes problems.

We simply retry with ClassUtils.class.getClassLoader() when failing to load a class. It is not the most efficient, but should be enough (if people cache the lookup results):

String = java.import('java.lang.String')
s = String('s')
-- instead of
s = java.import('java.lang.String')('s')

Also, I also think it is a good chance that we optimize our reflection usage. We were not caching the method lookup results (because Lua is dynamic, and we do need to check if every parameter matches (and convert them over) every single time.) Caching the results improves a bit for L.run("assert(big_num:intValue() == 1024") but we do need better profiling to get anything meaningful:

--- before
Benchmark                                             (lua)  Mode  Cnt  Score   Error  Units
MethodCallBenchmark.benchmarkObjectMethodCall       Lua 5.4  avgt    3  4.660 ± 0.234  us/op
MethodCallBenchmark.benchmarkObjectMethodCall        LuaJIT  avgt    3  3.932 ± 0.100  us/op
MethodCallBenchmark.benchmarkObjectMethodCall          LuaJ  avgt    3  4.003 ± 0.246  us/op
--- after
Benchmark                                             (lua)  Mode  Cnt  Score   Error  Units
MethodCallBenchmark.benchmarkObjectMethodCall       Lua 5.4  avgt    3  2.300 ± 0.038  us/op
MethodCallBenchmark.benchmarkObjectMethodCall        LuaJIT  avgt    3  1.698 ± 0.237  us/op
MethodCallBenchmark.benchmarkObjectMethodCall          LuaJ  avgt    3  1.673 ± 0.018  us/op

fixes #200

@codecov-commenter
Copy link

codecov-commenter commented Aug 27, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 94.02985% with 4 lines in your changes missing coverage. Please review.

Project coverage is 96.41%. Comparing base (1048aac) to head (6e0dd11).

Files Patch % Lines
...ava/src/main/java/party/iroiro/luajava/JuaAPI.java 93.22% 2 Missing and 2 partials ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #202      +/-   ##
============================================
- Coverage     96.55%   96.41%   -0.15%     
- Complexity      915      920       +5     
============================================
  Files            44       44              
  Lines          2469     2510      +41     
  Branches        286      295       +9     
============================================
+ Hits           2384     2420      +36     
- Misses           61       64       +3     
- Partials         24       26       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gudzpoz gudzpoz merged commit 0e4deae into main Aug 28, 2024
12 checks passed
@gudzpoz gudzpoz deleted the reflection branch August 28, 2024 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ClassNotFoundException When Importing a Java Class in LuaJava
2 participants