Skip to content

Commit

Permalink
backend: Use boxed message contents
Browse files Browse the repository at this point in the history
Replaces Vec<T> with Box<[T]> and String with Box<str> wherever modifications can't be sent to PipeWire,
except for Profiler profilings, since they are sent very frequently and .into_boxed_slice reallocates and copies.
  • Loading branch information
dimtpap committed Oct 6, 2024
1 parent 899d137 commit 8734882
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/backend/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl BoundGlobal {
if let Global::Metadata(ref metadata) = self.global {
metadata.set_property(
subject,
key.as_str(),
key.as_ref(),
type_.as_deref(),
value.as_deref(),
);
Expand Down
60 changes: 42 additions & 18 deletions src/backend/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub fn module(module: pw::module::Module, id: u32, on_event: impl Fn(Event) + 's
.add_listener_local()
.info({
move |info| {
let name = ("Name", info.name().to_owned());
let filename = ("Filename", info.filename().to_owned());
let name = ("Name", info.name().into());
let filename = ("Filename", info.filename().into());

let infos: Box<[(&str, String)]> = if let Some(args) = info.args() {
Box::new([name, filename, ("Arguments", args.to_owned())])
let infos: Box<[_]> = if let Some(args) = info.args() {
Box::new([name, filename, ("Arguments", args.into())])
} else {
Box::new([name, filename])
};
Expand All @@ -55,8 +55,8 @@ pub fn factory(factory: pw::factory::Factory, id: u32, on_event: impl Fn(Event)
.info({
move |info| {
let infos = Box::new([
("Type", info.type_().to_string()),
("Version", info.version().to_string()),
("Type", info.type_().to_string().into_boxed_str()),
("Version", info.version().to_string().into_boxed_str()),
]);

on_event(Event::GlobalInfo(id, infos));
Expand Down Expand Up @@ -132,12 +132,24 @@ pub fn node(node: pw::node::Node, id: u32, on_event: impl Fn(Event) + 'static) -
pw::node::NodeState::Running => "Running",
pw::node::NodeState::Error(e) => e,
}
.to_owned();
.into();
let infos = Box::new([
("Max Input Ports", info.max_input_ports().to_string()),
("Max Output Ports", info.max_output_ports().to_string()),
("Input Ports", info.n_input_ports().to_string()),
("Output Ports", info.n_output_ports().to_string()),
(
"Max Input Ports",
info.max_input_ports().to_string().into_boxed_str(),
),
(
"Max Output Ports",
info.max_output_ports().to_string().into_boxed_str(),
),
(
"Input Ports",
info.n_input_ports().to_string().into_boxed_str(),
),
(
"Output Ports",
info.n_output_ports().to_string().into_boxed_str(),
),
("State", state),
]);

Expand Down Expand Up @@ -165,7 +177,7 @@ pub fn port(port: pw::port::Port, id: u32, on_event: impl Fn(Event) + 'static) -
pw::spa::utils::Direction::Output => "Output",
_ => "Invalid",
}
.to_owned();
.into();

on_event(Event::GlobalInfo(id, Box::new([("Direction", direction)])));

Expand Down Expand Up @@ -195,12 +207,24 @@ pub fn link(link: pw::link::Link, id: u32, on_event: impl Fn(Event) + 'static) -
pw::link::LinkState::Unlinked => "Unlinked",
pw::link::LinkState::Error(e) => e,
}
.to_owned();
.into();
let infos = Box::new([
("Input Node ID", info.input_node_id().to_string()),
("Intput Port ID", info.input_port_id().to_string()),
("Output Node ID", info.output_node_id().to_string()),
("Output Port ID", info.output_port_id().to_string()),
(
"Input Node ID",
info.input_node_id().to_string().into_boxed_str(),
),
(
"Intput Port ID",
info.input_port_id().to_string().into_boxed_str(),
),
(
"Output Node ID",
info.output_node_id().to_string().into_boxed_str(),
),
(
"Output Port ID",
info.output_port_id().to_string().into_boxed_str(),
),
("State", state),
]);

Expand Down Expand Up @@ -251,7 +275,7 @@ pub fn metadata(
on_event(Event::MetadataProperty {
id,
subject,
key: key.map(ToOwned::to_owned),
key: key.map(|k| k.into()),
type_: type_.map(ToOwned::to_owned),
value: value.map(ToOwned::to_owned),
});
Expand Down
14 changes: 7 additions & 7 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum ObjectMethod {
num: u32,
},
ClientUpdatePermissions(Vec<pw::permissions::Permission>),
ClientUpdateProperties(std::collections::BTreeMap<String, String>),
ClientUpdateProperties(std::collections::BTreeMap<Box<str>, String>),
MetadataSetProperty {
subject: u32,
key: String,
Expand All @@ -53,19 +53,19 @@ pub enum Request {
props: Option<Vec<(String, String)>>,
},
GetContextProperties,
UpdateContextProperties(std::collections::BTreeMap<String, String>),
UpdateContextProperties(std::collections::BTreeMap<Box<str>, String>),
CallObjectMethod(u32, ObjectMethod),
}

pub enum Event {
GlobalAdded(
u32,
pw::types::ObjectType,
Option<std::collections::BTreeMap<String, String>>,
Option<std::collections::BTreeMap<Box<str>, String>>,
),
GlobalRemoved(u32),
GlobalInfo(u32, Box<[(&'static str, String)]>),
GlobalProperties(u32, std::collections::BTreeMap<String, String>),
GlobalInfo(u32, Box<[(&'static str, Box<str>)]>),
GlobalProperties(u32, std::collections::BTreeMap<Box<str>, String>),
ClientPermissions(
u32,
// Let's keep this as similar to PipeWire's message as possible
Expand All @@ -76,11 +76,11 @@ pub enum Event {
MetadataProperty {
id: u32,
subject: u32,
key: Option<String>,
key: Option<Box<str>>,
type_: Option<String>,
value: Option<String>,
},
ContextProperties(std::collections::BTreeMap<String, String>),
ContextProperties(std::collections::BTreeMap<Box<str>, String>),
Stop,
}

Expand Down
12 changes: 6 additions & 6 deletions src/backend/pipewire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub fn pipewire_thread(
}

if context
.load_module(name.as_str(), args.as_deref(), props)
.load_module(name.as_ref(), args.as_deref(), props)
.is_err()
{
eprintln!("Failed to load module: Name: {name} - Directory: {module_dir:?} - Arguments: {args:?}");
Expand Down Expand Up @@ -230,11 +230,11 @@ pub fn pipewire_thread(
}

let infos = Box::new([
("Name", info.name().to_owned()),
("Hostname", info.host_name().to_owned()),
("Username", info.user_name().to_owned()),
("Version", info.version().to_owned()),
("Cookie", info.cookie().to_string()),
("Name", info.name().into()),
("Hostname", info.host_name().into()),
("Username", info.user_name().into()),
("Version", info.version().into()),
("Cookie", info.cookie().to_string().into_boxed_str()),
]);

send(Event::GlobalInfo(0, infos));
Expand Down
4 changes: 2 additions & 2 deletions src/backend/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ where
}

pub fn key_val_to_props(
kv: impl Iterator<Item = (impl Into<Vec<u8>>, impl Into<Vec<u8>>)>,
kv: impl Iterator<Item = (impl AsRef<str>, impl AsRef<str>)>,
) -> pw::properties::Properties {
let mut props = pw::properties::Properties::new();
for (k, v) in kv {
props.insert(k, v);
props.insert(k.as_ref(), v.as_ref());
}
props
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ mod inspector {
ObjectType::Port => {
if let Some(parent) = global_borrow.parent_id() {
let name = global_borrow.name().cloned().unwrap_or_default();
match info[0].1.as_str() {
match info[0].1.as_ref() {
"Input" => {
self.graph.add_input_port(id, parent, name);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/context_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Tool for ContextManager {
}

impl ContextManager {
pub fn set_context_properties(&mut self, properties: BTreeMap<String, String>) {
pub fn set_context_properties(&mut self, properties: BTreeMap<Box<str>, String>) {
self.properties.set_map(properties);
}

Expand Down
41 changes: 29 additions & 12 deletions src/ui/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ pub struct Global {

subobjects: Vec<Weak<RefCell<Global>>>,

info: Option<Box<[(&'static str, String)]>>,
props: BTreeMap<String, String>,
info: Option<Box<[(&'static str, Box<str>)]>>,
props: BTreeMap<Box<str>, String>,

object_data: ObjectData,
}
Expand All @@ -202,7 +202,7 @@ impl Global {
pub fn new(
id: u32,
object_type: pw::types::ObjectType,
props: Option<BTreeMap<String, String>>,
props: Option<BTreeMap<Box<str>, String>>,
) -> Self {
let mut this = Self {
id,
Expand Down Expand Up @@ -256,9 +256,9 @@ impl Global {
for (k, v) in self
.props
.iter()
.filter(|(k, _)| k.ends_with(".name") && k.as_str() != "library.name")
.filter(|(k, _)| k.ends_with(".name") && k.as_ref() != "library.name")
{
if *self.object_type() != ObjectType::Factory && k == "factory.name" {
if *self.object_type() != ObjectType::Factory && k.as_ref() == "factory.name" {
continue;
}
name = Some(v);
Expand Down Expand Up @@ -325,7 +325,13 @@ impl Global {

ui.push_id(self.id, |ui| {
if let Some(info) = self.info() {
key_val_display(ui, 400f32, f32::INFINITY, "Info", info.iter().cloned());
key_val_display(
ui,
400f32,
f32::INFINITY,
"Info",
info.iter().map(|(k, v)| (*k, v.as_ref())),
);
}

// Clients can have their properties updated
Expand All @@ -340,7 +346,12 @@ impl Global {
ui.separator();

if ui.button("Update properties").clicked() {
self.props.extend(user_properties.take());
self.props.extend(
user_properties
.take()
.into_iter()
.map(|(k, v)| (k.into_boxed_str(), v)),
);

sx.send(Request::CallObjectMethod(
self.id,
Expand All @@ -350,7 +361,13 @@ impl Global {
}
});
} else {
key_val_display(ui, 400f32, f32::INFINITY, "Properties", self.props().iter());
key_val_display(
ui,
400f32,
f32::INFINITY,
"Properties",
self.props().iter().map(|(k, v)| (k.as_ref(), v.as_str())),
);
}

let subobjects_header = match self.object_type() {
Expand Down Expand Up @@ -456,20 +473,20 @@ impl Global {
self.subobjects.push(subobject);
}

pub const fn props(&self) -> &BTreeMap<String, String> {
pub const fn props(&self) -> &BTreeMap<Box<str>, String> {
&self.props
}

pub fn set_props(&mut self, props: BTreeMap<String, String>) {
pub fn set_props(&mut self, props: BTreeMap<Box<str>, String>) {
self.props = props;
self.update();
}

pub fn info(&self) -> Option<&[(&'static str, String)]> {
pub fn info(&self) -> Option<&[(&'static str, Box<str>)]> {
self.info.as_deref()
}

pub fn set_info(&mut self, info: Option<Box<[(&'static str, String)]>>) {
pub fn set_info(&mut self, info: Option<Box<[(&'static str, Box<str>)]>>) {
self.info = info;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ui/globals_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl GlobalsStore {
&mut self,
id: u32,
object_type: ObjectType,
props: Option<BTreeMap<String, String>>,
props: Option<BTreeMap<Box<str>, String>>,
) -> &Rc<RefCell<Global>> {
use std::collections::hash_map::Entry;

Expand Down Expand Up @@ -127,7 +127,7 @@ impl GlobalsStore {
self.globals.remove(&id)
}

pub fn set_global_props(&mut self, id: u32, props: BTreeMap<String, String>) {
pub fn set_global_props(&mut self, id: u32, props: BTreeMap<Box<str>, String>) {
use std::collections::btree_map::Entry;

if let Some(global) = self.globals.get(&id) {
Expand Down
8 changes: 4 additions & 4 deletions src/ui/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,11 @@ impl Graph {
{
sx.send(Request::CreateObject(
ObjectType::Link,
String::from("link-factory"),
"link-factory".into(),
vec![
("link.output.port".to_owned(), output),
("link.input.port".to_owned(), input),
("object.linger".to_owned(), "true".to_owned()),
("link.output.port".into(), output.into()),
("link.input.port".into(), input.into()),
("object.linger".into(), "true".into()),
],
))
.ok();
Expand Down
10 changes: 5 additions & 5 deletions src/ui/metadata_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Property {
}

struct Metadata {
properties: BTreeMap<String, Property>,
properties: BTreeMap<Box<str>, Property>,
user_properties: Vec<(String, Property)>,
global: Rc<RefCell<Global>>,
}
Expand Down Expand Up @@ -89,7 +89,7 @@ impl MetadataEditor {
&mut self,
global: &Rc<RefCell<Global>>,
subject: u32,
key: String,
key: Box<str>,
type_: Option<String>,
value: String,
) {
Expand Down Expand Up @@ -151,20 +151,20 @@ impl MetadataEditor {
.striped(true)
.show(ui, |ui| {
for (key, prop) in &mut metadata.properties {
ui.label(key);
ui.label(key.as_ref());

ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| {
if ui.small_button("Clear").clicked() {
sx.send(Request::CallObjectMethod(
*id,
prop.clear_request(key.clone()),
prop.clear_request(key.clone().into_string()),
))
.ok();
}
if ui.small_button("Set").clicked() {
sx.send(Request::CallObjectMethod(
*id,
prop.set_request(key.clone()),
prop.set_request(key.clone().into_string()),
))
.ok();
}
Expand Down
Loading

0 comments on commit 8734882

Please sign in to comment.