Myra Codec

Schema-driven binary codec with zero-copy serialization

Serialization LayerZero-Copy

Overview

Myra Codec is a high-performance binary serialization library with a schema-first design. It supports full schema evolution, union types, and zero-copy encoding/decoding paths for maximum throughput.

Key Features

Schema-First Design

  • YAML/JSON schema definitions for type safety
  • Code generation producing idiomatic Java classes
  • Compile-time validation of schema compatibility

Zero-Copy Serialization

  • Direct buffer encoding without intermediate objects
  • Off-heap support via Roray FFM Utils integration
  • Streaming encode/decode for large messages

Schema Evolution

  • Forward and backward compatibility
  • Field additions without breaking changes
  • Union type evolution with proper versioning

Rich Type System

  • Primitive types: int8-64, float32/64, bool, string, bytes
  • Complex types: struct, list, map, union
  • Optional fields with proper null handling

Schema Example

# schema.myra.yml
namespace: com.example.trading

types:
  Order:
    id: int64
    symbol: string
    quantity: int32
    price: float64
    side: OrderSide
    timestamp: int64
    metadata: OrderMetadata?
    
  OrderSide:
    enum: [BUY, SELL]
    
  OrderMetadata:
    union:
      - MarketOrder: {}
      - LimitOrder:
          expiry: int64
      - StopOrder:
          triggerPrice: float64

Generated Code Usage

// Create and encode
var order = Order.builder()
    .id(12345L)
    .symbol("AAPL")
    .quantity(100)
    .price(150.50)
    .side(OrderSide.BUY)
    .timestamp(System.currentTimeMillis())
    .metadata(OrderMetadata.limitOrder(
        LimitOrder.builder().expiry(tomorrow).build()))
    .build();

// Encode to buffer (zero-copy)
var buffer = ByteBuffer.allocateDirect(order.encodedSize());
order.encode(buffer);

// Decode from buffer (zero-copy)
buffer.flip();
var decoded = Order.decode(buffer);

Performance Comparison

CodecThroughputAlloc/OpMessage Size
Myra Codec850K ops/s0 bytes128 bytes
Protocol Buffers620K ops/s48 bytes134 bytes
JSON (Jackson)180K ops/s512 bytes245 bytes
Java Serialization45K ops/s2.1 KB512 bytes

Use Cases

  • High-frequency trading message serialization
  • Distributed systems RPC payloads
  • Event sourcing compact event storage
  • Network protocols efficient wire format

Installation

Gradle (Kotlin DSL)

plugins {
    id("express.mvp.myra-codec") version "0.1.0"
}

dependencies {
    implementation("express.mvp:myra-codec:0.1.0")
}

myraCodec {
    schemaDir = file("src/main/schemas")
    outputDir = file("build/generated/sources/myra")
}

Maven

<dependency>
    <groupId>express.mvp</groupId>
    <artifactId>myra-codec</artifactId>
    <version>0.1.0</version>
</dependency>