Files
turso/macros
Pekka Enberg 3fc00260b0 Merge 'core/mvcc: Make MVCC encryption less memory hungry with chunking' from Avinash Sajjanshetty
This PR builds on top of #5836. In the previous PR, we encrypted entire
frame as one. This adds a chunking optimisation. If the tx frame is
below 32KB, we encrypt the whole thing. Otherwise we split it into
chunks of 32KB and operate.
Why 32KB? picked some size, I felt not to big to hold in memory and not
so small that it adds lots of overhead.
## Why
In MVCC log, each log item is Tx Frame, each frame contains multiple ops
(insert/update etc). The payload size is not fixed (unlike 4KB fixed
size in a WAL), it can be anything. If we don't do chunking, we have to
keep entire payload in the memory while encrypting it (during write
path). With chunking, we can keep only upto chunk size and keep writing
to disk. Similar thing applies while reading from disk, we can keep
reading the part as we require it and decrypt it, as supposed to read
everything to decrypt.
We want to do this _now_ because encryption maps to disk format. If we
want to introduce such optimisation tomorrow, we have to do it with
versioning.
This is not a CPU performance optimisation, rather more like
optimisation in the algorithm, the direct effect would in reduction of
memory usage.
## Encrypted Chunking for MVCC Logical Log
  ### Frame Layout: Unencrypted vs Encrypted
  ```text
  Unencrypted:
  ┌──────────────┬──────────────────────────────┬───────────┐
  │ TX Header    │ Payload                      │ Trailer   │
  │ (24B plain)  │ Op₀ | Op₁ | Op₂ | ...        │ CRC + End │
  └──────────────┴──────────────────────────────┴───────────┘

  Encrypted (chunked):
  ┌──────────────┬──────────┬──────────┬──────────┬───────────┐
  │ TX Header    │ Chunk 0  │ Chunk 1  │ Chunk N  │ Trailer   │
  │ (24B plain)  │ ct|n     │ ct|n     │ ct|n     │ CRC + End │
  └──────────────┴──────────┴──────────┴──────────┴───────────┘
                      │
                      ▼
                ┌───────────────────────────┬───────┐
                │ ciphertext (plain + tag)  │ nonce │
                └───────────────────────────┴───────┘
  ```
  Each chunk encrypted with AAD (32B):
  ```text
  ┌────────┬────────────────────┬──────────┬────────────┬─────────────┐
  │salt (8)│payload_size_or_0(8)│op_cnt (4)│commit_ts(8)│chunk_idx (4)│
  └────────┴────────────────────┴──────────┴────────────┴─────────────┘
            ↑
            └── payload_size only in final chunk; zero for all others
  ```
  ### How Plaintext Payload Is Split Into Chunks
  ```text
  Plaintext payload (serialized ops, payload_size bytes):

  ┌──────┬──────┬────────────┬──────────┬──────┬────────────┬──────┬──────┬──────┬───────┐
  │ Op₀  │ Op₁  │    Op₂     │   Op₃    │ Op₄  │    Op₅     │ Op₆  │ Op₇  │ Op₈  │ Op₉   │
  └──────┴──────┴─────┼──────┴──────────┴──────┴──────┼─────┴──────┴──────┴──────┴───────┘
                      │                               │
                32 KB boundary                   64 KB boundary

  Chunking splits at fixed 32 KB boundaries — ops may straddle them:

    Chunk 0 (32 KB)              Chunk 1 (32 KB)              Chunk 2 (remainder)
  ┌──────┬──────┬──────┐     ┌──────┬──────┬──────┬──────┐   ┌──────┬──────┬──────┬──────┐
  │ Op₀  │ Op₁  │ Op₂▌ │     │▐Op₂  │ Op₃  │ Op₄  │ Op₅▌ │   │▐Op₅  │ Op₆  │ Op₇  │ ...  │
  └──────┴──────┴──────┘     └──────┴──────┴──────┴──────┘   └──────┴──────┴──────┴──────┘
                 ├─── Op₂ split across chunks 0 & 1 ───┤              │
                                           ├── Op₅ split across chunks 1 & 2 ──┤

    Op₂ starts in chunk 0, ends in chunk 1.  The reader uses a "carry buffer"
    to accumulate the partial op across chunk boundaries before parsing.

              │                          │                       │
              ▼                          ▼                       ▼
        ┌───────────┬────┐         ┌───────────┬────┐     ┌───────────┬────┐
        │ciphertext₀│ N₀ │         │ciphertext₁│ N₁ │     │ciphertext₂│ N₂ │
        │(32KB+tag) │    │         │(32KB+tag) │    │     │(rem+tag)  │    │
        └───────────┴────┘         └───────────┴────┘     └───────────┴────┘
         on-disk chunk blob         on-disk chunk blob     on-disk chunk blob

  Each chunk is encrypted independently with AEAD. The reader decrypts one chunk
  at a time. If an op is incomplete at the end of a chunk, the leftover bytes go
  into a carry buffer and are joined with bytes from the next decrypted chunk.
  ```
Used Codex, but with multiple iterations.

Reviewed-by: Nikita Sivukhin (@sivukhin)

Closes #6174
2026-04-09 12:16:53 +03:00
..
2025-08-31 14:35:03 +07:00