Skip to content

Use mmap_mode for probing#86

Open
nh2 wants to merge 1 commit into
dranjan:masterfrom
nh2:can_mmap-mode
Open

Use mmap_mode for probing#86
nh2 wants to merge 1 commit into
dranjan:masterfrom
nh2:can_mmap-mode

Conversation

@nh2

@nh2 nh2 commented Jul 21, 2026

Copy link
Copy Markdown

Fixes plyfile incorrectly falling back to non-mmap read even when mmap is supported.

Until now, for example, when the user wanted a read-only ("r") mmap and that would succeed, plyfile's _can_mmap() check would not use "r" but "c" for the check, which would fail for large files.

That in turn would then cause OOMs, because with _can_mmap() == False it would fall back to normal full reading.


📚 Documentation preview 📚: https://python-plyfile--86.org.readthedocs.build/en/86/

@dranjan

dranjan commented Jul 21, 2026

Copy link
Copy Markdown
Owner

Hey @nh2, thanks for the patch! This looks pretty simple, so it shouldn't take long to review and merge.

By the way, the documentation preview action failed because your branch was made from an earlier release, and the latest commits on master are needed for readthedocs.org to do the build. You don't need to do anything about this.

@dranjan dranjan left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The changes make sense to me.

Besides my one comment about the docstring, the only other thing I can think of at the moment is that it would be nice to add a test for this bug fix, but I can't immediately think of how. I guess we would need to make a file object that's readable and memory-mappable but can't be memory-mapped in copy-on-write mode, but if we need to trigger the memory overcommitment issue, I don't think we can do that in a unit test. If you can think of a solution, do let me know. However, to be clear, this isn't blocking the pull request.

If you want to try to address the docstring thing, let me know. Otherwise, I'll probably make the changes and merge later this week and then push a point release.

Comment thread plyfile.py Outdated
Comment thread plyfile.py
# Loading the data is straightforward. We will memory
# map the file in copy-on-write mode.
mmap_mode = mmap if isinstance(mmap, str) else 'c'
# map the file in the requested mode.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This comment was wrong! Thanks for fixing it.

@nh2

nh2 commented Jul 21, 2026

Copy link
Copy Markdown
Author

but if we need to trigger the memory overcommitment issue, I don't think we can do that in a unit test

I agree; I tested it locally by just mapping a PLY file that's larger than my memory, but the exact behaviour of that depends on the overcommit settings of the machine, the amount of RAM, and Linux's heuristics if the vm.overcommit_memory setting is 0. So I don't think we can make a test for it that won't be at least a little bit dependent on the system of whoever runs it.

@nh2
nh2 force-pushed the can_mmap-mode branch from 4a9753f to 30c51a5 Compare July 22, 2026 00:19
Fixes plyfile incorrectly falling back to non-mmap read even
when mmap is supported.

Until now, for example, when the user wanted a read-only ("r") mmap
and that would succeed, plyfile's `_can_mmap()` check would not
use "r" but "c" for the check, which would fail for large files.
@nh2
nh2 force-pushed the can_mmap-mode branch from 30c51a5 to 07dd7a6 Compare July 22, 2026 00:20
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