Skip to content

Commit

Permalink
Remove a bunch more clones
Browse files Browse the repository at this point in the history
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 <deso@posteo.net>
  • Loading branch information
d-e-s-o authored and danobi committed Aug 29, 2024
1 parent db7dc9e commit 7534bb2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions src/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -651,7 +651,7 @@ impl Qemu {
/// Construct a QEMU instance backing a vmtest target.
///
/// Does not run anything yet.
pub fn new(updates: Sender<Output>, target: &Target, host_shared: &Path) -> Result<Self> {
pub fn new(updates: Sender<Output>, target: Target, host_shared: &Path) -> Result<Self> {
let qga_sock = gen_sock("qga");
let qmp_sock = gen_sock("qmp");
let command_sock = gen_sock("cmdout");
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/vmtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7534bb2

Please sign in to comment.