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

Remove listeners in RedoAction for memory efficiency #11839

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

melisolmez
Copy link

@melisolmez melisolmez commented Sep 26, 2024

fixes #11809

jabref/src/main/java/org/jabref/gui/undo/RedoAction.java

Approach used: WeakChangeListener

Mandatory checks

  • Change in CHANGELOG.md described in a way that is understandable for the average user (if applicable)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (for UI changes)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
The tool reviewdog already placed comments on GitHub to indicate the places. See the tab "Files" in you PR.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

You can check review dog's comments at the tab "Files changed" of your pull request.

@koppor
Copy link
Member

koppor commented Sep 26, 2024

Checkstyle says

Error: eckstyle] [ERROR] /home/runner/work/jabref/jabref/src/main/java/org/jabref/gui/undo/RedoAction.java:8:1: Wrong order for 'javax.swing.undo.CannotRedoException' import. [ImportOrder]

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
The tool reviewdog already placed comments on GitHub to indicate the places. See the tab "Files" in you PR.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

You can check review dog's comments at the tab "Files changed" of your pull request.

Copy link
Collaborator

@subhramit subhramit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor formatting changes requested. Otherwise LGTM.
Thank you for your contribution!

P.S. Do mark the mandatory checks that you have completed in the PR description when you file one (the ones that apply).

Comment on lines 14 to 22
* @implNote See also {@link UndoAction}
* @implNote
* See
* also
* {@link
* UndoAction}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this

Comment on lines 37 to 50
} catch (CannotRedoException ex) {
} catch (
CannotRedoException ex) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also fix the catch indent (not your fault, IntelliJ does this by default when we format the document).

@subhramit subhramit changed the title fix remove listeners in RedoAction for memory efficiency, used WeakCh… Remove listeners in RedoAction for memory efficiency Sep 26, 2024
activeLibraryTab.ifPresent(libraryTab ->
this.executable.bind(libraryTab.getUndoManager().getRedoableProperty()));
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty line is not necessary, is it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this empty line is for exists to separate lines 30 and 32 from each other. But I can remove.

@koppor
Copy link
Member

koppor commented Sep 27, 2024

@subhramit We should test this. This PR implements both suggestions of you (#11809 (comment)). I think, one needs to open 5 to 10 libraries, switch tabs back and forth, modifying the library and see if undo/redo is enabled properly.

@melisolmez For consistency reasons, this patch also nees to be ported to the UndoAction. Undo is the twin of Redo. --> https://github.com/JabRef/jabref/blob/8412e651b671a427c828924ce5698eb4c32f269d/src/main/java/org/jabref/gui/undo/UndoAction.java

@melisolmez
Copy link
Author

@subhramit We should test this. This PR implements both suggestions of you (#11809 (comment)). I think, one needs to open 5 to 10 libraries, switch tabs back and forth, modifying the library and see if undo/redo is enabled properly.

@melisolmez For consistency reasons, this patch also nees to be ported to the UndoAction. Undo is the twin of Redo. --> https://github.com/JabRef/jabref/blob/8412e651b671a427c828924ce5698eb4c32f269d/src/main/java/org/jabref/gui/undo/UndoAction.java

Okey. Should I send it to the same branch?

@subhramit
Copy link
Collaborator

subhramit commented Sep 27, 2024

@subhramit We should test this. This PR implements both suggestions of you (#11809 (comment)). I think, one needs to open 5 to 10 libraries, switch tabs back and forth, modifying the library and see if undo/redo is enabled properly.

Actually, suggestion 1 was to conditionally but explicitly use removeListener, which is not what has been done in the PR. WeakChangeListner automatically takes care of that.

What is probably confusing - use of unbind - I think not really necessary here (I dont remember how the garbage collecter deals with a chain of references), but still a safe practice as there is a life cycle mismatch between RedoAction and tabs - RedoAction might live longer than the relevance of a particular tab. There can be "zombie" bindings between executable and the tab. By unbinding, the old/irrelevant tabs can be garbage collected even if RedoAction itself is still alive (provided they're not referenced anywhere else).

Comment on lines 30 to +32
this.executable.bind(libraryTab.getUndoManager().getRedoableProperty()));
});

oldValue.ifPresent(libraryTab -> this.executable.unbind());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking a bit more, I am a bit concerned about the order of operations here.
Case 1:
State A->B - change detected, bind to B, unbind if oldValue is not null. After this,
State B->A - change detected, bind to A (not needed), unbind if B (oldvalue) is not null, which is true, so unbind (this is fine).

Case 2:
State A->B - change detected, bind to B, unbind if oldValue is not null. After this,
State B->C - change detected, bind to C, unbind if B is not null (which is true) - thus executable is not bound.

@Siedlerchr would it be better if we don't unbind at all here? Or is this alright as LibraryTab can never have a third state "C"?

@HoussemNasri
Copy link
Member

HoussemNasri commented Sep 29, 2024

Okey. Should I send it to the same branch?

Undo/Redo are part of the same component, IMO this PR should handle both of them.

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.

Remove listeners in RedoAction for memory efficiency
4 participants