Skip to content

perf(sqlite-ext): reuse prepared helper state on local UDF hot paths #70

Open

Description

@marcus-pousette

Summary

Reduce steady-state overhead in the SQLite extension local operation path by reusing prepared helper state instead of rebuilding statement-heavy helper objects for each local operation.

Problem

The local sqlite-ext path still constructs fresh helper objects per operation, even on hot paths:

  • SqliteNodeStore::prepare(db)
  • SqlitePayloadStore::prepare(db)
  • SqliteParentOpIndex::prepare(db, doc_id)

Those helpers prepare one or more SQLite statements each time they are created. That is likely avoidable overhead for repeated local insert / move / delete / payload operations, especially in wa-sqlite worker mode.

Goal

Reuse or cache prepared helper state per SQLite connection so local UDFs do not repeatedly pay the full setup cost on the steady-state path.

Possible approaches

  • Maintain a per-sqlite3* cache of prepared helper objects.
  • Store per-connection helper state with a lifetime-managed SQLite attachment mechanism.
  • If full helper caching is too invasive, reduce the number of prepared statements or split out lighter-weight hot-path helpers.

Scope

This issue is about the whole local sqlite-ext hot path, not just one operation kind.

Relevant areas:

  • packages/treecrdt-sqlite-ext/src/extension/functions/local_ops.rs
  • packages/treecrdt-sqlite-ext/src/extension/functions/node_store.rs
  • packages/treecrdt-sqlite-ext/src/extension/functions/payload_store.rs
  • packages/treecrdt-sqlite-ext/src/extension/functions/op_index.rs

Acceptance criteria

  • Steady-state local insert / move / delete / payload operations avoid repeated sqlite_prepare_v2 setup for the main helper objects.
  • The resulting design is safe for connection lifetime and cleanup.
  • Add a small benchmark or profiling note showing reduced overhead on hot local-op paths.

Activity

  1. marcus-pousette commented on Apr 22, 2026

    @marcus-pousette
    CollaboratorAuthor

    Rewrite proposal for current scope:

    This issue is still valid, but the current body is tied too tightly to the older local-delete / order-key wording.

    What is still true:

    • the local sqlite-ext hot path still builds fresh prepared-statement helper objects per operation
    • that includes SqliteNodeStore, SqlitePayloadStore, and SqliteParentOpIndex
    • this is still a plausible source of avoidable overhead, especially for high-frequency local ops and wa-sqlite worker usage

    What is stale:

    • it is no longer mainly about treecrdt_local_delete
    • the old standalone order_key.rs path mentioned in the issue body no longer exists

    Suggested rewrite:

    • keep the issue open
    • reframe it as caching / reusing prepared helper state for the sqlite-ext local operation path as a whole
    • acceptance criteria should focus on reducing steady-state sqlite_prepare_v2 calls across local insert/move/delete/payload paths, not just one function
  2. changed the title [-]perf(sqlite-ext): cache prepared statements for local UDF hot paths[/-] [+]perf(sqlite-ext): reuse prepared helper state on local UDF hot paths[/+] on Apr 22, 2026
  3. added
    performance
    area:sqlite
    on Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:sqliteperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions