Skip to content

Commit b38226e

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 8c7fb5c commit b38226e

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-2022 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.
@@ -246,18 +246,19 @@ public boolean isRunning() {
246246
* description.
247247
*/
248248
public ExitStatus addExitDescription(String description) {
249-
StringBuilder buffer = new StringBuilder();
250-
boolean changed = StringUtils.hasText(description) && !exitDescription.equals(description);
251249
if (StringUtils.hasText(exitDescription)) {
252-
buffer.append(exitDescription);
253-
if (changed) {
250+
if (StringUtils.hasText(description) && !exitDescription.equals(description)) {
251+
StringBuilder buffer = new StringBuilder(description.length() + 2 + exitDescription.length());
252+
buffer.append(exitDescription);
254253
buffer.append("; ");
254+
buffer.append(description);
255+
return new ExitStatus(exitCode, buffer.toString());
255256
}
257+
return this;
256258
}
257-
if (changed) {
258-
buffer.append(description);
259+
else {
260+
return new ExitStatus(exitCode, description);
259261
}
260-
return new ExitStatus(exitCode, buffer.toString());
261262
}
262263

263264
/**

0 commit comments

Comments
 (0)