Skip to content

fix(sqlite-native): gate native read cache behind env var#4630

Draft
NathanFlurry wants to merge 1 commit into04-12-fix_pegboard_isolate_runner_config_dc_lookup_failuresfrom
04-12-fix_sqlite-native_gate_native_read_cache_behind_env_var
Draft

fix(sqlite-native): gate native read cache behind env var#4630
NathanFlurry wants to merge 1 commit into04-12-fix_pegboard_isolate_runner_config_dc_lookup_failuresfrom
04-12-fix_sqlite-native_gate_native_read_cache_behind_env_var

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 13, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude bot commented Apr 13, 2026

PR Review: fix(sqlite-native): gate native read cache behind env var

Summary: This PR gates the native SQLite VFS read cache behind the RIVETKIT_SQLITE_NATIVE_READ_CACHE env var (disabled by default) to align native behavior with the WASM VFS, which has no read cache.


Code Quality

read_cache_enabled() initialization (vfs.rs:106): Using a function-level static OnceLock is idiomatic and correct. The env var is read exactly once per process life and cached. Worth noting in a comment that this is intentionally process-scoped -- subsequent setenv calls after first initialization have no effect.

Option pattern (vfs.rs:296): Clean representation. read_cache_enabled.then(HashMap::new) is idiomatic Rust.

VfsContext.read_cache_enabled field (vfs.rs:155): Good decision to snapshot the flag at VFS creation rather than calling read_cache_enabled() per file open. Consistent across file handles opened through the same VFS instance.


Minor Issue

Unnecessary allocation in batch flush (vfs.rs:920):

When read_cache is None, the dirty buffer is still collected into a Vec and then immediately dropped. Not a correctness issue (std::mem::take correctly clears the dirty buffer regardless), but it allocates on every atomic commit when caching is disabled. Suggested fix -- take the BTreeMap directly and skip the Vec:

let dirty = std::mem::take(&mut state.dirty_buffer);
if let Some(read_cache) = state.read_cache.as_mut() {
    for (chunk_index, data) in dirty {
        let key = kv::get_chunk_key(file.file_tag, chunk_index);
        read_cache.insert(key.to_vec(), data);
    }
}
// dirty is dropped here if read_cache is None, no Vec allocation

Test Coverage

No tests for the env var gating. Given that OnceLock makes env-var-based flags hard to test in parallel suites, this is understandable. A doc comment on read_cache_enabled() noting that it reads once at first call and subsequent env var changes are ignored would prevent future confusion.


Documentation

The env var RIVETKIT_SQLITE_NATIVE_READ_CACHE is undocumented outside the code. If this is user-facing (operators tuning performance), it should appear in the self-hosting configuration docs. If developer-only, the code comment is sufficient.


Correctness

The change is correct. All cache interactions are properly guarded, and the no-cache path does not regress writes (data still goes through KV; the cache was purely an optimization). The WASM parity goal is achieved by defaulting the cache to disabled.


Overall: Solid, focused change. The one actionable suggestion is removing the unnecessary Vec collect in the flush path by taking the BTreeMap directly. Everything else is clean and idiomatic.

Generated with Claude Code

@NathanFlurry NathanFlurry changed the base branch from 04-12-fix_sqlite-native_replace_stderr_sql_trace_logging to graphite-base/4630 April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_gate_native_read_cache_behind_env_var branch from a60a42d to 9d72e25 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry changed the base branch from graphite-base/4630 to 04-12-fix_pegboard_isolate_runner_config_dc_lookup_failures April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_pegboard_isolate_runner_config_dc_lookup_failures branch from 0f0530c to 7702123 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_gate_native_read_cache_behind_env_var branch from 9d72e25 to 2bb44ed Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_gate_native_read_cache_behind_env_var branch from 2bb44ed to bf17632 Compare April 13, 2026 07:03
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