From 7534bb2907ca1986eb1e4d7ebf1ad03ede937a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Wed, 28 Aug 2024 13:29:21 -0700 Subject: [PATCH] Remove a bunch more clones MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We the Qemu::new() constructor might as well just consume the Target object, given that it won't be used afterwards. Adjust the signature and then just pull out the members we need instead of cloning them, removing a bunch of unnecessary allocations. Similarly, the CommandContext that we instantiate just for the sake of conveying some data to the template does not need to contain any owned objects. So just work with references to existing ones, eliminating a bunch of more clones. Signed-off-by: Daniel Müller --- src/qemu.rs | 26 +++++++++++++------------- src/vmtest.rs | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/qemu.rs b/src/qemu.rs index ecbac90..1a2aad2 100644 --- a/src/qemu.rs +++ b/src/qemu.rs @@ -76,15 +76,15 @@ pub struct Qemu { /// Used by templating engine to render command #[derive(Serialize)] -struct CommandContext { +struct CommandContext<'data> { /// True if command should change working directory before executing. should_cd: bool, /// Path to directory shared between guest/host - host_shared: PathBuf, + host_shared: &'data Path, /// User supplied command to run - command: String, + command: &'data str, /// virtio-serial output port name - command_output_port_name: String, + command_output_port_name: &'data str, } /// Used by templating engine to render init.sh @@ -173,7 +173,7 @@ fn init_script() -> String { fn gen_init(rootfs: &Path) -> Result<(NamedTempFile, PathBuf)> { let guest_temp_dir = std::env::temp_dir(); let mut host_dest_dir = rootfs.to_path_buf().into_os_string(); - host_dest_dir.push(guest_temp_dir.clone().into_os_string()); + host_dest_dir.push(&guest_temp_dir); let mut host_init = Builder::new() .prefix("vmtest-init") @@ -651,7 +651,7 @@ impl Qemu { /// Construct a QEMU instance backing a vmtest target. /// /// Does not run anything yet. - pub fn new(updates: Sender, target: &Target, host_shared: &Path) -> Result { + pub fn new(updates: Sender, target: Target, host_shared: &Path) -> Result { let qga_sock = gen_sock("qga"); let qmp_sock = gen_sock("qmp"); let command_sock = gen_sock("cmdout"); @@ -715,12 +715,12 @@ impl Qemu { process: c, qga_sock, qmp_sock, - command: target.command.to_string(), + command: target.command, command_sock, host_shared: host_shared.to_owned(), - rootfs: target.rootfs.clone(), - arch: target.arch.clone(), - mounts: target.vm.mounts.clone(), + rootfs: target.rootfs, + arch: target.arch, + mounts: target.vm.mounts, init: Some(init), updates, image: target.image.is_some(), @@ -777,9 +777,9 @@ impl Qemu { let context = CommandContext { // Only `cd` for kernel targets that share userspace with host should_cd: !self.image && self.rootfs == Target::default_rootfs(), - host_shared: self.host_shared.clone(), - command: self.command.clone(), - command_output_port_name: COMMAND_OUTPUT_PORT_NAME.into(), + host_shared: &self.host_shared, + command: &self.command, + command_output_port_name: COMMAND_OUTPUT_PORT_NAME, }; // Ignore errors cuz only trivial bugs are possible diff --git a/src/vmtest.rs b/src/vmtest.rs index 1ab2815..9ea509e 100644 --- a/src/vmtest.rs +++ b/src/vmtest.rs @@ -120,7 +120,7 @@ impl Vmtest { target.rootfs = self.resolve_path(target.rootfs.as_path()); target.vm.bios = target.vm.bios.map(|s| self.resolve_path(s.as_path())); - Qemu::new(updates, &target, &self.base).context("Failed to setup QEMU") + Qemu::new(updates, target, &self.base).context("Failed to setup QEMU") } /// Run a single target