ToonEx.Btoon.SchemaCompiler (toon_ex v1.3.1)

Copy Markdown View Source

Compiles a BTOON schema into a specialized encoder function.

This eliminates runtime type dispatch and field name lookups during encoding.

Summary

Types

Compiled schema with precomputed encoder functions.

Functions

Compiles a schema into a specialized encoder.

Encodes a schema body using a compiled schema.

Types

compiled_schema()

@type compiled_schema() :: %{
  id: integer(),
  name: String.t(),
  fields: [
    %{
      name: String.t(),
      type: Btoon.Types.element_type(),
      encoder: (term(), Btoon.Encode.Ctx ->
                  {iodata(), non_neg_integer(), Btoon.Encode.Ctx}),
      size: non_neg_integer()
    }
  ]
}

Compiled schema with precomputed encoder functions.

Functions

compile(schema)

@spec compile(ToonEx.Btoon.Schema.t()) :: compiled_schema()

Compiles a schema into a specialized encoder.

Returns a map with the schema ID, name, and a list of fields with their precomputed encoder functions.

encode_schema_body(compiled, map, ctx)

@spec encode_schema_body(compiled_schema(), map(), Btoon.Encode.Ctx) ::
  {iodata(), non_neg_integer(), Btoon.Encode.Ctx}

Encodes a schema body using a compiled schema.

This is significantly faster than the generic schema encoder because it:

  • Precomputes encoder functions for each field type
  • Eliminates Map.fetch for field names (uses ordered list)
  • Avoids runtime type dispatch