Skip to content

v0.1.0 — Lightning-fast in-memory database

Choose a tag to compare

@hussain-alsaibai hussain-alsaibai released this 23 Jun 04:15
· 38 commits to main since this release

SnapDB v0.1.0

Extremely lightweight, lightning-fast in-memory database for Python.

Key Innovations

  • Slab-oriented storage — contiguous pages, CPU cache friendly, no malloc fragmentation
  • Zero-copy readsmemoryview slices into mmap: 1,212,499 reads/sec
  • Single-file — schema + bitmap + data in one .snap file
  • Pure Python, zero dependencies — stdlib only (mmap, struct, os, json)
  • Fixed-width typesi8/16/32/64, u8/16/32/64, f32/64, bool, bytes:N

Performance (100K rows)

Operation Speed
Insert 69,185 rows/sec
Read (decoded dict) 309,698 rows/sec
Read (zero-copy raw) 1,212,499 rows/sec
Sequential scan 389,072 rows/sec

Quick Start

from snapdb import SnapDB, Schema, ColumnDef

schema = Schema([
    ColumnDef("id", "i32"),
    ColumnDef("temp", "f32"),
    ColumnDef("active", "bool"),
])

with SnapDB("data.snap", schema) as db:
    db.insert({"id": 1, "temp": 25.5, "active": True})
    row = db.get(0)           # decoded dict
    raw = db.get_raw(0)       # zero-copy memoryview
    for idx, row in db.query(lambda r: r["active"]):
        print(idx, row)

Files

  • snapdb.py — Core engine (~400 lines)
  • test_snapdb.py — Unit tests
  • quickbench.py — Benchmark script

Design

SnapDB uses a slab allocator: each slab is a fixed-size page (default 4KB) holding all columns for N rows contiguously. A bitmap tracks live vs deleted rows. The file is memory-mapped for persistence and zero-copy access.

No external dependencies. No server. Just a single Python file.