#1766 - Performance tweaks in TemplateVariable.toString(…).

Heavily inspired by the PR @MikeRocke, we removed all usage of String.format(…) from hot code paths triggered by ….toString() as it's used in general output a lot.

Before:

Benchmark                                    Mode  Cnt         Score        Error  Units
TemplateVariableBenchmark.toString(…)       thrpt    3   2803239,270 ± 110258,955  ops/s

After:

Benchmark                                    Mode  Cnt         Score        Error  Units
TemplateVariableBenchmark.toString(…)       thrpt    3  10753653,459 ± 156684,459  ops/s
This commit is contained in:
Oliver Drotbohm
2022-04-14 14:27:11 +02:00
parent afb87f83b8
commit 0558bd0cd2

View File

@@ -280,18 +280,21 @@ public final class TemplateVariable implements Serializable, UriTemplate.Expanda
*/
@Override
public String toString() {
return StringUtils.hasText(description) ? String.format("%s - %s", asString(), description) : asString();
return StringUtils.hasText(description) ? asString() + " - " + description : asString();
}
public String asString() {
return String.format("{%s%s}", type.toString(), essence());
return "{" + type.toString() + essence() + "}";
}
String essence() {
return String.format("%s%s%s", name,
limit != -1 ? ":".concat(String.valueOf(limit)) : "",
isComposite() ? "*" : "");
String result = name;
result += limit != -1 ? ":" + limit : "";
result += isComposite() ? "*" : "";
return result;
}
public String getName() {