carterkozak commented on a change in pull request #352:
URL:
https://github.com/apache/httpcomponents-client/pull/352#discussion_r814900201
##########
File path:
httpclient5/src/main/java/org/apache/hc/client5/http/impl/ExecSupport.java
##########
@@ -45,7 +45,29 @@ public static long getNextExecNumber() {
}
public static String getNextExchangeId() {
- return String.format("ex-%010d", COUNT.incrementAndGet());
+ return createId(COUNT.incrementAndGet());
+ }
+
+ /**
+ * Create an exchange ID.
+ *
+ * Hand rolled equivalent to `String.format("ex-%010d", value)` optimized
to reduce
+ * allocation and CPU overhead.
+ */
+ static String createId(long value) {
+ String longString = Long.toString(value);
+ return "ex-" + zeroPad(10 - longString.length()) + longString;
Review comment:
Either a switch:
```java
switch (longString.length()) { // It's too bad
java.lang.Long.stringSize(long) is package-private
case 1:
return "ex-000000000" + longString;
/* etc... */
case 9:
return "ex-0" + longString;
default:
return "ex-" + longString;
}
```
Or an array of pre-computed values where we bounds-check and load a prefix
based on the strlen of the value:
```java
private static final prefixes = new String[] {"ex-000000000", /* etc... */
ex-0"}
return len > 10 ? "ex-" + longString : prefixes[len-1] + longString;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]