提交 7becc68e authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Optimize worker communication performance by avoiding `Value` in payload channel…

Optimize worker communication performance by avoiding `Value` in payload channel notifications and extra memory allocations
上级 dbd933c3
......@@ -557,7 +557,7 @@ impl Consumer {
let handlers = Arc::clone(&handlers);
payload_channel.subscribe_to_notifications(id.into(), move |message, payload| {
match serde_json::from_value::<PayloadNotification>(message) {
match serde_json::from_slice::<PayloadNotification>(message) {
Ok(notification) => match notification {
PayloadNotification::Rtp => {
handlers.rtp.call(|callback| {
......
......@@ -401,7 +401,7 @@ impl DataConsumer {
let handlers = Arc::clone(&handlers);
payload_channel.subscribe_to_notifications(id.into(), move |message, payload| {
match serde_json::from_value::<PayloadNotification>(message) {
match serde_json::from_slice::<PayloadNotification>(message) {
Ok(notification) => match notification {
PayloadNotification::Message { ppid } => {
match WebRtcMessage::new(ppid, Cow::from(payload)) {
......
......@@ -481,7 +481,7 @@ impl DirectTransport {
let handlers = Arc::clone(&handlers);
payload_channel.subscribe_to_notifications(id.into(), move |message, payload| {
match serde_json::from_value::<PayloadNotification>(message) {
match serde_json::from_slice::<PayloadNotification>(message) {
Ok(notification) => match notification {
PayloadNotification::Rtcp => {
handlers.rtcp.call(|callback| {
......
use parking_lot::Mutex;
use serde::Deserialize;
use std::collections::HashMap;
use std::sync::{Arc, Weak};
use uuid::Uuid;
......@@ -104,11 +105,11 @@ impl<V: Clone> EventHandlers<Arc<dyn Fn(V) + Send + Sync + 'static>> {
}
}
impl<V1: Clone, V2: ?Sized> EventHandlers<Arc<dyn Fn(V1, &V2) + Send + Sync + 'static>> {
impl<V1: ?Sized, V2: ?Sized> EventHandlers<Arc<dyn Fn(&V1, &V2) + Send + Sync + 'static>> {
pub(super) fn call_callbacks_with_two_values(
&self,
target_id: &SubscriptionTarget,
value1: V1,
value1: &V1,
value2: &V2,
) {
let handlers = self.handlers.lock();
......@@ -124,7 +125,7 @@ impl<V1: Clone, V2: ?Sized> EventHandlers<Arc<dyn Fn(V1, &V2) + Send + Sync + 's
// Drop mutex guard before running callbacks to avoid deadlocks
drop(handlers);
for callback in callbacks {
callback(value1.clone(), value2);
callback(value1, value2);
}
}
}
......@@ -144,7 +145,8 @@ impl<F> WeakEventHandlers<F> {
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Deserialize)]
#[serde(untagged)]
pub(crate) enum SubscriptionTarget {
Uuid(Uuid),
Number(u32),
......
......@@ -35,7 +35,8 @@ enum PayloadChannelReceiveMessage {
error: Value,
reason: String,
},
Notification(Value),
#[serde(rename_all = "camelCase")]
Notification { target_id: SubscriptionTarget },
/// Unknown data
#[serde(skip)]
Internal(InternalMessage),
......@@ -84,7 +85,7 @@ struct Inner {
internal_message_receiver: async_channel::Receiver<InternalMessage>,
requests_container_weak: Weak<Mutex<RequestsContainer>>,
#[allow(clippy::type_complexity)]
event_handlers_weak: WeakEventHandlers<Arc<dyn Fn(Value, &[u8]) + Send + Sync + 'static>>,
event_handlers_weak: WeakEventHandlers<Arc<dyn Fn(&[u8], &[u8]) + Send + Sync + 'static>>,
}
impl Drop for Inner {
......@@ -163,28 +164,10 @@ impl PayloadChannel {
);
}
}
PayloadChannelReceiveMessage::Notification(notification) => {
let target_id = notification.get("targetId").and_then(|value| {
let str = value.as_str()?;
str.parse()
.ok()
.map(SubscriptionTarget::Uuid)
.or_else(|| str.parse().ok().map(SubscriptionTarget::Number))
});
PayloadChannelReceiveMessage::Notification { target_id } => {
trace!("received notification payload of {} bytes", payload.len());
if let Some(target_id) = target_id {
event_handlers.call_callbacks_with_two_values(
&target_id,
notification,
payload,
);
} else {
let unexpected_message =
InternalMessage::UnexpectedData(Vec::from(message));
let _ = internal_message_sender.try_send(unexpected_message);
}
event_handlers.call_callbacks_with_two_values(&target_id, message, payload);
}
PayloadChannelReceiveMessage::Internal(internal_message) => {
let _ = internal_message_sender.try_send(internal_message);
......@@ -378,7 +361,7 @@ impl PayloadChannel {
callback: F,
) -> Option<SubscriptionHandler>
where
F: Fn(Value, &[u8]) + Send + Sync + 'static,
F: Fn(&[u8], &[u8]) + Send + Sync + 'static,
{
self.inner
.event_handlers_weak
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论