Skip to content

Commit 2512b4c

Browse files
committed
initializing vectors with capacity
1 parent 8063f3a commit 2512b4c

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ignore = "0.4.23"
1515
itertools = "0.13.0"
1616
path-clean = "1.0.1"
1717
lazy_static = "1.5.0"
18+
num_cpus = "1.16.0"
1819
rayon = "1.10.0"
1920
regex = "1.11.1"
2021
serde = { version = "1.0.214", features = ["derive"] }

src/project_builder.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub struct ProjectBuilder<'a> {
3535
pub codeowners_file_path: PathBuf,
3636
}
3737

38+
const INITIAL_VECTOR_CAPACITY: usize = 1000;
39+
3840
impl<'a> ProjectBuilder<'a> {
3941
pub fn new(config: &'a Config, base_path: PathBuf, codeowners_file_path: PathBuf) -> Self {
4042
Self {
@@ -46,8 +48,7 @@ impl<'a> ProjectBuilder<'a> {
4648

4749
#[instrument(level = "debug", skip_all)]
4850
pub fn build(&mut self) -> Result<Project, Error> {
49-
let mut entry_types = Vec::new();
50-
51+
let mut entry_types = Vec::with_capacity(INITIAL_VECTOR_CAPACITY);
5152
let mut builder = WalkBuilder::new(&self.base_path);
5253
builder.hidden(false);
5354
let walkdir = builder.build();
@@ -56,7 +57,9 @@ impl<'a> ProjectBuilder<'a> {
5657
let entry = entry.change_context(Error::Io)?;
5758
entry_types.push(self.build_entry_type(entry)?);
5859
}
59-
self.build_project_from_entry_types(entry_types)
60+
let project = self.build_project_from_entry_types(entry_types)?;
61+
62+
Ok(project)
6063
}
6164

6265
fn build_entry_type(&mut self, entry: ignore::DirEntry) -> Result<EntryType, Error> {
@@ -101,7 +104,7 @@ impl<'a> ProjectBuilder<'a> {
101104
.fold(
102105
|| {
103106
(
104-
Vec::<ProjectFile>::new(),
107+
Vec::<ProjectFile>::with_capacity(INITIAL_VECTOR_CAPACITY),
105108
Vec::<Package>::new(),
106109
Vec::<VendoredGem>::new(),
107110
Vec::<DirectoryCodeownersFile>::new(),
@@ -223,7 +226,6 @@ fn build_project_file(path: PathBuf) -> ProjectFile {
223226
};
224227

225228
let first_line = content.lines().next();
226-
227229
let Some(first_line) = first_line else {
228230
return ProjectFile { path, owner: None };
229231
};

0 commit comments

Comments
 (0)