Skip to content

Commit

Permalink
[MNG-7899] Various memory usage improvements
Browse files Browse the repository at this point in the history
- Non-threadsafe StringBuilder instance 'buffer' can be make class
instance since it is always called in synchronized methods

- remove synchronized block in transferProgressed() method, the method
is synchronized and the block is not needed
  • Loading branch information
sebastien-doyon committed Oct 19, 2023
1 parent 730d0b5 commit 2895d01
Showing 1 changed file with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ConsoleMavenTransferListener extends AbstractMavenTransferListener

private Map<TransferResource, Long> transfers = Collections.synchronizedMap(new LinkedHashMap<>());
private FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH); // use in a synchronized fashion
private StringBuilder buffer = new StringBuilder(128); // use in a synchronized fashion

private boolean printResourceNames;
private int lastLength;
Expand Down Expand Up @@ -65,39 +66,36 @@ public synchronized void transferProgressed(TransferEvent event) throws Transfer
TransferResource resource = event.getResource();
transfers.put(resource, event.getTransferredBytes());

StringBuilder buffer = new StringBuilder(128);
buffer.append("Progress (").append(transfers.size()).append("): ");

synchronized (transfers) {
Iterator<Map.Entry<TransferResource, Long>> entries =
transfers.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<TransferResource, Long> entry = entries.next();
long total = entry.getKey().getContentLength();
Long complete = entry.getValue();

String resourceName = entry.getKey().getResourceName();

if (printResourceNames) {
int idx = resourceName.lastIndexOf('/');

if (idx < 0) {
buffer.append(resourceName);
} else {
buffer.append(resourceName, idx + 1, resourceName.length());
}
buffer.append(" (");
}
Iterator<Map.Entry<TransferResource, Long>> entries =
transfers.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<TransferResource, Long> entry = entries.next();
long total = entry.getKey().getContentLength();
Long complete = entry.getValue();

buffer.append(format.formatProgress(complete, total));
String resourceName = entry.getKey().getResourceName();

if (printResourceNames) {
buffer.append(")");
}
if (printResourceNames) {
int idx = resourceName.lastIndexOf('/');

if (entries.hasNext()) {
buffer.append(" | ");
if (idx < 0) {
buffer.append(resourceName);
} else {
buffer.append(resourceName, idx + 1, resourceName.length());
}
buffer.append(" (");
}

buffer.append(format.formatProgress(complete, total));

if (printResourceNames) {
buffer.append(")");
}

if (entries.hasNext()) {
buffer.append(" | ");
}
}

Expand All @@ -107,6 +105,7 @@ public synchronized void transferProgressed(TransferEvent event) throws Transfer
buffer.append('\r');
out.print(buffer);
out.flush();
buffer.setLength(0);
}

private void pad(StringBuilder buffer, int spaces) {
Expand Down Expand Up @@ -136,12 +135,12 @@ public synchronized void transferFailed(TransferEvent event) {

private void overridePreviousTransfer(TransferEvent event) {
if (lastLength > 0) {
StringBuilder buffer = new StringBuilder(lastLength + 1);
pad(buffer, lastLength);
buffer.append('\r');
out.print(buffer);
out.flush();
lastLength = 0;
buffer.setLength(0);
}
}
}

0 comments on commit 2895d01

Please sign in to comment.