GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-101196: Make isdir/isfile/exists faster on Windows by mdboom · Pull Request #101324 · python/cpython Conversation
|
| benchmark | main | this PR | ratio |
|---|---|---|---|
| isdir exists | 27.3 us | 23.0 us | 0.84 |
| isdir symlink | 38.0 us | 33.1 us | 0.87 |
| isdir missing | 9.1 us | 6.5 us | 0.72 |
| isfile exists | 20.7 us | 16.7 us | 0.81 |
| isfile symlink | 32.4 us | 28.1 us | 0.87 |
| isfile missing | 9.0 us | 6.7 us | 0.74 |
| exists exists | 24.1 us | 19.2 us | 0.8 |
| exists symlink | 35.6 us | 29.4 us | 0.83 |
| exists missing | 9.0 us | 6.5 us | 0.72 |
So therefore, these operations are around 13%-28% faster, depending on whether the file exists, is a symlink etc.
NOTE: This does not improve the performance of pathlib.Path.is_dir and friends. The behavior there is slightly different in terms of error handling, but I think a similar approach could also be applied there. If this PR is acceptable, I plan to do that as follow-up work.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
fb3c6ac to
a07f1e7
Compare
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
A builtin |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Co-authored-by: Eryk Sun <[email protected]>
|
A few cases that are handled by
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I guess this means we don't have test coverage for them...? In any event, I agree we should probably try to cover these cases. I'll see what can be done.
My initial implementation from this morning did that, which I like because it doesn't repeat what's clearly battle-tested code, but it made things slower in the event of symlinks. I was almost ok with that given that that isn't the common case, but your solution seemed to make everything a little faster (ignoring these new corner cases). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There shouldn't be an issue with speed for common cases as long as you only fall back on
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| @unittest.skipIf(sys.platform != 'win32', "drive letters are a windows concept") | ||
| def test_isfile_driveletter(self): | ||
| current_drive = os.path.splitdrive(os.path.abspath(__file__))[0] + "\\" | ||
| self.assertFalse(os.path.isfile(current_drive)) |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The reason this is false is because a relative drive path like "C:" resolves to the working directory on the drive. Did you want to test a volume device path like r'\\.\C:' instead?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes. And this is exactly the thing that (erroneously) returns True without checking the error code from GetFileInformationByHandleEx.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@eryksun: Thanks for all of your help and pointers. I've implemented support for these corner cases by calling I'm a little stuck on writing unit tests for them. I was able to cover I'm not sure how to recreate the case you describe in For |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Looks good to me, but I'll add that there's a lot of code duplication in here; it might be worth it to refactor the code shortly after landing this PR, so the added code is more maintainable and readable. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I'll take a first pass at reducing some of this duplication. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
My read of the duplication is that there's just enough difference that it's probably not worth it. The main change I'd like to see (but wasn't requiring) is to add an option to If all the additional functions were using the same OS call, it would make sense to merge them all. But they're subtly different each time, and since this is for perf, we don't want to introduce more switches. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
+1 (I suggest adding that in a follow-up PR; let's land this.)
True that. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Michael, are you still working on refactoring the code? Steve and Erlend seem to be in agreement that it's ready to merge as is. I think it's ready. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Just heard that it's good to go as it is. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Sorry, @mdboom and @zooba, I could not cleanly backport this to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Sorry @mdboom and @zooba, I had trouble checking out the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This ought to be safe to backport from a public API perspective, but I'll let the bot figure out how easy it'll be. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Sorry, @mdboom and @zooba, I could not cleanly backport this to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Okay, it's not going to backport trivially :) If anyone's up for it, go ahead, otherwise I'll take a look when I get time. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
(It's relatively rare that we backport performance improvements, so if it ends up being complicated maybe we shouldn't) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
* main: (82 commits) pythongh-101670: typo fix in PyImport_ExtendInittab() (python#101723) pythonGH-99293: Document that `Py_TPFLAGS_VALID_VERSION_TAG` shouldn't be used. (#pythonGH-101736) no-issue: Add Dong-hee Na as the cjkcodecs codeowner (pythongh-101731) pythongh-101678: Merge math_1_to_whatever() and math_1() (python#101730) pythongh-101678: refactor the math module to use special functions from c11 (pythonGH-101679) pythongh-85984: Remove legacy Lib/pty.py code. (python#92365) pythongh-98831: Use opcode metadata for stack_effect() (python#101704) pythongh-101283: Version was just released, so should be changed in 3.11.3 (pythonGH-101719) pythongh-101283: Fix use of unbound variable (pythonGH-101712) pythongh-101283: Improved fallback logic for subprocess with shell=True on Windows (pythonGH-101286) pythongh-101277: Port more itertools static types to heap types (python#101304) pythongh-98831: Modernize CALL and family (python#101508) pythonGH-101696: invalidate type version tag in `_PyStaticType_Dealloc` (python#101697) pythongh-100221: Fix creating dirs in `make sharedinstall` (pythonGH-100329) pythongh-101670: typo fix in PyImport_AppendInittab() (pythonGH-101672) pythongh-101196: Make isdir/isfile/exists faster on Windows (pythonGH-101324) pythongh-101614: Don't treat python3_d.dll as a Python DLL when checking extension modules for incompatibility (pythonGH-101615) pythongh-100933: Improve `check_element` helper in `test_xml_etree` (python#100934) pythonGH-101578: Normalize the current exception (pythonGH-101607) pythongh-47937: Note that Popen attributes are read-only (python#93070) ...
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.