Skip to content

Commit f274d9a

Browse files
authored
Swap outer and inner loops in write_python_part() (#2861)
Of the two loops (walking the directory tree and iterating over the python packges), the directory traversal is the more expensive one. It should be the outer loop so we only do it once.
1 parent bcca65c commit f274d9a

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/module_writer/mod.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,35 +102,36 @@ pub fn write_python_part(
102102
python_packages.push(package_path);
103103
}
104104

105-
for package in python_packages {
106-
for absolute in WalkBuilder::new(&project_layout.project_root)
107-
.hidden(false)
108-
.parents(false)
109-
.git_global(false)
110-
.git_exclude(false)
111-
.build()
105+
for absolute in WalkBuilder::new(&project_layout.project_root)
106+
.hidden(false)
107+
.parents(false)
108+
.git_global(false)
109+
.git_exclude(false)
110+
.build()
111+
{
112+
let absolute = absolute?.into_path();
113+
if !python_packages
114+
.iter()
115+
.any(|path| absolute.starts_with(path))
112116
{
113-
let absolute = absolute?.into_path();
114-
if !absolute.starts_with(package.as_path()) {
115-
continue;
116-
}
117-
let relative = absolute.strip_prefix(python_dir).unwrap();
118-
if !absolute.is_dir() {
119-
// Ignore native libraries from develop, if any
120-
if let Some(extension) = relative.extension() {
121-
if extension.to_string_lossy() == "so" {
122-
debug!("Ignoring native library {}", relative.display());
123-
continue;
124-
}
117+
continue;
118+
}
119+
let relative = absolute.strip_prefix(python_dir).unwrap();
120+
if !absolute.is_dir() {
121+
// Ignore native libraries from develop, if any
122+
if let Some(extension) = relative.extension() {
123+
if extension.to_string_lossy() == "so" {
124+
debug!("Ignoring native library {}", relative.display());
125+
continue;
125126
}
126-
#[cfg(unix)]
127-
let mode = absolute.metadata()?.permissions().mode();
128-
#[cfg(not(unix))]
129-
let mode = 0o644;
130-
writer
131-
.add_file_with_permissions(relative, &absolute, mode)
132-
.context(format!("File to add file from {}", absolute.display()))?;
133127
}
128+
#[cfg(unix)]
129+
let mode = absolute.metadata()?.permissions().mode();
130+
#[cfg(not(unix))]
131+
let mode = 0o644;
132+
writer
133+
.add_file_with_permissions(relative, &absolute, mode)
134+
.context(format!("File to add file from {}", absolute.display()))?;
134135
}
135136
}
136137

0 commit comments

Comments
 (0)