Skip to content

Commit 29c57b3

Browse files
⚡ perf: optimize memory usage by avoiding unnecessary cloning of entries on iter (#51)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 5c2c8c5 commit 29c57b3

File tree

1 file changed

+55
-33
lines changed

1 file changed

+55
-33
lines changed

src/git_manager.rs

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -818,35 +818,58 @@ impl GitManager {
818818

819819
/// Initialize submodule - add it first if not registered, then initialize
820820
pub fn init_submodule(&mut self, name: &str) -> Result<(), SubmoduleError> {
821-
let submodules = self.config.clone().submodules;
822-
let config = submodules
823-
.get(name)
824-
.ok_or_else(|| SubmoduleError::SubmoduleNotFound {
825-
name: name.to_string(),
826-
})?;
821+
let (
822+
path_str,
823+
url_str,
824+
branch,
825+
ignore,
826+
update,
827+
fetch_recurse,
828+
shallow,
829+
sparse_paths_opt,
830+
) = {
831+
let config = self.config
832+
.get_submodule(name)
833+
.ok_or_else(|| SubmoduleError::SubmoduleNotFound {
834+
name: name.to_string(),
835+
})?;
827836

828-
let path_str = config.path.as_ref().ok_or_else(|| {
829-
SubmoduleError::ConfigError("No path configured for submodule".to_string())
830-
})?;
831-
let url_str = config.url.as_ref().ok_or_else(|| {
832-
SubmoduleError::ConfigError("No URL configured for submodule".to_string())
833-
})?;
837+
let path_str = config.path.as_ref().ok_or_else(|| {
838+
SubmoduleError::ConfigError("No path configured for submodule".to_string())
839+
})?.clone();
834840

835-
let submodule_path = Path::new(path_str);
841+
let url_str = config.url.as_ref().ok_or_else(|| {
842+
SubmoduleError::ConfigError("No URL configured for submodule".to_string())
843+
})?.clone();
836844

837-
if submodule_path.exists() && submodule_path.join(".git").exists() {
838-
if self.verbose {
839-
println!("✅ {name} already initialized");
840-
}
841-
// Even if already initialized, check if we need to configure sparse checkout
842845
let sparse_paths_opt = self
843846
.config
844847
.submodules
845848
.sparse_checkouts()
846849
.and_then(|sparse_checkouts| sparse_checkouts.get(name).cloned());
850+
851+
(
852+
path_str,
853+
url_str,
854+
config.branch.clone(),
855+
config.ignore,
856+
config.update.clone(),
857+
config.fetch_recurse,
858+
config.shallow.unwrap_or(false),
859+
sparse_paths_opt,
860+
)
861+
};
862+
863+
let submodule_path = Path::new(&path_str);
864+
865+
if submodule_path.exists() && submodule_path.join(".git").exists() {
866+
if self.verbose {
867+
println!("✅ {name} already initialized");
868+
}
869+
// Even if already initialized, check if we need to configure sparse checkout
847870
if let Some(sparse_paths) = sparse_paths_opt {
848871
let use_git_default = self.effective_use_git_default_sparse_checkout(name);
849-
self.configure_sparse_checkout(path_str, &sparse_paths, use_git_default)?;
872+
self.configure_sparse_checkout(&path_str, &sparse_paths, use_git_default)?;
850873
}
851874
return Ok(());
852875
}
@@ -870,13 +893,13 @@ impl GitManager {
870893
// Submodule not registered yet, add it first via GitOpsManager
871894
let opts = crate::config::SubmoduleAddOptions {
872895
name: name.to_string(),
873-
path: std::path::PathBuf::from(path_str),
874-
url: url_str.to_string(),
875-
branch: config.branch.clone(),
876-
ignore: config.ignore,
877-
update: config.update.clone(),
878-
fetch_recurse: config.fetch_recurse,
879-
shallow: config.shallow.unwrap_or(false),
896+
path: std::path::PathBuf::from(&path_str),
897+
url: url_str,
898+
branch,
899+
ignore,
900+
update,
901+
fetch_recurse,
902+
shallow,
880903
no_init: false,
881904
};
882905
self.git_ops
@@ -885,12 +908,12 @@ impl GitManager {
885908
} else {
886909
// Submodule is registered, just initialize and update using GitOperations
887910
self.git_ops
888-
.init_submodule(path_str)
911+
.init_submodule(&path_str)
889912
.map_err(Self::map_git_ops_error)?;
890913

891914
let update_opts = crate::config::SubmoduleUpdateOptions::default();
892915
self.git_ops
893-
.update_submodule(path_str, &update_opts)
916+
.update_submodule(&path_str, &update_opts)
894917
.map_err(Self::map_git_ops_error)?;
895918
}
896919

@@ -899,11 +922,10 @@ impl GitManager {
899922
}
900923

901924
// Configure sparse checkout if specified
902-
if let Some(sparse_checkouts) = submodules.sparse_checkouts()
903-
&& let Some(sparse_paths) = sparse_checkouts.get(name) {
904-
let use_git_default = self.effective_use_git_default_sparse_checkout(name);
905-
self.configure_sparse_checkout(path_str, sparse_paths, use_git_default)?;
906-
}
925+
if let Some(sparse_paths) = sparse_paths_opt {
926+
let use_git_default = self.effective_use_git_default_sparse_checkout(name);
927+
self.configure_sparse_checkout(&path_str, &sparse_paths, use_git_default)?;
928+
}
907929

908930
if self.verbose {
909931
println!("✅ {name} initialized");

0 commit comments

Comments
 (0)