perf: replace Arc<String> with Arc<str>

Signed-off-by: Changyuan Lyu <changyuanl@google.com>
This commit is contained in:
Changyuan Lyu 2024-08-31 22:51:07 -07:00 committed by Lencerf
parent ed2a72ccd0
commit 5304e37ab5
16 changed files with 62 additions and 51 deletions

View file

@ -354,7 +354,7 @@ fn main_run(args: RunArgs) -> Result<(), Error> {
};
if args.entropy {
vm.add_virtio_dev("virtio-entropy".to_owned(), EntropyParam)
vm.add_virtio_dev("virtio-entropy", EntropyParam)
.context(error::CreateDevice)?;
}
#[cfg(target_os = "linux")]
@ -396,7 +396,7 @@ fn main_run(args: RunArgs) -> Result<(), Error> {
serde_aco::from_args(&vsock, &objects).context(error::ParseArg { arg: vsock })?;
match param {
VsockParam::Vhost(p) => vm
.add_virtio_dev("vhost-vsock".to_owned(), p)
.add_virtio_dev("vhost-vsock", p)
.context(error::CreateDevice)?,
};
}

View file

@ -71,7 +71,7 @@ pub trait UartRecv: Send + 'static {
}
struct ConsoleWorker<U: UartRecv> {
name: Arc<String>,
name: Arc<str>,
uart: U,
poll: Poll,
}
@ -139,7 +139,7 @@ impl<U: UartRecv> ConsoleWorker<U> {
#[derive(Debug)]
pub struct Console {
pub name: Arc<String>,
pub name: Arc<str>,
worker_thread: Option<JoinHandle<()>>,
exit_waker: Waker,
}
@ -148,7 +148,8 @@ const TOKEN_SHUTDOWN: Token = Token(1);
const TOKEN_STDIN: Token = Token(0);
impl Console {
pub fn new(name: Arc<String>, uart: impl UartRecv) -> Result<Self> {
pub fn new(name: impl Into<Arc<str>>, uart: impl UartRecv) -> Result<Self> {
let name = name.into();
let poll = Poll::new()?;
let waker = Waker::new(poll.registry(), TOKEN_SHUTDOWN)?;
let mut worker = ConsoleWorker {

View file

@ -143,7 +143,7 @@ struct Pl011Reg {
/// https://developer.arm.com/documentation/ddi0183/g
#[derive(Debug)]
pub struct Pl011<I> {
name: Arc<String>,
name: Arc<str>,
irq_line: Arc<I>,
reg: Arc<Mutex<Pl011Reg>>,
console: Console,
@ -156,7 +156,7 @@ where
pub fn new(base_addr: u64, irq_line: I) -> io::Result<Self> {
let irq_line = Arc::new(irq_line);
let reg = Arc::new(Mutex::new(Pl011Reg::default()));
let name = Arc::new(format!("pl011@{base_addr:#x}"));
let name: Arc<str> = Arc::from(format!("pl011@{base_addr:#x}"));
let pl011_recv = Pl011Recv {
irq_line: irq_line.clone(),
reg: reg.clone(),

View file

@ -183,7 +183,7 @@ struct SerialReg {
#[derive(Debug)]
pub struct Serial<I> {
name: Arc<String>,
name: Arc<str>,
irq_sender: Arc<I>,
reg: Arc<Mutex<SerialReg>>,
console: Console,
@ -283,7 +283,7 @@ where
}
struct SerialRecv<I: IrqSender> {
pub name: Arc<String>,
pub name: Arc<str>,
pub irq_sender: Arc<I>,
pub reg: Arc<Mutex<SerialReg>>,
}
@ -312,7 +312,7 @@ where
pub fn new(base_port: u16, irq_sender: I) -> io::Result<Self> {
let irq_sender = Arc::new(irq_sender);
let reg = Arc::new(Mutex::new(SerialReg::default()));
let name = Arc::new(format!("serial_{:#x}", base_port));
let name: Arc<str> = Arc::from(format!("serial_{:#x}", base_port));
let uart_recv = SerialRecv {
irq_sender: irq_sender.clone(),
name: name.clone(),

View file

@ -97,10 +97,7 @@ pub struct PciBus {
impl PciBus {
pub fn new() -> Self {
let devices = if cfg!(target_arch = "x86_64") {
let bridge = PciDevice::new(
Arc::new("host_bridge".to_owned()),
Arc::new(HostBridge::new()),
);
let bridge = PciDevice::new("host_bridge", Arc::new(HostBridge::new()));
HashMap::from([(Bdf(0), bridge)])
} else {
HashMap::new()
@ -122,7 +119,7 @@ impl PciBus {
}
}
pub fn reserve(&self, bdf: Option<Bdf>, name: Arc<String>) -> Option<Bdf> {
pub fn reserve(&self, bdf: Option<Bdf>, name: Arc<str>) -> Option<Bdf> {
self.segment.reserve(bdf, name)
}

View file

@ -92,12 +92,12 @@ impl MemRegionCallback for BarCallback {
#[derive(Debug)]
pub struct PciDevice {
pub name: Arc<String>,
pub name: Arc<str>,
pub dev: Arc<dyn Pci>,
}
impl PciDevice {
pub fn new(name: Arc<String>, dev: Arc<dyn Pci>) -> PciDevice {
pub fn new(name: impl Into<Arc<str>>, dev: Arc<dyn Pci>) -> PciDevice {
let config = dev.config();
let dev_bars = &config.get_header().bars;
for (index, dev_bar) in dev_bars.iter().enumerate() {
@ -114,6 +114,9 @@ impl PciDevice {
})),
}
}
PciDevice { name, dev }
PciDevice {
name: name.into(),
dev,
}
}
}

View file

@ -59,7 +59,7 @@ impl PciSegment {
}
}
pub fn reserve(&self, bdf: Option<Bdf>, name: Arc<String>) -> Option<Bdf> {
pub fn reserve(&self, bdf: Option<Bdf>, name: Arc<str>) -> Option<Bdf> {
let mut empty_dev = PciDevice {
name: name.clone(),
dev: Arc::new(EmptyDevice),

View file

@ -210,7 +210,7 @@ impl SlotBackend for MaskedCap {
#[derive(Debug)]
struct VfioCdev {
name: Arc<String>,
name: Arc<str>,
dev: Cdev,
}
@ -397,14 +397,17 @@ where
M: MsiSender,
{
pub fn new(
name: Arc<String>,
name: impl Into<Arc<str>>,
param: &VfioParam,
ioas: Arc<Ioas>,
msi_sender: M,
) -> Result<VfioPciDev<M>> {
let mut dev = Cdev::new(&param.cdev)?;
dev.attach_iommu_ioas(ioas)?;
let cdev = Arc::new(VfioCdev { dev, name });
let cdev = Arc::new(VfioCdev {
dev,
name: name.into(),
});
let region_config = cdev.dev.get_region_info(VfioPciRegion::CONFIG.raw())?;

View file

@ -136,21 +136,21 @@ pub struct BlockParam {
impl DevParam for BlockParam {
type Device = Block;
fn build(self, name: Arc<String>) -> Result<Block> {
fn build(self, name: impl Into<Arc<str>>) -> Result<Block> {
Block::new(self, name)
}
}
#[derive(Debug)]
pub struct Block {
name: Arc<String>,
name: Arc<str>,
config: Arc<BlockConfig>,
disk: File,
feature: BlockFeature,
}
impl Block {
pub fn new(param: BlockParam, name: Arc<String>) -> Result<Self> {
pub fn new(param: BlockParam, name: impl Into<Arc<str>>) -> Result<Self> {
let access_disk = error::AccessFile {
path: param.path.as_path(),
};
@ -171,7 +171,7 @@ impl Block {
feature |= BlockFeature::RO;
}
Ok(Block {
name,
name: name.into(),
disk,
config,
feature,

View file

@ -146,7 +146,7 @@ struct DeviceWorker<D, S>
where
S: IrqSender,
{
name: Arc<String>,
name: Arc<str>,
dev: D,
poll: Poll,
memory: Arc<RamBus>,
@ -162,7 +162,7 @@ where
S: IrqSender,
E: IoeventFd,
{
pub name: Arc<String>,
pub name: Arc<str>,
pub device_config: Arc<D::Config>,
pub reg: Arc<Register>,
pub queue_regs: Arc<Vec<Queue>>,
@ -192,7 +192,7 @@ where
}
pub fn new<R>(
name: Arc<String>,
name: impl Into<Arc<str>>,
dev: D,
memory: Arc<RamBus>,
registry: &R,
@ -201,6 +201,7 @@ where
where
R: IoeventFdRegistry<IoeventFd = E>,
{
let name = name.into();
let poll = Poll::new().context(error::CreatePoll)?;
let device_config = dev.config();
let mut dev_feat = dev.feature();
@ -250,7 +251,7 @@ where
state: WorkerState::Pending,
};
let handle = std::thread::Builder::new()
.name(name.as_ref().to_owned())
.name(name.to_string())
.spawn(move || {
let r = device_worker.do_work();
if let Err(e) = r {
@ -419,7 +420,7 @@ where
pub trait DevParam {
type Device;
fn build(self, name: Arc<String>) -> Result<Self::Device>;
fn build(self, name: impl Into<Arc<str>>) -> Result<Self::Device>;
fn needs_mem_shared_fd(&self) -> bool {
false
}

View file

@ -55,19 +55,19 @@ bitflags! {
#[derive(Debug)]
pub struct Entropy {
name: Arc<String>,
name: Arc<str>,
source: File,
config: Arc<EntropyConfig>,
}
impl Entropy {
pub fn new(name: Arc<String>) -> Result<Self> {
pub fn new(name: impl Into<Arc<str>>) -> Result<Self> {
let mut options = OpenOptions::new();
options.custom_flags(O_NONBLOCK).read(true);
let path = "/dev/urandom";
let file = options.open(path).context(error::AccessFile { path })?;
Ok(Entropy {
name,
name: name.into(),
source: file,
config: Arc::new(EntropyConfig),
})
@ -140,7 +140,7 @@ pub struct EntropyParam;
impl DevParam for EntropyParam {
type Device = Entropy;
fn build(self, name: Arc<String>) -> Result<Self::Device> {
fn build(self, name: impl Into<Arc<str>>) -> Result<Self::Device> {
Entropy::new(name)
}
}

View file

@ -74,7 +74,7 @@ const VHOST_USER_BACKEND_FS_UNMAP: u32 = 7;
#[derive(Debug)]
pub struct VuFs {
name: Arc<String>,
name: Arc<str>,
vu_dev: Arc<VuDev>,
config: Arc<FsConfig>,
feature: u64,
@ -84,7 +84,8 @@ pub struct VuFs {
}
impl VuFs {
pub fn new(param: VuFsParam, name: Arc<String>) -> Result<Self> {
pub fn new(param: VuFsParam, name: impl Into<Arc<str>>) -> Result<Self> {
let name = name.into();
let mut vu_dev = VuDev::new(param.socket)?;
let dev_feat = vu_dev.get_features()?;
let virtio_feat = VirtioFeature::from_bits_retain(dev_feat);
@ -167,7 +168,7 @@ pub struct VuFsParam {
impl DevParam for VuFsParam {
type Device = VuFs;
fn build(self, name: Arc<String>) -> Result<Self::Device> {
fn build(self, name: impl Into<Arc<str>>) -> Result<Self::Device> {
VuFs::new(self, name)
}

View file

@ -135,7 +135,7 @@ bitflags! {
#[derive(Debug)]
pub struct Net {
name: Arc<String>,
name: Arc<str>,
config: Arc<NetConfig>,
tap_sockets: Vec<File>,
feature: NetFeature,
@ -168,7 +168,7 @@ pub struct NetParam {
impl DevParam for NetParam {
type Device = Net;
fn build(self, name: Arc<String>) -> Result<Net> {
fn build(self, name: impl Into<Arc<str>>) -> Result<Net> {
Net::new(self, name)
}
}
@ -184,7 +184,7 @@ fn new_socket(dev_tap: Option<&Path>) -> Result<File> {
}
impl Net {
pub fn new(param: NetParam, name: Arc<String>) -> Result<Self> {
pub fn new(param: NetParam, name: impl Into<Arc<str>>) -> Result<Self> {
let mut socket = new_socket(param.tap.as_deref())?;
let max_queue_pairs = param.queue_pairs.map(From::from).unwrap_or(1);
setup_socket(&mut socket, param.if_name.as_deref(), max_queue_pairs > 1)?;
@ -202,7 +202,7 @@ impl Net {
dev_feat |= NetFeature::MQ;
}
let net = Net {
name,
name: name.into(),
config: Arc::new(NetConfig {
mac: param.mac,
max_queue_pairs,

View file

@ -45,14 +45,14 @@ pub struct VhostVsockParam {
impl DevParam for VhostVsockParam {
type Device = VhostVsock;
fn build(self, name: Arc<String>) -> Result<Self::Device> {
fn build(self, name: impl Into<Arc<str>>) -> Result<Self::Device> {
VhostVsock::new(self, name)
}
}
#[derive(Debug)]
pub struct VhostVsock {
name: Arc<String>,
name: Arc<str>,
vhost_dev: Arc<VhostDev>,
config: VsockConfig,
features: u64,
@ -60,7 +60,8 @@ pub struct VhostVsock {
}
impl VhostVsock {
pub fn new(param: VhostVsockParam, name: Arc<String>) -> Result<VhostVsock> {
pub fn new(param: VhostVsockParam, name: impl Into<Arc<str>>) -> Result<VhostVsock> {
let name = name.into();
let vhost_dev = match param.dev {
Some(dev) => VhostDev::new(dev),
None => VhostDev::new("/dev/vhost-vsock"),

View file

@ -184,7 +184,7 @@ pub struct VirtioPciRegisterMmio<M>
where
M: MsiSender,
{
name: Arc<String>,
name: Arc<str>,
reg: Arc<Register>,
queues: Arc<Vec<Queue>>,
irq_sender: Arc<PciIrqSender<M>>,

View file

@ -202,7 +202,7 @@ where
pub fn add_pvpanic(&mut self) -> Result<(), Error> {
let dev = PvPanic::new();
let pci_dev = PciDevice::new("pvpanic".to_owned().into(), Arc::new(dev));
let pci_dev = PciDevice::new("pvpanic", Arc::new(dev));
self.add_pci_dev(None, pci_dev)
}
@ -226,7 +226,7 @@ where
pub fn add_virtio_dev<D, P>(
&mut self,
name: String,
name: impl Into<Arc<str>>,
param: P,
) -> Result<Arc<VirtioPciDev<D, H>>, Error>
where
@ -236,7 +236,7 @@ where
if param.needs_mem_shared_fd() && !self.board.config.mem.has_shared_fd() {
return error::MemNotSharedFd.fail();
}
let name = Arc::new(name);
let name = name.into();
let bdf = self.board.pci_bus.reserve(None, name.clone()).unwrap();
let dev = param.build(name.clone())?;
if let Some(callback) = dev.mem_update_callback() {
@ -301,8 +301,12 @@ where
#[cfg(target_os = "linux")]
impl Machine<Kvm> {
pub fn add_vfio_dev(&mut self, name: String, param: VfioParam) -> Result<(), Error> {
let name = Arc::new(name);
pub fn add_vfio_dev(
&mut self,
name: impl Into<Arc<str>>,
param: VfioParam,
) -> Result<(), Error> {
let name = name.into();
let iommu = if let Some(iommu) = &self.iommu {
iommu.clone()
} else {