-
Notifications
You must be signed in to change notification settings - Fork 79
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
feat(EC22): Implementing Avoid Method Usage for Only Basic Operations #148
feat(EC22): Implementing Avoid Method Usage for Only Basic Operations #148
Conversation
1e55bf0
to
888223c
Compare
888223c
to
8ae4967
Compare
Kudos, SonarCloud Quality Gate passed! |
Hi I think this rule does not apply to Java, because the JVM does the optimisation at runtime. |
@wokier Thanks for your review.
|
Issue #131 |
I know that the JVM is able to rearrange the program structure to simplify it. In this case, it is called inlining. I have found this documentation that explains it https://blog.jdriven.com/2019/11/hotspot-jvm-optimizations/ I agree that this rule does apply to other language, especially interpreted ones, where the method will be an extra step.
My conclusion is that this proposed rule would reduce the readability (in term of domain richness) and provide near to zero optimization. |
This PR has been automatically marked as stale because it has no activity for 30 days. |
Hi @moabidi91, |
This PR has been automatically marked as stale because it has no activity for 30 days. |
Hi @moabidi91, |
EDIT : wrong conclusion with compilation demonstration, please see next comments to complete discussion I found another recent benchmark (september 2023) by baeldung ( a Java reference on web) : https://www.baeldung.com/jvm-method-inlining My local tests show us that by default there is no optimization. Here is decompiled Class file with IntelliJ with JDK 11 : Here is decompiled Class file with another decompilation tool (JD-GUI app) : In conclusion, I don't think optimizations are systematic with JIT compiler and this optimization is maybe usefull. |
Hello |
Hi @wokier,
Thus, I don't think this rule is very relevant to make our code greener and we should let JIT optimze our code (with "inline" system). Maybe, what we can do in our code to help JIT optimizations is to split our algorithms like said in th quot above @cyChop @jycr @MP-Aubay @jhertout @glalloue, your point of view ? |
Many interesting facts and opinions. This is one of those times when finding the right balance between eco-friendliness and maintainability must be found. I fully agree with both those statements:
I'll also say this:
This is only food for thought, not a definite answer of course. About testing which code is best:
True, but a benchmark could be useful, if we can measure the CPU, memory and bandwidth used. It could help for all our rules, and if we can do something generic with Docker (for instance), we probably could apply it to all languages. It could also help us arbiter all rules, decide if they're relevant or not, provide figures to explain our rule choices and so on. I've thought (months ago) of setting up something, probably with Docker and Scaphandre, to compare two snippets of code. ETSDiff might be a nice solution for that, too. Maybe something we could build on GreenFrame. Unfortunately, I've lacked the time until now, and things are not getting better on that front. This might actually be a third challenge at the ecoCode: we need a way to evaluate our rules, probably measure the energy gain of compliant vs non-compliant code. Teams compete to create a tool, and we can hope to build something streamlined from the participations. Just a way to kickstart it. One final consideration we might have is: since which version this optimization has been implemented, and do we want to add a rule that applies only to the affected versions of Java? (I've wondered about this for loops and StringBuilders: rule has been deprecated because of optimizations in recent Java, but it still applies to some older JDKs and I know clients who still are hosting Java 7 code.) |
decision inside core-team : we have to measure and find real use cases to prove this rule |
Description :
Using methods for basic operations consumes additional system resources.
The interpreter must in effect and solve the objects and then the methods, just to carry out these simple operations of the language.
Implementation
What's a basic operation in Java?
A "basic operation" involve the use of arithmetic, comparison, and logical operators.
These operators are used either in Binary expression or Unary expression and also in Conditional exprssion using ternary operator.
So technically, using Java Sonar's API, an expression which is instance of BinaryExpressionTree or UnaryExpressionTree or is Tree.Kind.CONDITIONAL_EXPRESSION => represents a basic operation !
What kind of methods should be targeted ?
Each method having a return statement of a basic operation
What this Rule do not cover ?
The below example of code contains an assignment ecpression then a return statement :
As there is an unnecessarily assignement , it should be reported by another Rule.
So respecting Single Resp. Principle of SOLID and regarding the scope of EC22 this practice wont be reported despite the presence of a basic operation.
Same case could be detected with mutation.