[SharedCache] Simplify MMappedFileAccessor::Read* methods #6315
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.
This change is mostly motivated by simplifying the code, but it also brings minor correctness and performance benefits.
mmap
is stored as auint8_t*
rather thanvoid*
as that is how it is used. This eliminates a bunch of casts.Read
template function that in turn delegates to the general-purposeRead(void* dest, size_t address, size_t length)
. This improves the consistency of bounds checking and simplifies the code. The compiler is more than willing to inline this so we get less repetition and consistent bounds checking with no additional overhead.ReadNullTermString
now usesstd::find
to find the NUL byte and directly constructs the string from that range of bytes. This removes an unnecessary allocation that was previously being forced by the use ofreserve
followed byshrink_to_fit
. It also avoids repeated reallocation for longer strings as they grew past the the reserved size as they were being built up a character at a time.