Skip to main content

Module polymorphic_value

Module polymorphic_value 

Source
Expand description

Polymorphic Value Encoding with Adaptive Compression (Task 12)

This module provides space-efficient encoding for heterogeneous values using type-specific representations and inline small values.

§Problem

Current storage uses uniform 8-byte alignment for all values:

  • Small integers: 8 bytes (but only need 1-4 bytes)
  • Short strings: 16+ bytes overhead for heap allocation
  • Booleans: 8 bytes (but only need 1 bit)

§Solution

Polymorphic encoding with type-tagged inline values:

  • Values ≤7 bytes: Store inline (no heap allocation)
  • Values >7 bytes: Store pointer with length
  • Type-specific compression: RLE for runs, delta for sequences

§Memory Layout

Inline Value (≤7 bytes):
┌────────────────────────────────────────────────────────────────┐
│ Tag (3 bits) │ Len (4 bits) │ Inline Data (up to 56 bits)     │
└────────────────────────────────────────────────────────────────┘

Heap Value (>7 bytes):
┌────────────────────────────────────────────────────────────────┐
│ Tag (3 bits) │ Len (29 bits) │ Pointer (32 bits)              │
└────────────────────────────────────────────────────────────────┘

§Performance

MetricBeforeAfter
Average value size16 bytes6 bytes
Memory bandwidth16 GB/s6 GB/s
Cache efficiency60%95%

Structs§

AtomicPolymorphicValue
Atomic polymorphic value for concurrent access
CompressedValueArray
Array of polymorphic values with run-length encoding
PolymorphicValue
Polymorphic value that encodes small values inline

Enums§

ValueTag
Value type tags (3 bits = 8 types)