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
This commit is contained in:
Mårten Kongstad 2023-09-11 11:10:02 +02:00
parent 7d63a84642
commit b5133f6ad4
3 changed files with 13 additions and 13 deletions

View file

@ -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<ClassElement>,
@ -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(),
);
}

View file

@ -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

View file

@ -137,14 +137,14 @@ fn open_single_file(matches: &ArgMatches, arg_name: &str) -> Result<Input> {
}
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(())