DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • On Some Aspects of Big Data Processing in Apache Spark, Part 1: Serialization
  • High-Performance Java Serialization to Different Formats
  • Integrating Apache Doris and Hudi for Data Querying and Migration
  • Problem Analysis in Apache Doris StreamLoad Scenarios

Trending

  • Solid Testing Strategies for Salesforce Releases
  • The Role of Retrieval Augmented Generation (RAG) in Development of AI-Infused Enterprise Applications
  • Ensuring Configuration Consistency Across Global Data Centers
  • Grafana Loki Fundamentals and Architecture
  1. DZone
  2. Data Engineering
  3. Data
  4. Improving Serialization and Memory Efficiency With a LongConverter

Improving Serialization and Memory Efficiency With a LongConverter

Chronicle Wire is an OSS library that makes it easier to work with fields encoding short strings and timestamps as 64-bit long.

By 
Peter Lawrey user avatar
Peter Lawrey
·
Jun. 20, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
5.0K Views

Join the DZone community and get the full member experience.

Join For Free

Chronicle Wire is a powerful open-source serialization library for high-performance data exchange in various binary and text formats, including YAML.

Strings in your data structures can have significant overhead regarding memory usage and access patterns. For each String, you have two objects, the String object, and the char[] or byte[], which contains the actual text. Strings are also immutable, and object pooling tends to create many objects for garbage collection in initialization and deserialization.

Java
 
class MyDto {
    String field; // Reference to a String object.
}

class String {
    final char[] value; // Reference to a char[] containing the text.
}


The char[] contains the actual text.

By comparison, using a long requires no additional memory or memory access. However, it can only contain 64 bits of data. With careful encoding, this limitation can be effectively managed. The two most commonly used LongConverters in Chronicle Wire are Base-85 encoded strings and nano-second resolution timestamps. 

Base-85 Encoding

With Base-85 encoding, you can encode up to 10 characters in a 64-bit long. This method is highly efficient for handling short text strings.

Nanosecond Timestamps

For timestamps, you can encode a yyyy/MM/dd’T’sss.SSSSSSSSS into a 64-bit long, providing nanosecond resolution.

Practical Example

Consider an example using both of these:

Java
 
class Order extends BytesInBinaryMarshallable {
    @NanoTime 
    private long timestamp;
    @ShortText
    private long trader;
}


In YAML this can be encoded as:

YAML
 
!Order {
  timestamp: 2024-06-14T12:41:58.4512345,
  trader: alice
}


This YAML representation takes 68 bytes. However, the same data is more compact using 16 bytes when using longs. The message is smaller, and serializing and deserializing are much more efficient.

toString() as YAML

The toString() of the class will produce the YAML, and that can be deserialized as the original data.

Java
 
// Keep the type short in YAML, but it is not required.
ClassAliasPool.CLASS_ALIASES.addAlias(Order.class);

// create an instance with a nano-second resolution timestamps and trader name called “trader”
Order order = new Order();
order.timestamp = SystemTimeProvider.CLOCK.currentTimeNanos(); 
order.trader = ShortText.INSTANCE.parse("alice"));

// prints the object in YAML
System.out.println(order);

// prints: order.timestamp = 17da1070c7ef5d00
System.out.println("order.timestamp = " + Long.toHexString(order.timestamp));

// prints: order.trader = 8aeffe61
System.out.println("order.trader = " + Long.toHexString(order.trader));

// start with a data in YAML
String cs = "" +
        "!Order {\n" +
        "  timestamp: 2024-06-14T12:41:58.8178963,\n" +
        "  trader: alice\n" +
        "}";

// deserialize the object
Order o = Marshallable.fromString(cs);


Benefits of Using LongConverters

Reduced Memory Usage

64-bit long values do not require additional allocations beyond the object itself, unlike Strings or ZonedDateTime, which involve multiple objects.

Efficient Serialization/Deserialization

Handling longs is faster than dealing with complex String or date objects, leading to lower latency and higher throughput.

Making Annotations More Concise

The annotations above are shorthand for:

Java
 
class Order {
    @LongConvertion(NanoTime.class)
    private final long timestamp;

    @LongConvertion(ShortText.class)
    private final long trader;
}


However, Chronicle Wire can support specifying a custom annotation as a shorthand for an @LongConvertion. This annotation specifies the LongConverter to use as it has a LongConvertion annotation.

Java
 
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@LongConversion(ShortText.class)
public @interface ShortText {
    /**
     * An instance of {@link ShortTextLongConverter} specifically 
     * configured for Base85 conversions.
     */
    // NOTE: this is a public static final field on the annotation
    LongConverter INSTANCE = ShortTextLongConverter.INSTANCE;
}


This allows defining a shorthand for what would otherwise be a wordy annotation.

Other Integer Types

A LongConverter can also be used for shorter primitive fields such as byte, char, short, and int. A 32-bit int value can store five letters in Base-85 with @ShortText, and a byte can store a single character. A 32-bit int could also store a date or time of day with a custom converter. We have used char and short to store data with limited possible values, but are generally very specific to the use case.

Custom Converters

Additional converters can be provided by implementing LongConverter mapping text to a long and vice-versa.

Conclusion

By leveraging LongConverters, Chronicle Wire provides a significant performance boost, making it ideal for applications that require high performance and low latency, such as financial trading systems and real-time data processing.

This approach exemplifies how careful data encoding and efficient memory usage can lead to substantial performance improvements in high-frequency, low-latency applications.

Data exchange Data processing Strings Serialization

Opinions expressed by DZone contributors are their own.

Related

  • On Some Aspects of Big Data Processing in Apache Spark, Part 1: Serialization
  • High-Performance Java Serialization to Different Formats
  • Integrating Apache Doris and Hudi for Data Querying and Migration
  • Problem Analysis in Apache Doris StreamLoad Scenarios

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!