Repository navigation
Reduce peak memory during native folder adoption - #2035
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
The native adoption reader reserved 16 MiB plus one byte for every note in both passes that build the initial sync snapshot. The second pass retained all those buffers in the current bucket. Start with a 16 KiB read, grow the bound by 16 on a full read, and trim each accepted document before the bucket retains it. Even a file at the admission limit takes at most four reads.
Each read starts at offset zero on a confined regular file. A short read proves EOF on that handle. At the admission limit, the final request reads up to 16 MiB plus one byte to distinguish an exact fit from an oversized file. The existing digest check between inventory and sealing still detects changed contents. A regression test replaces the file between reads and checks exact-length and over-limit inputs.
Private adoption on 10,000 Markdown files
Measurements compare
9e58e4578with its adoption patch before rebase (c2f89aee6). The PR reapplies the same adoption change to6d9f7cc95(head60092dff8). Linux x86-64 on a Ryzen 9 7940HS. Each run created a fresh collection and a loopback memory log. The release-profile rehearsal enabled debug assertions only for the daemon test credential backend. The fixture held 12,262,780 bytes of Markdown and one 50,000-byte attachment. Both builds used the same four-CPU affinity, two Tokio workers and allocator instrumentation within each series. Filesystem caches were left warm. Each cell is the median of three alternating A/B pairs. The allocation counter sums requested bytes over the life of the adopter process. Peak RSS measures resident memory.Individual ext4 confirmation times in seconds were A [78.46, 56.97, 32.80] and B [53.20, 59.43, 61.86]. Shared-disk load varied during the run. Linux wait4 supplied process peak RSS. A glibc interposer counted bytes requested through malloc, calloc and realloc, including repeated reservations. Each baseline made 20,000 requests of exactly 16 MiB plus one byte. Each candidate made zero.
The workload checked source bytes after adoption, all 256 confirmed revisions after the edit burst, an empty pending queue, all 10,000 original IDs, and a byte-identical second folder including the attachment. All six final tmpfs runs finished with zero holds and zero unresolved conflicts. The read change runs during adoption. The warm, bulk and second-replica rows describe adjacent behavior.
Read profile
The new rehearsal subcommand times the inventory pass and then holds each production-sized generation-zero bucket of documents while it reads that bucket. Nine alternating A/B pairs used a fixed plan for each corpus. The two-pass time is the median of the per-run sums. Sorting and sealing are outside these timers.
The mixed corpus put 900 files just over 16 KiB, 90 just over 256 KiB, and 10 just over 4 MiB. It exposes the extra reads at growth boundaries. The mixed two-pass median increased, even though bucket reservations fell.
A separate strace run of the authenticated headless daemon on 10,000 Markdown files measured a 13.97 s local-only first scan. The whole traced request path made 43,014 statx calls and 33,074 openat calls. In the traced Private workload, the warm watcher delivered 262 queued events in 24 batches and performed one debounced observation. Fresh-replica verification and materialization made 486,177 pwrite64 calls, 10,514 renameat2 calls and 1,110 fsync calls. Trace timings include instrumentation overhead. The numbers describe local synthetic workloads. Production startup and settle timings need a separate retest.
Five separately instrumented reads of both passes on the fixture spent a median 12.8 ms hashing the returned bytes with SHA-256. The whole traced local daemon spent 4.60 aggregate syscall seconds in futex across 687 calls. That aggregate includes ordinary sleeping threads and does not identify a contended lock.
Verification
pnpm ci:localsteps passed except the initialpnpm testinvocation. Two fake-CLI canary deadlines expired. All 331 root tests passed when run with--test-concurrency=1, and all 208 Desktop tests passed. One shared UI focus test timed out during the first recursive run. All remaining workspace package suites passed with--workspace-concurrency=1andVITEST_MAX_WORKERS=2, including all 506 shared UI tests. These reruns used the original test deadlines and normal database-dependent skips.Reproduction
Run from the PR root on Linux with glibc, Python 3, a C compiler and the pinned Rust toolchain. The commands build both measured variants from the original base and copy the PR's rehearsal into both. Place
benchon ext4. Each adoption uses a new plan, collection, state directory and loopback log server. The focused comparison reuses a single plan within each corpus. Replace the CPU lists with available CPUs on your machine.Save the two blocks below as
$bench/alloc.cand$bench/run.py. SetW9_VOLUMEto a new directory on each filesystem. For ext4 the driver stops after validating the initial adoption. On tmpfs it also edits 256 files, checks the confirmed revisions and verifies materialization.Allocation counter and measurement driver
Refs #752 (introduced the bounded native adoption read).