Skip to content

Commit 097891f

Browse files
marschallfmbenhassine
authored andcommitted
Optimize ExitStatus#addExitDescription
Optimize ExitStatus#addExitDescription by reducing string allocations through: - avoid string allocation when the current description is empty - avoid string allocation when the given description is empty - avoid intermediate string allocation by allocating the correct buffer size Issue #3979
1 parent bcc68f1 commit 097891f

File tree

1 file changed

+9
-8
lines changed
  • spring-batch-core/src/main/java/org/springframework/batch/core

1 file changed

+9
-8
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/ExitStatus.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -244,18 +244,19 @@ public boolean isRunning() {
244244
* description
245245
*/
246246
public ExitStatus addExitDescription(String description) {
247-
StringBuilder buffer = new StringBuilder();
248-
boolean changed = StringUtils.hasText(description) && !exitDescription.equals(description);
249247
if (StringUtils.hasText(exitDescription)) {
250-
buffer.append(exitDescription);
251-
if (changed) {
248+
if (StringUtils.hasText(description) && !exitDescription.equals(description)) {
249+
StringBuilder buffer = new StringBuilder(description.length() + 2 + exitDescription.length());
250+
buffer.append(exitDescription);
252251
buffer.append("; ");
252+
buffer.append(description);
253+
return new ExitStatus(exitCode, buffer.toString());
253254
}
255+
return this;
254256
}
255-
if (changed) {
256-
buffer.append(description);
257+
else {
258+
return new ExitStatus(exitCode, description);
257259
}
258-
return new ExitStatus(exitCode, buffer.toString());
259260
}
260261

261262
/**

0 commit comments

Comments
 (0)