Use StringBuilder.append(char) where possible

To slightly improve performance, this commit switches to
StringBuilder.append(char) instead of StringBuilder.append(String)
whenever we append a single character to a StringBuilder.

Closes gh-27098
This commit is contained in:
Sam Brannen
2021-06-24 18:53:51 +02:00
parent ddbb7c1b5b
commit a2ef6badc4
74 changed files with 232 additions and 220 deletions

View File

@@ -509,7 +509,7 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
}
// Patterns conditions are never empty and have "" (empty path) at least.
builder.append(" ").append(getActivePatternsCondition());
builder.append(' ').append(getActivePatternsCondition());
if (!this.paramsCondition.isEmpty()) {
builder.append(", params ").append(this.paramsCondition);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ import org.springframework.util.StringUtils;
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @author Sam Brannen
* @since 4.2
*/
public class SseEmitter extends ResponseBodyEmitter {
@@ -175,7 +176,7 @@ public class SseEmitter extends ResponseBodyEmitter {
SseEventBuilder data(Object object, @Nullable MediaType mediaType);
/**
* Return one or more Object-MediaType pairs to write via
* Return one or more Object-MediaType pairs to write via
* {@link #send(Object, MediaType)}.
* @since 4.2.3
*/
@@ -195,25 +196,25 @@ public class SseEmitter extends ResponseBodyEmitter {
@Override
public SseEventBuilder id(String id) {
append("id:").append(id).append("\n");
append("id:").append(id).append('\n');
return this;
}
@Override
public SseEventBuilder name(String name) {
append("event:").append(name).append("\n");
append("event:").append(name).append('\n');
return this;
}
@Override
public SseEventBuilder reconnectTime(long reconnectTimeMillis) {
append("retry:").append(String.valueOf(reconnectTimeMillis)).append("\n");
append("retry:").append(String.valueOf(reconnectTimeMillis)).append('\n');
return this;
}
@Override
public SseEventBuilder comment(String comment) {
append(":").append(comment).append("\n");
append(':').append(comment).append('\n');
return this;
}
@@ -227,7 +228,7 @@ public class SseEmitter extends ResponseBodyEmitter {
append("data:");
saveAppendedText();
this.dataToSend.add(new DataWithMediaType(object, mediaType));
append("\n");
append('\n');
return this;
}
@@ -239,12 +240,20 @@ public class SseEmitter extends ResponseBodyEmitter {
return this;
}
SseEventBuilderImpl append(char ch) {
if (this.sb == null) {
this.sb = new StringBuilder();
}
this.sb.append(ch);
return this;
}
@Override
public Set<DataWithMediaType> build() {
if (!StringUtils.hasLength(this.sb) && this.dataToSend.isEmpty()) {
return Collections.emptySet();
}
append("\n");
append('\n');
saveAppendedText();
return this.dataToSend;
}

View File

@@ -268,7 +268,7 @@ public class PathResourceResolver extends AbstractResourceResolver {
while (tokenizer.hasMoreTokens()) {
String value = UriUtils.encode(tokenizer.nextToken(), charset);
sb.append(value);
sb.append("/");
sb.append('/');
}
if (!path.endsWith("/")) {
sb.setLength(sb.length() - 1);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -352,7 +352,7 @@ public class BindStatus {
public String toString() {
StringBuilder sb = new StringBuilder("BindStatus: ");
sb.append("expression=[").append(this.expression).append("]; ");
sb.append("value=[").append(this.value).append("]");
sb.append("value=[").append(this.value).append(']');
if (!ObjectUtils.isEmpty(this.errorCodes)) {
sb.append("; errorCodes=").append(Arrays.asList(this.errorCodes));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -286,7 +286,7 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
}
}
if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) {
url.append("/");
url.append('/');
}
url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams));
url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1)));
@@ -324,15 +324,15 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
for (Param param : params) {
if (!usedParams.contains(param.getName()) && StringUtils.hasLength(param.getName())) {
if (includeQueryStringDelimiter && qs.length() == 0) {
qs.append("?");
qs.append('?');
}
else {
qs.append("&");
qs.append('&');
}
try {
qs.append(UriUtils.encodeQueryParam(param.getName(), encoding));
if (param.getValue() != null) {
qs.append("=");
qs.append('=');
qs.append(UriUtils.encodeQueryParam(param.getValue(), encoding));
}
}