bpo-28564: Use os.scandir() in shutil.rmtree() on Unix.#4085
bpo-28564: Use os.scandir() in shutil.rmtree() on Unix.#4085serhiy-storchaka merged 7 commits intopython:masterfrom
Conversation
This speeds up it to 20%.
|
What about the unsafe variant? Can't it benefit from a similar optimization? |
|
Good question! Using |
| try: | ||
| orig_st = os.stat(name, dir_fd=topfd, follow_symlinks=False) | ||
| mode = orig_st.st_mode | ||
| is_dir = entry.is_dir(follow_symlinks=False) |
There was a problem hiding this comment.
Can you explain why you are doing this is_dir call before re-stat()ing below?
There was a problem hiding this comment.
If is_dir is false, we can avoid calling stat(). On Posix scandir() usually fills the is_dir bit, but stat() needs a syscall.
There was a problem hiding this comment.
But then why is stat() needed at all? Isn't is_dir() sufficient?
There was a problem hiding this comment.
Oh, I hadn't noticed the use of orig_st below. My bad.
| # os.scandir above. | ||
| raise OSError("Cannot call rmtree on a symbolic link") | ||
| except OSError: | ||
| onerror(os.path.islink, fullname, sys.exc_info()) |
There was a problem hiding this comment.
Shouldn't you add a return just after this?
There was a problem hiding this comment.
Good catch. But rather continue.
This speeds up it to 20-40%.
This speeds up it to 20%.
https://bugs.python.org/issue28564