Skip to content

Add DatabaseLoadOptions.BypassOsPageCache (fcntl F_NOCACHE on macOS) - #38

Open
hadashiA wants to merge 1 commit into
mainfrom
claude/nocache-option
Open

hadashiA wants to merge 1 commit into
mainfrom
claude/nocache-option

Conversation

@hadashiA

@hadashiA hadashiA commented Sep 7, 2026

Copy link
Copy Markdown
Owner

Adds an opt-in DatabaseLoadOptions.BypassOsPageCache that asks the OS not to retain the database file's pages in its own page cache, so the only in-memory copy of the data is DryDB's page cache (no double caching).

  • When to use: deployments where the OS page cache is charged to the process (cgroup memory limits in containers, consoles/mobile) and the DryDB cache is sized to hold the working set. Leave it off when a small DryDB cache relies on the OS cache as a second level — with the flag on, an evicted page is re-read from disk.
  • Scope: best effort. Implemented on macOS via fcntl(F_NOCACHE); a documented no-op on other platforms and for non-file streams / custom loaders. (Linux equivalents — O_DIRECT needs aligned reads that the variable-length page format doesn't provide, posix_fadvise(DONTNEED) needs per-read calls — are left for later.)

The interesting part: fcntl is variadic, and the Apple arm64 ABI passes variadic arguments on the stack, so the naive DllImport int fcntl(int, int, int) hands the callee garbage (it "works" only by luck of nonzero stack bytes). The import here declares eight fixed integer parameters after cmd so the ninth lands exactly where the callee's va_list starts on arm64, while x86-64's System V convention picks up the first duplicated register slot — passing the value in every slot satisfies both. A test proves the argument survives the call by round-tripping FD_CLOEXEC through F_SETFD/F_GETFD.

All 88 tests pass (85 existing + ABI round-trip, F_NOCACHE success, and an end-to-end read with the flag enabled).

🤖 Generated with Claude Code

Opt-in flag asking the OS not to retain the database file's pages in its
own page cache, so the only in-memory copy is DryDB's page cache — for
deployments where page cache is charged to the process (containers,
consoles) and the DryDB cache is sized to hold the working set. Best
effort: implemented on macOS via fcntl(F_NOCACHE); a no-op elsewhere and
for non-file streams.

fcntl is variadic and the Apple arm64 ABI passes variadic arguments on
the stack, so a naive 3-parameter P/Invoke would hand the callee garbage.
The import declares eight fixed integer parameters after cmd so the ninth
lands in the outgoing stack area where the callee's va_list starts on
arm64 (x86-64 reads the first duplicate register instead); a test
round-trips FD_CLOEXEC through F_SETFD/F_GETFD to prove the argument
survives the call on the running platform.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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