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
diff --git a/tools/aconfig/src/codegen_cpp.rs b/tools/aconfig/src/codegen_cpp.rs
index 8c2d7ba..177587d 100644
--- a/tools/aconfig/src/codegen_cpp.rs
+++ b/tools/aconfig/src/codegen_cpp.rs
@@ -38,9 +38,9 @@
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 @@
}
#[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<ClassElement>,
@@ -517,7 +517,7 @@
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 c31d715..ac24244 100644
--- a/tools/aconfig/src/codegen_java.rs
+++ b/tools/aconfig/src/codegen_java.rs
@@ -282,7 +282,7 @@
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 @@
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 84073f7..7e44baf 100644
--- a/tools/aconfig/src/main.rs
+++ b/tools/aconfig/src/main.rs
@@ -137,14 +137,14 @@
}
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(())