Skip to content

Conversation

@arun1729
Copy link
Owner

@arun1729 arun1729 commented Oct 11, 2025

This PR adds memory-mapped file support to improve store read performance by using mmap for faster file access when reading records from the store.

Added memory mapping initialization during store file setup with fallback to regular file I/O
Modified the read method to use memory-mapped reading when available
Implemented a new __read_until_mmap method that reads from memory-mapped files in chunks

@arun1729 arun1729 requested a review from Copilot October 11, 2025 14:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds memory-mapped file support to improve store read performance by using mmap for faster file access when reading records from the store.

  • Added memory mapping initialization during store file setup with fallback to regular file I/O
  • Modified the read method to use memory-mapped reading when available
  • Implemented a new __read_until_mmap method that reads from memory-mapped files in chunks

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +380 to +388
# Use memory mapping for faster reads on large files
try:
if os.path.getsize(self.store) > 0:
self.store_mem = mmap.mmap(self.store_file.fileno(), 0)
else:
self.store_mem = None
except (ValueError, OSError):
self.store_mem = None
self.logger.warning("Could not create memory map for store, falling back to file I/O")
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing import statement for mmap and os modules. The code uses mmap.mmap() and os.path.getsize() but these modules are not imported at the top of the file.

Copilot uses AI. Check for mistakes.
self.logger.warning("Could not create memory map for store, falling back to file I/O")
logger.info("Store for file init: " + self.store)

def close(self):
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The close() method should be updated to properly close the memory-mapped file (self.store_mem) before closing the regular file handle to prevent resource leaks.

Suggested change
def close(self):
def close(self):
if self.store_mem is not None:
self.store_mem.close()

Copilot uses AI. Check for mistakes.
chunk = self.store_mem[cursor:end_pos]

if len(chunk) == 0:
return data
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential issue when data is None and an empty chunk is encountered. The method would return None instead of an empty bytes object, which may cause inconsistent behavior compared to the original __read_until() method.

Suggested change
return data
return data if data is not None else b''

Copilot uses AI. Check for mistakes.
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting

Comment on lines +430 to +433
# Use memory-mapped file if available for faster reads
if self.store_mem is not None:
record = self.__read_until_mmap(position)
else:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 Badge Fall back when reads go past mmap length

When the store file already contains data at initialization, __init__ creates a fixed-size memory map. read then unconditionally routes every request through __read_until_mmap. After any subsequent save, position for the new record is greater than len(self.store_mem), so __read_until_mmap returns None on the first iteration and the caller receives no bytes for the record. In short, any records appended after the memory map is created are unreadable unless the process is restarted or the mapping is resized. Consider remapping when the file grows or falling back to direct file I/O when the requested position exceeds the map length.

Useful? React with 👍 / 👎.

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.

2 participants