Skip to main content
Version: 1.2.0

Compression

This page covers compression options for reducing serialized data size.

Integer Compression​

ForyBuilder#withIntCompressed/ForyBuilder#withLongCompressed can be used to compress int/long for smaller size. Normally compressing int is enough.

Both compression options are enabled by default. If the serialized size is not important (for example, you use FlatBuffers for serialization before, which doesn't compress anything), then you should disable compression. If your data are all numbers, the compression may bring 80% performance regression.

Int Compression​

For int compression, Fory uses 1~5 bytes for encoding. The first bit in every byte indicates whether there is a next byte. If the first bit is set, then the next byte will be read until the first bit of the next byte is unset.

Long Compression​

For long compression, Fory supports two encodings:

SLI (Small Long as Int) Encoding (Default)​

  • If long is in [-1073741824, 1073741823], encode as 4 bytes int: | little-endian: ((int) value) << 1 |
  • Otherwise write as 9 bytes: | 0b1 | little-endian 8bytes long |

PVL (Progressive Variable-length Long) Encoding​

  • First bit in every byte indicates whether there is a next byte. If first bit is set, then next byte will be read until first bit of next byte is unset.
  • Negative numbers will be converted to positive numbers by (v << 1) ^ (v >> 63) to reduce cost of small negative numbers.

If a number is of long type but can't be represented by smaller bytes mostly, the compression won't get good enough results—not worthy compared to performance cost. Maybe you should try to disable long compression if you find it didn't bring much space savings.

Array Compression​

Fory supports SIMD-accelerated compression for primitive arrays (int[] and long[]) when array values can fit in smaller data types. This feature is available on Java 16+ and uses the Vector API for optimal performance.

How Array Compression Works​

Array compression analyzes arrays to determine if values can be stored using fewer bytes:

  • int[] → byte[]: When all values are in range [-128, 127] (75% size reduction)
  • int[] → short[]: When all values are in range [-32768, 32767] (50% size reduction)
  • long[] → int[]: When all values fit in integer range (50% size reduction)

Configuration and Registration​

To enable array compression, you must explicitly register the serializers:

Fory fory = Fory.builder()
.withXlang(false)
// Enable int array compression
.withIntArrayCompressed(true)
// Enable long array compression
.withLongArrayCompressed(true)
.build();

// You must explicitly register compressed array serializers
CompressedArraySerializers.registerSerializers(fory);

Compressed array serializers are included in fory-core and use the Java 16+ Vector API when it is available.

String Compression​

String compression can be enabled via ForyBuilder#withStringCompressed(true). This is disabled by default.

Configuration Summary​

OptionDescriptionDefault
compressIntEnable int compressiontrue
compressLongEnable long compressiontrue
compressIntArrayEnable SIMD int array compression (Java 16+)false
compressLongArrayEnable SIMD long array compression (Java 16+)false
compressStringEnable string compressionfalse

Performance Considerations​

  1. Disable compression for numeric-heavy data: If your data is mostly numbers, compression overhead may not be worth it
  2. Array compression requires Java 16+: Uses Vector API for SIMD acceleration
  3. Long compression may not help large values: If most longs can't fit in smaller representations, disable it
  4. String compression has overhead: Only enable if strings are highly compressible

Example Configuration​

// For mostly numeric data - disable compression
Fory fory = Fory.builder()
.withXlang(false)
.withIntCompressed(false)
.withLongCompressed(false)
.build();

// For mixed data with arrays - enable array compression
Fory fory = Fory.builder()
.withXlang(false)
.withIntCompressed(true)
.withLongCompressed(true)
.withIntArrayCompressed(true)
.withLongArrayCompressed(true)
.build();
CompressedArraySerializers.registerSerializers(fory);