Skip to content

perf: cache DDL constraint metadata on TableSchema instead of re-parsing per statement #643

Description

@iheitlager

Description

compile_insert (src/codegen/stmt/insert.rs:200) and compile_update_with_catalog (src/codegen/stmt/update.rs:80) both call parse_create_table(&schema.sql) on every single statement compile, re-tokenizing and re-parsing the table's full original CREATE TABLE text purely to recover constraint info (CHECK, PRIMARY KEY/AUTOINCREMENT) that TableSchema doesn't store structurally. This runs once per INSERT/UPDATE compile — i.e., once per single-row statement — and is a likely major contributor to a ~10x perf gap vs. the SQLite oracle on single-row insert/update benchmarks (insert_single: 14.9ms ours vs 1.85ms oracle).

Found via a 4-agent investigation into tests/performance/crud.rs/engine.rs benchmark regressions. Fsync frequency and journal discipline were separately ruled out as correct (matching SQLite's own 2-fsync-per-commit rollback-journal model).

Complexity

Estimate: small
Reasoning: Add a constraints/rowid-alias-autoincrement field to TableSchema (src/schema/ddl_reader.rs:71), populate it once in read_schema, and switch the two call sites to read it directly instead of re-parsing. Contained to schema loading + two codegen call sites; no grammar/parser surface change.

Context

Related to the tokenizer string-copy ticket (filed alongside this one) — the two compound, since each DDL re-parse also pays a full tokenizer string allocation.

Acceptance Criteria

  • TableSchema carries parsed constraint/autoincrement metadata populated at read_schema time
  • compile_insert/compile_update_with_catalog no longer call parse_create_table(&schema.sql) per statement
  • insert_single/update_pk benchmarks in tests/performance/crud.rs show measurable improvement
  • Existing oracle-parity/corpus tests still pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions