resizable_memory: use mprotect instead of mmap to resize on linux#13
resizable_memory: use mprotect instead of mmap to resize on linux#13
Conversation
WalkthroughThe Changes
Possibly related PRs
Estimated code review effort🎯 4 (Complex) | ⏱️ ~40 minutes Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🧰 Additional context used🧬 Code Graph Analysis (1)test/src/mappedfile.cpp (1)
🔇 Additional comments (8)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
include/decodeless/detail/mappedfile_linux.hpp(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: build (windows-latest, Release, cl)
- GitHub Check: build (windows-latest, Debug, cl)
🔇 Additional comments (4)
include/decodeless/detail/mappedfile_linux.hpp (4)
257-257: Clean implementation change in constructorDirectly calling
resize(initialSize)instead of the separatemap()method simplifies the initialization flow. This is more intuitive as it consolidates the memory allocation logic in theresize()method.
283-287: Well-designed protection helper methodThe new
protectmethod properly encapsulates themprotectcall with appropriate error handling. The pointer arithmetic is correctly implemented to target the specific memory region.
290-290: Appropriate tracking of mapped memory sizeThe new
m_mappedSizemember variable correctly tracks the page-aligned memory size that's actually mapped, which is necessary for the incremental protection approach. This separation from the logicalm_sizeis a good design choice.
250-291: Verify overall impact on memory reclamationThe implementation now properly supports unmapping pages when shrinking the memory region, allowing the OS to reclaim that memory. This should provide memory usage benefits in addition to the performance improvements mentioned in the PR description.
To confirm the memory reclamation behavior is working as expected, you could consider adding a test that:
- Allocates a large memory region
- Shrinks it significantly
- Verifies that the physical memory usage decreases
This would validate that the OS is properly reclaiming the memory when portions are unmapped.
2a4947a to
74c5514
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
include/decodeless/detail/mappedfile_linux.hpp(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: build (windows-latest, Release, cl)
- GitHub Check: build (windows-latest, Release, cl)
- GitHub Check: build (windows-latest, Debug, cl)
🔇 Additional comments (2)
include/decodeless/detail/mappedfile_linux.hpp (2)
257-258: Constructor switch-over toresize()is soundCalling
resize(initialSize)directly from the constructor eliminates the short-lived “partially-initialised” state that existed when we previously relied on a privatemap()call. The new approach is clearer and guarantees all invariants (m_size,m_mappedSize, protections) are set up in one place.
268-270: 🧹 Nitpick (assertive)Minor nit – avoid recomputing the page size inside hot paths
pageSize()is already cached, but it is still invoked every timeresize()is called. This is fine for most workloads, yet ifresize()is on a tight growth loop (e.g. bump allocator), the extra function call shows up in profiles.- size_t ps = pageSize(); + constexpr size_t ps = pageSize(); // forces compile-time constant after first callBecause
pageSize()isinlineand returns a static value, making the local variableconstexprlets compilers fold it away completely.Likely an incorrect or invalid review comment.
|
Resizing down now actually frees pages. Yes, |
599f911 to
977ad2a
Compare
Uses
mprotect()instead ofmmap()to add the additional pages. I'm amazed how much faster this is. Unmapping is supported too now and should allow the OS to reclaim unused pages.Before
Mapping 1048576 bytes in increments of 1009 bytes
mmap+mprotectresizable_memoryMapping 1048576 bytes in increments of 4096 bytes
mmap+mprotectresizable_memoryAfter
Mapping 1048576 bytes in increments of 1009 bytes
mmap+mprotectresizable_memoryMapping 1048576 bytes in increments of 4096 bytes
mmap+mprotectresizable_memorySummary by CodeRabbit