Skip to main content

Hybrid API

HybridBigInt — Optimal Memory/Speed Trade-off.

Module: src/hybrid.zig

Storage Modes

ModeStorageSpeedUse Case
Packed1.58 bits/tritSlowerStorage
Unpacked8 bits/tritFastComputation

Core Functions

zero() → HybridBigInt

var v = HybridBigInt.zero();

random(len) → HybridBigInt

var v = HybridBigInt.random(1000);

pack() / ensureUnpacked()

vector.pack();           // Memory efficient
vector.ensureUnpacked(); // Compute efficient

Constants

ConstantValueDescription
MAX_TRITS59049Maximum dimension
SIMD_WIDTH32Parallel trits

Memory Comparison

Live Editor
function MemoryDemo() {
  const [trits, setTrits] = React.useState(1000);

  // Storage calculations
  const binaryBits = trits * 2;          // 2 bits per trit (naive)
  const packedBits = trits * 1.585;      // log2(3) bits per trit
  const unpackedBits = trits * 8;        // 1 byte per trit

  const savings = ((binaryBits - packedBits) / binaryBits * 100).toFixed(1);

  return (
    <div style={{fontFamily: 'monospace'}}>
      <div>
        <label>Trits: </label>
        <input
          type="range"
          min="100"
          max="10000"
          value={trits}
          onChange={(e) => setTrits(Number(e.target.value))}
        />
        <span> {trits}</span>
      </div>
      <table style={{marginTop: '1rem', width: '100%'}}>
        <thead>
          <tr><th>Mode</th><th>Bits</th><th>Bytes</th></tr>
        </thead>
        <tbody>
          <tr>
            <td>Binary (2 bit)</td>
            <td>{binaryBits}</td>
            <td>{(binaryBits/8).toFixed(0)}</td>
          </tr>
          <tr style={{color: '#16a34a', fontWeight: 'bold'}}>
            <td>Packed (1.58 bit)</td>
            <td>{packedBits.toFixed(0)}</td>
            <td>{(packedBits/8).toFixed(0)}</td>
          </tr>
          <tr>
            <td>Unpacked (8 bit)</td>
            <td>{unpackedBits}</td>
            <td>{(unpackedBits/8).toFixed(0)}</td>
          </tr>
        </tbody>
      </table>
      <div style={{marginTop: '0.5rem'}}>
        <b>Packed savings vs binary: {savings}%</b>
      </div>
    </div>
  );
}
Result
Loading...