tdutils: mmap-backed BufferSlice for large file reads#2303
Open
Lordron wants to merge 1 commit into
Open
Conversation
During initial sync, DownloadShardState reads the persistent state file
via td::read_file, which allocates the entire file size as one heap
buffer. For current mainnet masterchain state this is ~7 GB held in RSS
across deserialization.
This change adds BufferAllocator::create_reader_mmap(fd, size) that
mmap's the file MAP_PRIVATE and constructs a BufferRaw whose data
pointer points into the mmap'd region. dec_ref_cnt handles munmap on
the last reference. A new td::read_file_mmap() helper wraps this, and
ReadFile::start_up opts into mmap for whole-file reads of 64 MB or
more (offset=0, size=-1).
Existing heap-backed code paths are untouched; BufferRaw adds a single
nullable pointer that defaults to null, and all callers access through
a new BufferRaw::data() accessor that picks between the inline array
and the external pointer.
Measured on a 32 GB host syncing mainnet masterchain state 60660942:
RSS at +5 min uptime: 30 GB -> 9 GB
Swap used at peak: 15 GB -> 65 MB
CPU usage: unchanged
Disk reads: +1 MB/s from mmap page faults (9% util)
DB persistence rate: unchanged
The 64 MB threshold avoids mmap overhead for small files while capturing
the target case of multi-GB persistent state files.
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.
During initial sync, DownloadShardState reads the persistent state file
via td::read_file, which allocates the entire file size as one heap
buffer. For current mainnet masterchain state this is ~7 GB held in RSS
across deserialization.
This change adds BufferAllocator::create_reader_mmap(fd, size) that
mmap's the file MAP_PRIVATE and constructs a BufferRaw whose data
pointer points into the mmap'd region. dec_ref_cnt handles munmap on
the last reference. A new td::read_file_mmap() helper wraps this, and
ReadFile::start_up opts into mmap for whole-file reads of 64 MB or
more (offset=0, size=-1).
Existing heap-backed code paths are untouched; BufferRaw adds a single
nullable pointer that defaults to null, and all callers access through
a new BufferRaw::data() accessor that picks between the inline array
and the external pointer.
Measured on a 32 GB host syncing mainnet masterchain state 60660942:
RSS at +5 min uptime: 30 GB -> 9 GB
Swap used at peak: 15 GB -> 65 MB
CPU usage: unchanged
Disk reads: +1 MB/s from mmap page faults (9% util)
DB persistence rate: unchanged
The 64 MB threshold avoids mmap overhead for small files while capturing
the target case of multi-GB persistent state files.