# Native C/C++ Like Performance For Java Object Serialisation

DevFeed: [Native C/C++ Like Performance For Java Object Serialisation](<https://devfeed.tech/articles/native-c-c-like-performance-for-java-object-serialisation-13626.md>)

Original publisher: [Read original article](<https://mechanical-sympathy.blogspot.com/2012/07/native-cc-like-performance-for-java.html>)

Author: Martin Thompson (noreply@blogger.com)

Published: 2012-07-05T17:51:00Z

Content type: tutorial

Language: en

Sources: [Mechanical Sympathy](<https://devfeed.tech/sources/mechanical-sympathy.md>)

Topics: [Java](<https://devfeed.tech/topics/java.md>), [IO](<https://devfeed.tech/topics/io.md>), [systems](<https://devfeed.tech/topics/systems.md>), [Processes](<https://devfeed.tech/topics/processes.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [distributed](<https://devfeed.tech/tags/distributed.md>), [io](<https://devfeed.tech/tags/io.md>), [java](<https://devfeed.tech/tags/java.md>), [low-latency](<https://devfeed.tech/tags/low-latency.md>), [native](<https://devfeed.tech/tags/native.md>), [nio](<https://devfeed.tech/tags/nio.md>), [object](<https://devfeed.tech/tags/object.md>), [performance](<https://devfeed.tech/tags/performance.md>), [processes](<https://devfeed.tech/tags/processes.md>), [serialization](<https://devfeed.tech/tags/serialization.md>), [systems](<https://devfeed.tech/tags/systems.md>)

## AI overview

This article compares Java Serialization, a ByteBuffer-based binary protocol, and Unsafe-based serialization for converting Java objects to byte streams. It reports that Unsafe can reduce serialization time for a small object from about 10,000 nanoseconds to less than 100 nanoseconds, while binary approaches use fewer bytes than Java Serialization in the example.

## Source excerpt

Do you ever wish you could turn a Java object into a stream of bytes as fast as it can be done in a native language like C++? If you use standard Java Serialization you could be disappointed with the performance. Java Serialization was designed for a very different purpose than serialising objects as quickly and compactly as possible. Why do we need fast and compact serialisation? Many of our systems are distributed and we need to communicate by passing state between processes efficiently. This state lives inside our objects. I've profiled many systems and often a large part of the cost is the serialisation of this state to-and-from byte buffers. I've seen a significant range of protocols and mechanisms used to achieve this. At one end of the spectrum are the easy to use but inefficient protocols likes Java Serialisation, XML and JSON. At the other end of this spectrum are the binary protocols that can be very fast and efficient but they require a deeper understanding and skill. In this article I will illustrate the performance gains that are possible when using simple binary protocols and introduce a little known technique available in Java to achieve similar performance to what is possible with native languages like C or C++. The three approaches to be compared are: Java Serialization: The standard method in Java of having an object implement Serializable. Binary via ByteBuffer: A simple protocol using the ByteBuffer API to write the fields of an object in binary format. This is our baseline for what is considered a good binary encoding approach. Binary via Unsafe: Introduction to Unsafe and its collection of methods that allow direct memory manipulation. Here I will show how to get similar performance to C/C++. The Code import sun.misc.Unsafe; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.Field; import java.nio.By