XStrings provide a zero garbage alternative to Java Strings for ultra-low-latency applications. XStrings are mutable, pooled string-like objects that avoid allocations during message processing.
Overview
An XString is a zero garbage, mutable string-like object defined in ADM that can be used in place of Java Strings. Unlike Java Strings, which are immutable and allocated on the heap, XStrings are mutable and can be pooled and reused across message processing cycles. XStrings are particularly useful for fields that need to be frequently updated with string values, such as timestamps, identifiers, or status messages.
Declaring XStrings in ADM
XStrings are declared in ADM message or state definitions using the xstring type:
The ADM code generator produces accessor methods for XString fields that enable zero-garbage operations:
Method
Description
getXXX()
Returns the XString object for reading
setXXX(CharSequence)
Copies the provided CharSequence into the XString
setXXX(CharSequence, int, int)
Copies a substring into the XString
lendXXX()
Returns a reference to the XString for direct manipulation
Example usage:
XString Preallocation
To achieve zero-garbage operation with XStrings, ensure they are preallocated with sufficient capacity:
When an XString's capacity is exceeded during a setXXX() call, the backing storage will be reallocated, causing garbage. Monitor your XString usage to ensure capacities are appropriately sized.
@EventHandler
public void onNewOrder(NewOrder order) {
// Set from a String
order.setOrderId("ORD-12345");
// Read the value
XString orderId = order.getOrderId();
String orderIdStr = orderId.toString(); // creates a new String
// Better: compare without allocating
if (orderId.equals("ORD-12345")) {
// process
}
// Direct manipulation for zero-garbage
XString symbolXStr = order.lendSymbol();
symbolXStr.clear();
symbolXStr.append("AAPL");
}