Skip to content

refactor: replace string concatenation in monitor CLI option construc… - #858

Open
singhshresth26 wants to merge 1 commit into
urunc-dev:mainfrom
singhshresth26:issue-844-refactor-monitor-cli
Open

refactor: replace string concatenation in monitor CLI option construc…#858
singhshresth26 wants to merge 1 commit into
urunc-dev:mainfrom
singhshresth26:issue-844-refactor-monitor-cli

Conversation

@singhshresth26

@singhshresth26 singhshresth26 commented Jul 29, 2026

Copy link
Copy Markdown

Description

This PR refactors the monitor/hypervisor command line arguments construction to use string slices directly instead of inefficient string concatenation.

Previously, monitors (such as HVT, SPT, QEMU, Firecracker, and Cloud Hypervisor) built their CLI options as a single space-separated string using cli += " option" and split it into a string slice at the end using strings.Split. This is inefficient because strings are immutable in Go, leading to unnecessary memory allocations, and the split operation adds overhead.

This PR:

  • Updates the Unikernel interface method MonitorNetCli to return []string instead of string.
  • Updates MonitorBlockArgs.ExactArgs and MonitorCliArgs.OtherArgs to use []string instead of string.
  • Updates all hypervisors to construct their arguments directly as slices of strings, avoiding all string concatenations and subsequent splits.

Related issues

How was this tested?

Compiled the packages and verified that unit tests build successfully targeting Linux:

  • go build ./pkg/unikontainers/... - Passed successfully.
  • go test -c ./pkg/unikontainers/... - Passed successfully.

Checklist

  • I have read the contribution guide.
  • The linter passes locally (make lint).
  • The e2e tests of at least one tool pass locally (make test_ctr, make test_nerdctl, make test_docker, make test_crictl).
  • If LLMs were used: I have read the llm policy.

@netlify

netlify Bot commented Jul 29, 2026

Copy link
Copy Markdown

Deploy Preview for urunc canceled.

Name Link
🔨 Latest commit fbbaa3c
🔍 Latest deploy log https://app.netlify.com/projects/urunc/deploys/6a90b9941864a50008c659a4

@singhshresth26

Copy link
Copy Markdown
Author

Hi @cmainas, just following up on this PR, would love your feedback whenever you’re available.

@cmainas cmainas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @singhshresth26 ,

thank you for the PR. I have added some comments and a generic request. Please do not use Sprintf directly in append, instead construct the string first and then add append it. The code becomes much cleaner.

Also, can you rebase over main and apply the changes to hyperlight and take into consideration the changes in Solo5?

}
extraMonArgs := ukernel.MonitorCli()
exArgs = append(exArgs, extraMonArgs.OtherArgs...)
exArgs = append(exArgs, args.UnikernelPath)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let;s add the -- which is the Solo5 separator for Solo5 cli options and unikernel cl options.

Comment thread pkg/unikontainers/hypervisors/qemu.go Outdated

if args.VCPUs > 0 {
cmdString += fmt.Sprintf(" -smp %d", args.VCPUs)
exArgs = append(exArgs, "-smp", fmt.Sprintf("%d", args.VCPUs))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use strconv instead of fmt for integer to string convention.

Comment thread pkg/unikontainers/hypervisors/qemu.go Outdated
blockCli1 := fmt.Sprintf(" -device virtio-blk-pci,serial=%s,drive=%s,scsi=off", blockArg.ID, blockArg.ID)
blockCli2 := fmt.Sprintf(" -drive format=raw,if=none,id=%s,file=%s", blockArg.ID, blockArg.Path)
blockCli = blockCli1 + blockCli2
if len(blockCli) == 0 && blockArg.ID != "" && blockArg.Path != "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should always give priority to ID and Path. and then fallback to blcokCli.

Comment thread pkg/unikontainers/hypervisors/qemu.go Outdated
devType = "virtio-net-device"
}
netcli += fmt.Sprintf(" %s,host_mtu=%d,mac=%s", getVirtioNetArg(), args.Net.MTU, args.Net.MAC)
exArgs = append(exArgs, "-device", fmt.Sprintf("%s,netdev=net0,host_mtu=%d,mac=%s", devType, args.Net.MTU, args.Net.MAC))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Construct the netcli before appending it to make it cleaner.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also getVirtioNetArg has been removed.

cmdArgs := strings.Split(cmdString, " ")
return cmdArgs, nil
exArgs = append(exArgs, extraMonArgs.OtherArgs...)
exArgs = append(exArgs, args.UnikernelPath)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let;s add the -- separator for Solo5 cli options and unikernel cl options.

…tion (urunc-dev#844)

Signed-off-by: Shresth Singh <shresthengineer@gmail.com>
@singhshresth26
singhshresth26 force-pushed the issue-844-refactor-monitor-cli branch from 158fe2f to fbbaa3c Compare August 27, 2026 22:26
@singhshresth26

Copy link
Copy Markdown
Author

Sorry for the delay, I have addressed all your comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace the string concatenation in the monitor command line construction

2 participants