From b5133f6ad45109d1c986eb03d98cf98c44bbe683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Kongstad?= Date: Mon, 11 Sep 2023 11:10:02 +0200 Subject: [PATCH] aconfig: remove unnecessary clones Improve performance slightly: remove unnecessary clone operations, or use references where a new object is not needed. Bug: 283910447 Test: atest aconfig.test Change-Id: I75205ffa1723dd2654039baac882c225d2653c86 --- tools/aconfig/src/codegen_cpp.rs | 16 ++++++++-------- tools/aconfig/src/codegen_java.rs | 4 ++-- tools/aconfig/src/main.rs | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tools/aconfig/src/codegen_cpp.rs b/tools/aconfig/src/codegen_cpp.rs index 8c2d7ba07f..177587d29c 100644 --- a/tools/aconfig/src/codegen_cpp.rs +++ b/tools/aconfig/src/codegen_cpp.rs @@ -38,9 +38,9 @@ where let cpp_namespace = package.replace('.', "::"); ensure!(codegen::is_valid_name_ident(&header)); let context = Context { - header: header.clone(), - cpp_namespace, - package: package.to_string(), + header: &header, + cpp_namespace: &cpp_namespace, + package, readwrite, for_test: codegen_mode == CodegenMode::Test, class_elements, @@ -77,10 +77,10 @@ pub struct FileSpec<'a> { } #[derive(Serialize)] -pub struct Context { - pub header: String, - pub cpp_namespace: String, - pub package: String, +pub struct Context<'a> { + pub header: &'a str, + pub cpp_namespace: &'a str, + pub package: &'a str, pub readwrite: bool, pub for_test: bool, pub class_elements: Vec, @@ -517,7 +517,7 @@ void com_android_aconfig_test_reset_flags() { for file in generated { generated_files_map.insert( String::from(file.path.to_str().unwrap()), - String::from_utf8(file.contents.clone()).unwrap(), + String::from_utf8(file.contents).unwrap(), ); } diff --git a/tools/aconfig/src/codegen_java.rs b/tools/aconfig/src/codegen_java.rs index c31d715867..ac24244a0c 100644 --- a/tools/aconfig/src/codegen_java.rs +++ b/tools/aconfig/src/codegen_java.rs @@ -282,7 +282,7 @@ mod tests { None, crate::test::first_significant_code_diff( file_set.get(file_path).unwrap(), - &String::from_utf8(file.contents.clone()).unwrap() + &String::from_utf8(file.contents).unwrap() ), "File {} content is not correct", file_path @@ -362,7 +362,7 @@ mod tests { None, crate::test::first_significant_code_diff( file_set.get(file_path).unwrap(), - &String::from_utf8(file.contents.clone()).unwrap() + &String::from_utf8(file.contents).unwrap() ), "File {} content is not correct", file_path diff --git a/tools/aconfig/src/main.rs b/tools/aconfig/src/main.rs index 84073f7ce0..7e44baf3c9 100644 --- a/tools/aconfig/src/main.rs +++ b/tools/aconfig/src/main.rs @@ -137,14 +137,14 @@ fn open_single_file(matches: &ArgMatches, arg_name: &str) -> Result { } fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> { - let path = root.join(output_file.path.clone()); + let path = root.join(&output_file.path); let parent = path .parent() .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?; fs::create_dir_all(parent) .with_context(|| format!("failed to create directory {}", parent.display()))?; - let mut file = fs::File::create(path.clone()) - .with_context(|| format!("failed to open {}", path.display()))?; + let mut file = + fs::File::create(&path).with_context(|| format!("failed to open {}", path.display()))?; file.write_all(&output_file.contents) .with_context(|| format!("failed to write to {}", path.display()))?; Ok(())