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

[MNG-7899] Various memory usage improvements 4-1 #1296

Merged
merged 6 commits into from
Nov 9, 2023

Conversation

sebastien-doyon
Copy link
Contributor

  • BatchModeMavenTransferListener removed
  • FileSizeFormat moved to top level class
  • FileSizeFormat.formatProcess() refactored to accept an StringBuilder
    as argument
  • FileSizeFormat refactored format() methods

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

- BatchModeMavenTransferListener removed
- FileSizeFormat moved to top level class
- FileSizeFormat.formatProcess() refactored to accept an StringBuilder
as argument
- FileSizeFormat refactored format() methods
double scaledSize = (double) size / unit.bytes();

if (unit == ScaleUnit.BYTE) {
return largeFormat.format(size);
Copy link
Contributor

Choose a reason for hiding this comment

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

The goal is to avoid those calls.
NumberFormat has methods that take a StringBuilder...
In NumberFormat, I have the following declaration:

    public final String format(long number) {
        return format(number, new StringBuffer(),
                      DontCareFieldPosition.INSTANCE).toString();
    }

so we should go straight to the method that takes a StringBuffer to avoid unneeded allocations + toString....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would have been nice, but the NumberFormat.format() method takes only StringBuffer as parameter, the synchronized StringBuilder. They are not interchangeable and I am not sure we should switch StringBuilder for StringBuffer for that....

Copy link
Contributor

Choose a reason for hiding this comment

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

What about new Formatter(locale, builder).format("%3.f", size) ? i.e. do not use the NumberFormat, but Formatter ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using a Formatter, the pattern string is passed as parameter to the format() method. Doing this the pattern is recompiled each method call and not reused. This generates a lot of garbage. A more optimised solution would be to avoid using generic Format or Formatter classes and implement a custom logic specific to this particular case. Some math logic to round the numbers as needed. This would reduce even more the memory allocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gnodet you can find the refactored FileSizeFormat class using Math logic in the last commit

sebastien-doyon and others added 3 commits October 21, 2023 01:15
- FileSizeFormat refactored, replace DecimalFormat with Math logic to
reduce memory allocation
@sebastien-doyon
Copy link
Contributor Author

@gnodet your changes are cleaner. I agree with your contribution, thanks!

@sebastien-doyon
Copy link
Contributor Author

@gnodet I just fixed a format problem that made the build fail.

@gnodet gnodet merged commit 9474904 into apache:master Nov 9, 2023
18 checks passed
@gnodet gnodet added this to the 4.0.0-alpha-9 milestone Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants