From a5b0ccdd026550f6e6efbb5af72454bdb59b01b5 Mon Sep 17 00:00:00 2001 From: Andrew Senetar Date: Tue, 29 Mar 2022 21:48:14 -0500 Subject: [PATCH] Improve performance of Directories.get_state() --- core/directories.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/core/directories.py b/core/directories.py index c0cf3a6e..0a937a65 100644 --- a/core/directories.py +++ b/core/directories.py @@ -222,14 +222,11 @@ class Directories: if state != DirectoryState.NORMAL: self.states[path] = state return state - - prevlen = 0 - # we loop through the states to find the longest matching prefix - # if the parent has a state in cache, return that state - for p, s in self.states.items(): - if p in path.parents and len(p.parts) > prevlen: - prevlen = len(p.parts) - state = s + # find the longest parent path that is in states and return that state if found + # NOTE: path.parents is ordered longest to shortest + for parent_path in path.parents: + if parent_path in self.states: + return self.states[parent_path] return state def has_any_file(self):