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
os.path.isdir() is slow on windows #55792
Comments
Report here: http://stackoverflow.com/questions/5316928/python-os-path-isdir-is-slow-on-windows It could be a problem with Windows itself, but I'm opening this to keep track of the issue, just to be on the safe side. |
How do you expect this to be resolved? Or will it stay open until Microsoft improves the NTFS performance? |
On Thu, Mar 17, 2011 at 12:46, Martin v. Löwis <report@bugs.python.org>wrote: I opened this in order not to forget to look at the implementation of isdir |
Ok. It's probably not the most optimal thing we can do. It opens the directory with CreateFileW (specifying OPEN_EXISTING and BACKUP_SEMANTICS), then GetFileInformationByHandle. If we only want to find out whether the file is a directory, we could just to FindFirstFile, which wouldn't need to go to the MFT record. I would be surprised if this makes a difference in practice, though. |
I made a bunch of the stat changes in 3.2 so I'll assign this to myself and take a look. |
Attached is a patch that makes this about twice as fast for regular files and about 15 times faster for symbolic links and directories. Not that this would be anyone's performance bottleneck, but it does make the time more of a constant due to the recursion and extra overhead when stat'ing symbolic paths. The numbers were tested with the patch on bpo-12084 (new stat impl). # The numbers were run before I hooked nt._isdir up through Lib/ntpath.py, so os.path.isdir is the old way.
# Regular directory
# testlink is a symbolically linked directory
# setup.py.link is a symbolic link
|
Looks like I didn't test this enough - many other test failures are occurring. Disregard this patch for now. |
One (presumably unintended) side-effect is that you can now do os.path.isdir on a wildcard and the first returned entry will be tested for directoryness: import os
os.path.isdir ("c:/tem*")
# => True where c:/temp exists |
Here's a patch that works. All tests are passing. I changed from using FindFirstFile to GetFileAttributes, which provides basically the same performance characteristics. One change in this implementation is to not raise a WindowsError when the file cannot be opened. The original os.stat would return False in that case, so now this function returns False (before it just passed on the WindowsError, which killed lots of tests). |
Code looks good and tests pass. Only one thing: the code in the patched lib\ntpath.py still refers to FindFirstFile |
New changeset 88e318166eaf by Brian Curtin in branch '3.2': New changeset 567f30527913 by Brian Curtin in branch 'default': |
This was also pushed to 2.7 in f1509fc75435. |
Brian - great, thanks. |
New changeset d40609dd01e0 by Brian Curtin in branch '3.2': New changeset fe813f5711a5 by Brian Curtin in branch '2.7': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: