[ https://issues.apache.org/jira/browse/MNG-7899?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17777677#comment-17777677 ]
ASF GitHub Bot commented on MNG-7899: ------------------------------------- gnodet commented on code in PR #1269: URL: https://github.com/apache/maven/pull/1269#discussion_r1366713989 ########## maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java: ########## @@ -64,20 +65,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(); - buffer.append(getStatus(entry.getKey().getResourceName(), complete, total)); - if (entries.hasNext()) { - 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(); + + 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(" ("); + } + + buffer.append(format.formatProgress(complete, total)); Review Comment: I wonder if the `FileSizeFormat` should be refactored to be given a `StringBuffer` to write into, instead of (or in addition to) the current methods. But this may be for another PR... > Various memory usage improvements > --------------------------------- > > Key: MNG-7899 > URL: https://issues.apache.org/jira/browse/MNG-7899 > Project: Maven > Issue Type: Improvement > Components: Design, Patterns & Best Practices, Embedding, > General, Logging > Affects Versions: 4.0.0-alpha-2 > Reporter: sebastien > Priority: Major > > Some optimisations can be applied to the code to reduce the use of temporary > objects. > Typical improvements identified are: > * reduce scope of temporary objects creation to avoid creating when not > needed. Example : > {code:java} > public String toString() { > StringBuilder sb = new StringBuilder(512); > if (isEmpty()) { > return "empty"; > } > for (MetadataGraphVertex v : vertices) { > .....{code} > can be replaced by > {code:java} > public String toString() { > if (isEmpty()) { > return "empty"; > } > StringBuilder sb = new StringBuilder(512); > for (MetadataGraphVertex v : vertices) { > .....{code} > * Reuse StringBuilder objects in loops by setting its length to zero > * Use the StringBuilder.append() with index to avoid String.substring(). > Example: > > {code:java} > int idx = resourceName.lastIndexOf('/'); > buffer.append(idx < 0 ? resourceName : resourceName.substring(idx + 1)); > {code} > can be replaced by > > {code:java} > int idx = resourceName.lastIndexOf('/'); > if (idx < 0) { > buffer.append(resourceName); > } else { > buffer.append(resourceName, idx + 1, resourceName.length()); > } {code} > > > * Replace dynamic string creation static constants when possible > * Avoid creating temporary strings with + operator when the final > destination can be used instead, like PrintStream.print() method > * Avoir using StringBuilder.append() method and util method MessageUtils.a() > when the final destination can be used instead, like PrintStream.print() > method > -- This message was sent by Atlassian Jira (v8.20.10#820010)