Skip to content

Commit

Permalink
Merge pull request dbus2#1037 from zeenix/fdo-avoid-allocation
Browse files Browse the repository at this point in the history
⚡️ zb,zm: Make use of Cow to avoid forcing allocations in fdo
  • Loading branch information
zeenix authored Oct 3, 2024
2 parents 2470aed + 10ae1ae commit 2c4f8e3
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions zbus/src/fdo/object_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! be useful across various D-Bus applications. This module provides their proxy.

use static_assertions::assert_impl_all;
use std::collections::HashMap;
use std::{borrow::Cow, collections::HashMap};
use zbus_names::{InterfaceName, OwnedInterfaceName};
use zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Value};

Expand Down Expand Up @@ -72,7 +72,7 @@ impl ObjectManager {
pub async fn interfaces_removed(
emitter: &SignalEmitter<'_>,
object_path: ObjectPath<'_>,
interfaces: Vec<InterfaceName<'_>>,
interfaces: Cow<'_, [InterfaceName<'_>]>,
) -> zbus::Result<()>;
}

Expand Down
4 changes: 2 additions & 2 deletions zbus/src/fdo/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! be useful across various D-Bus applications. This module provides their proxy.

use static_assertions::assert_impl_all;
use std::collections::HashMap;
use std::{borrow::Cow, collections::HashMap};
use zbus_names::InterfaceName;
use zvariant::{OwnedValue, Value};

Expand Down Expand Up @@ -120,7 +120,7 @@ impl Properties {
emitter: &SignalEmitter<'_>,
interface_name: InterfaceName<'_>,
changed_properties: HashMap<&str, Value<'_>>,
invalidated_properties: Vec<&str>,
invalidated_properties: Cow<'_, [&str]>,
) -> zbus::Result<()>;
}

Expand Down
2 changes: 1 addition & 1 deletion zbus/src/object_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl ObjectServer {
}
if let Some(manager_path) = manager_path {
let ctxt = SignalEmitter::new(&self.connection(), manager_path.clone())?;
ObjectManager::interfaces_removed(&ctxt, path.clone(), vec![I::name()]).await?;
ObjectManager::interfaces_removed(&ctxt, path.clone(), (&[I::name()]).into()).await?;
}
if node.is_empty() {
let mut path_parts = path.rsplit('/').filter(|i| !i.is_empty());
Expand Down
12 changes: 6 additions & 6 deletions zbus/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl PropertiesCache {
}
Some(Either::Right(populate)) => {
populate?.body().deserialize().map(|values| {
self.update_cache(&uncached_properties, &values, Vec::new(), &interface);
self.update_cache(&uncached_properties, &values, &[], &interface);
})?;
break;
}
Expand All @@ -379,7 +379,7 @@ impl PropertiesCache {
self.update_cache(
&uncached_properties,
&args.changed_properties,
args.invalidated_properties,
&args.invalidated_properties,
&interface,
);
}
Expand Down Expand Up @@ -410,7 +410,7 @@ impl PropertiesCache {
self.update_cache(
&uncached_properties,
&args.changed_properties,
args.invalidated_properties,
&args.invalidated_properties,
&interface,
);
}
Expand All @@ -424,13 +424,13 @@ impl PropertiesCache {
&self,
uncached_properties: &HashSet<Str<'_>>,
changed: &HashMap<&str, Value<'_>>,
invalidated: Vec<&str>,
invalidated: &[&str],
interface: &InterfaceName<'_>,
) {
let mut values = self.values.write().expect("lock poisoned");

for inval in invalidated {
if uncached_properties.contains(&Str::from(inval)) {
if uncached_properties.contains(&Str::from(*inval)) {
debug!(
"Ignoring invalidation of uncached property `{}.{}`",
interface, inval
Expand All @@ -439,7 +439,7 @@ impl PropertiesCache {
}
trace!("Property `{interface}.{inval}` invalidated");

if let Some(entry) = values.get_mut(inval) {
if let Some(entry) = values.get_mut(*inval) {
entry.value = None;
entry.event.notify(usize::MAX);
}
Expand Down
2 changes: 1 addition & 1 deletion zbus/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ async fn my_iface_test(conn: Connection, event: Event) -> zbus::Result<u32> {

let args = ifaces_removed.args()?;
assert_eq!(args.object_path(), "/zbus/test/MyObj");
assert_eq!(args.interfaces(), &["org.freedesktop.MyIface"]);
assert_eq!(args.interfaces().as_ref(), &["org.freedesktop.MyIface"]);

assert!(my_obj_proxy.inner().introspect().await.is_err());
assert!(my_obj_proxy.ping().await.is_err());
Expand Down
4 changes: 2 additions & 2 deletions zbus_macros/src/iface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ pub fn expand(args: Punctuated<Meta, Token![,]>, mut input: ItemImpl) -> syn::Re
signal_emitter,
#zbus::names::InterfaceName::from_static_str_unchecked(#iface_name),
changed,
vec![],
::std::borrow::Cow::Borrowed(&[]),
).await
}
);
Expand All @@ -717,7 +717,7 @@ pub fn expand(args: Punctuated<Meta, Token![,]>, mut input: ItemImpl) -> syn::Re
signal_emitter,
#zbus::names::InterfaceName::from_static_str_unchecked(#iface_name),
::std::collections::HashMap::new(),
vec![#member_name],
::std::borrow::Cow::Borrowed(&[#member_name]),
).await
}
);
Expand Down

0 comments on commit 2c4f8e3

Please sign in to comment.