Skip to content

feat: convert native SQLite result rows on demand - #537

Draft
chrispader wants to merge 10 commits into
mainfrom
feat/398-lazy-row-conversion
Draft

chrispader wants to merge 10 commits into
mainfrom
feat/398-lazy-row-conversion

Conversation

@chrispader

@chrispader chrispader commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Large queries can spend time building JavaScript row objects that callers never read. The native executeLazy and executeLazyAsync methods retain the completed result in positional storage and convert each row on first access. Repeated reads return the same row object, and Nitro tracks the native allocation while unconverted rows remain.

Part of #398.

Reviewed with sol-loop: 2 rounds with gpt-6.2-sol on high. Sol verdict: approved.

Note

🤖 Generated with Codex

@vercel

vercel Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
react-native-nitro-sqlite Ready Ready Preview Dec 4, 2026 4:11pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 30, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@chrispader chrispader left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Chris's AI collaborator dropping in (Claude Opus 5.5). 🤖

The native design is sound, but no CI job has built or run the new code, and one line breaks the declared Nitro range. I reviewed this PR and Callstack's Apex model reviewed it separately. Apex compiled the changed translation units with clang++ -fsyntax-only against react-native-nitro-modules@0.37.2 and React Native's jsi.h, and they came back clean.

What we both checked:

  • Lifetime: rows are deep copies, so they outlive the statement and connection. Native code holds no strong JS references.

  • Re-entrancy: a row that is mid-conversion is swapped out and restored if conversion throws.

  • Bounds: NaN, ±∞, fractions, negative and out-of-range indices are rejected.

  • Value parity: both paths share convertQueryRow. Int64 arrives as a double, BLOBs are copied, and NULL is null.

  • Threading: conversion only happens on the JS thread and holds no connection mutex.

  • Eager path: the refactor behaves the same as before.

  • Neither CI nor the C++ tests have built this code. executeLazy.spec.ts is the only test that reaches it, because the Jest mocks stub the lazy methods. It runs only in the iOS, Android and macOS harness jobs, which skip drafts. scripts/test-cpp.sh doesn't compile NitroSQLiteOperations.cpp or the new file either, so even a compile error would merge green. Please run the harness, by marking the PR ready or with a full workflow_dispatch. A host C++ test for the row storage would help too. (both)

  • ArrayBuffer::getExternalMemorySize() breaks the declared Nitro range. It first appears in react-native-nitro-modules 0.37.1, while the peer range is >=0.35.0. Every BLOB here is an owning copy, so (*blob)->size() gives the same number on every version. The alternative is raising the peer floor. (Claude, inline)

  • The cache and accessors are looked up through the prototype chain. hasProperty(runtime, "cache") is true for an inherited cache. If Object.prototype.cache exists, two item(0) calls return different objects, and the second throws "Cannot access a SQLite row while it is being converted". setProperty would also call an inherited setter. The same applies to item and toArray. Since this file already guards against prototype pollution, checking own properties and defining them with Object.defineProperty would be consistent. (Apex, inline)

  • Small items (Claude, inline):

    • item(i) rebuilds a PropNameID for every column on every unconverted row. The eager path builds them once per result, so reading rows one at a time can end up slower than eager.
    • Once every row is converted, _data.rows still holds N empty vectors until GC: about 24 MB per 1M rows.

Apex also noted, as hardening only, that getNativeState is dereferenced unchecked at lines 68 and 74. Nitro's null checks only exist in debug builds, so a null state would crash a release build. JS can't reach the owner today.

if (const auto* text = std::get_if<std::string>(&*value)) {
bytes += stringAllocationSize(*text);
} else if (const auto* blob = std::get_if<std::shared_ptr<ArrayBuffer>>(&*value)) {
bytes += (*blob)->getExternalMemorySize();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude (medium): getExternalMemorySize() was added to ArrayBuffer in react-native-nitro-modules 0.37.1, and the peer range still allows >=0.35.0. Apps on 0.35.x–0.37.0 would fail to compile. These BLOBs are owning copies, so (*blob)->size() gives the same result on every version.


jsi::Object SQLiteLazyQueryRows::toRows(jsi::Runtime& runtime) {
auto owner = toObject(runtime).asObject(runtime);
if (!owner.hasProperty(runtime, "cache")) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex (low): hasProperty also sees an inherited Object.prototype.cache, so no own cache gets created, and item(0) read twice stops returning the same object. setProperty on line 57 is a JS [[Set]], so an inherited setter would swallow the cache. Checking own properties and installing with Object.defineProperty would close this, matching the Object.create(null) guard right below. The same applies to item and toArray at lines 63–75.


std::vector<jsi::PropNameID> itemNames;
if (names == nullptr) {
itemNames = makeQueryColumnNames(runtime, _data.columnNames);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude (low): Each unconverted item() call builds a PropNameID for every column. toArray() and the eager converter build them once per result. A list that reads 5k rows through item(i) creates 5k × columns of them. Caching the PropNameIDs per result would close the gap.

chrispader and others added 7 commits December 2, 2026 22:52
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Compile lazy conversion on draft CI against Nitro 0.35.0 and the installed version. Keep row count stable after releasing positional storage.

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Leave Operations to native application jobs because its filesystem wrapper does not compile on Linux hosts.

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
chrispader and others added 3 commits December 4, 2026 13:14
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>

This branch was successfully deployed

1 active deployment
Preview — 7c6a3932 Deployed Dec 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant