Skip to content

Commit 6cba620

Browse files
committed
Allocations optimization
See pull request Allocations optimization for converters quickfix-j#34
1 parent d8cd45b commit 6cba620

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

quickfixj-core/src/main/java/quickfix/field/converter/UtcTimeOnlyConverter.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@
2929
* Convert between a time and a String.
3030
*/
3131
public class UtcTimeOnlyConverter extends AbstractDateTimeConverter {
32+
33+
protected static final class Context {
34+
private final DateFormat utcTimeFormat = createDateFormat("HH:mm:ss");
35+
private final DateFormat utcTimeFormatMillis = createDateFormat("HH:mm:ss.SSS");
36+
private final StringBuffer buffer = new StringBuffer(128);
37+
}
38+
3239
// SimpleDateFormats are not thread safe. A thread local is being
3340
// used to maintain high concurrency among multiple session threads
34-
private static final ThreadLocal<UtcTimeOnlyConverter> utcTimeConverter = new ThreadLocal<UtcTimeOnlyConverter>();
35-
private final DateFormat utcTimeFormat = createDateFormat("HH:mm:ss");
36-
private final DateFormat utcTimeFormatMillis = createDateFormat("HH:mm:ss.SSS");
41+
private static final ThreadLocal<Context> utcTimeConverter = new ThreadLocal<Context>() {
42+
@Override
43+
protected Context initialValue() {
44+
return new Context();
45+
}
46+
};
3747

3848
/**
3949
* Convert a time (represented as a Date) to a String (HH:MM:SS or HH:MM:SS.SSS)
@@ -43,15 +53,36 @@ public class UtcTimeOnlyConverter extends AbstractDateTimeConverter {
4353
* @return a String representing the time.
4454
*/
4555
public static String convert(Date d, boolean includeMilliseconds) {
46-
return getFormatter(includeMilliseconds).format(d);
56+
Context context = utcTimeConverter.get();
57+
try {
58+
(includeMilliseconds ? context.utcTimeFormatMillis : context.utcTimeFormat)
59+
.format(d, context.buffer, DontCareFieldPosition.INSTANCE);
60+
return context.buffer.toString();
61+
} finally {
62+
context.buffer.setLength(0);
63+
}
4764
}
4865

49-
private static DateFormat getFormatter(boolean includeMillis) {
50-
UtcTimeOnlyConverter converter = utcTimeConverter.get();
51-
if (converter == null) {
52-
converter = new UtcTimeOnlyConverter();
53-
utcTimeConverter.set(converter);
66+
/**
67+
* Convert a time (represented as a Date) to a String (HH:MM:SS or HH:MM:SS.SSS)
68+
*
69+
* @param d the date with the time to convert
70+
* @param includeMilliseconds controls whether milliseconds are included in the result
71+
* @param stringBuilder the out buffer to hold a String representing the time.
72+
*/
73+
public static void convert(Date d, StringBuilder stringBuilder, boolean includeMilliseconds) {
74+
Context context = utcTimeConverter.get();
75+
try {
76+
(includeMilliseconds ? context.utcTimeFormatMillis : context.utcTimeFormat)
77+
.format(d, context.buffer, DontCareFieldPosition.INSTANCE);
78+
stringBuilder.append(context.buffer);
79+
} finally {
80+
context.buffer.setLength(0);
5481
}
82+
}
83+
84+
private static DateFormat getFormatter(boolean includeMillis) {
85+
Context converter = utcTimeConverter.get();
5586
return includeMillis ? converter.utcTimeFormatMillis : converter.utcTimeFormat;
5687
}
5788

0 commit comments

Comments
 (0)