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