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

[enhancement] : Implement a multi-pass analysis #58

Closed
swayamraina opened this issue Jun 16, 2020 · 6 comments
Closed

[enhancement] : Implement a multi-pass analysis #58

swayamraina opened this issue Jun 16, 2020 · 6 comments
Labels
enhancement New feature or request legacy-java Issues/PR related to older PiranhaJava implementation

Comments

@swayamraina
Copy link
Contributor

while working on #57 and #35 i noticed we do not clean up empty methods.

Ideally we should delete methods which result in no-code and also delete their references in parent methods

@mkr-plse mkr-plse added enhancement New feature or request legacy-java Issues/PR related to older PiranhaJava implementation labels Jun 16, 2020
@mkr-plse
Copy link
Contributor

mkr-plse commented Jun 16, 2020

This is part of a broader problem. For PiranhaJava, multi-pass analysis is not yet implemented.

  1. So, if a refactoring results in a method becoming empty, or it returning a constant true or false, the callers have to be refactored appropriately.
  2. Similarly, if the return value of a feature flag API is stored in a variable and re-used elsewhere as part of some other conditional expression, that also needs to be refactored appropriately. For example, see code given in point 4 in Extensions for PiranhaJava #28.

I am increasing the scope of this issue to address these two issues.

@mkr-plse mkr-plse changed the title [enhancement] : remove empty (no-code) methods [enhancement] : Implement a multi-pass analysis Jun 16, 2020
@mkr-plse mkr-plse added this to the java-multipass-analysis milestone Jun 22, 2020
@romaninozemtsev
Copy link

@mkr-plse @swayamraina is there high level explanation on what needs to be done for a multi pass analysis? may be that could help with contributions.

@mkr-plse
Copy link
Contributor

mkr-plse commented Dec 6, 2021

In the current single pass analysis (in PiranhaJava), the code associated with the feature flags based on the configured APIs is removed. However, the code that becomes dead code due to the deletions in this pass are not deleted by the current implemenation of PiranhaJava. The goal of the multi pass analysis is to be able to identify such dead code and delete them. This has to be a multi-pass analysis (as opposed to a two pass analysis) because deletions in the second pass can potentially result in more dead code, and enable further deletions. A few scenarios:

  1. Deletion of the code in the first pass can result in unused functions that used to be called from within the deleted code.
  2. Same case as above, but unused variables.
  3. Wrapper functions that return a boolean constant due to the updates in the first pass. This requires the call sites to be updated, along with deletion of the wrapper functions.
  4. Same case as above, except when variables are assigned with a boolean constant value. This requires the usages of the variables to be refactored.

For a stale flag STALE_FLAG, assume it is disabled. Then the refactorings should be as follows:

Case 1:

...
if(isToggleEnabled(STALE_FLAG)) {
    fooOnlyCalledFromHere(); 
} else {
    bar(); 
}
...

void fooOnlyCalledFromHere() { 
...
}

should be refactored to

...
bar()
...

Currently, it is refactored as

...
bar(); 
...

void fooOnlyCalledFromHere() { 
...
}

Case 2:

private boolean willBecomeUnused = false;
...
if(isToggleEnabled(STALE_FLAG)) {
    willBecomeUnused = ... 
} else {
    bar();
}
...

should be refactored to

...
bar()
...

Currently, it is refactored as

private boolean willBecomeUnused = false;
...
bar(); 
...

Case 3:

...
if(shouldBeRefactored()) {
    somecall(); 
} else {
    bar(); 
}
...

boolean shouldBeRefactored() { 
    return isToggleEnabled(STALE_FLAG); 
}

should be refactored to

...
bar()
...

Currently, it is refactored as

...
if(shouldBeRefactored()) {
    somecall(); 
} else {
    bar(); 
}
...

boolean shouldBeRefactored() { 
    return false;
}

The above are a few examples that we see more often in the refactorings that requires additional manual changes and motivates the need for a multi-pass analysis.

@romaninozemtsev Please let me know if that clarifies the problem.

@himanshuguptan
Copy link

@mkr-plse @romaninozemtsev Do we have any updates on this milestone for allowing multi-pass refactoring for PiranhaJava?

@mkr-plse
Copy link
Contributor

mkr-plse commented May 9, 2022

@himanshuguptan We are working on making this much easier with a newer implementation of Piranha. See here. Please check it out as we expect it to be very customizable and adaptable for various use cases. cc @ketkarameya

@ketkarameya
Copy link
Collaborator

ketkarameya commented Aug 7, 2022

@swayamraina @himanshuguptan we have developed the initial version of polyglot piranha. Let us know, if it is performing the "deep" cleans that you want.
If it is not, please feel free to open a new issue describing the expected deep clean. We could potentially add new rules to polyglot piranha to perform the desired deep cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request legacy-java Issues/PR related to older PiranhaJava implementation
Projects
None yet
Development

No branches or pull requests

5 participants