Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SharedCache] Simplify MMappedFileAccessor::Read* methods #6315

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

bdash
Copy link
Contributor

@bdash bdash commented Jan 14, 2025

This change is mostly motivated by simplifying the code, but it also brings minor correctness and performance benefits.

  1. The pointer returned by mmap is stored as a uint8_t* rather than void* as that is how it is used. This eliminates a bunch of casts.
  2. Read methods for primitives delegate to a new Read template function that in turn delegates to the general-purpose Read(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.
  3. ReadNullTermString now uses std::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 of reserve followed by shrink_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.

This change is mostly motivated by simplifying the code, but it also
brings minor correctness and performance benefits.

1. The pointer returned by mmap is stored as a uint8_t* rather than
   void* as that is how it is used. This reduces how often it needs to
   be cast to a different type before it is used.
2. Read methods for primitives delegate to a new Read template function
   that in turn delegates to the general-purpose `Read(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 with no overhead.
3. ReadNullTermString now uses std::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
   reserve followed by shrink_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.
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