Convert Block::instance from a Box to an Arc

This will reduce an allocation and deallocation everytime a message is
sent
This commit is contained in:
Skye Jensen 2020-06-02 21:57:02 -04:00
parent 11f31d24ad
commit c0de250953
7 changed files with 32 additions and 12 deletions

12
Cargo.lock generated
View file

@ -501,6 +501,7 @@ dependencies = [
"dbus-tokio", "dbus-tokio",
"serde", "serde",
"serde_json", "serde_json",
"smart-default",
"tokio", "tokio",
"uuid", "uuid",
] ]
@ -558,6 +559,17 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
[[package]]
name = "smart-default"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "133659a15339456eeeb07572eb02a91c91e9815e9cbc89566944d2c8d3efdbf6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.3.12" version = "0.3.12"

View file

@ -11,5 +11,6 @@ dbus = "0.8"
dbus-tokio = "0.5" dbus-tokio = "0.5"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
smart-default = "0.6"
tokio = { version = "0.2", features = ["full"] } tokio = { version = "0.2", features = ["full"] }
uuid = { version = "0.8", features = [ "v4" ] } uuid = { version = "0.8", features = [ "v4" ] }

View file

@ -25,17 +25,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Arc::new(tiles::Load::new( Arc::new(tiles::Load::new(
0, 0,
sender.clone(), sender.clone(),
Uuid::new_v4().to_string().into_boxed_str(), Uuid::new_v4().to_string().into(),
)), )),
Arc::new(tiles::Hostname::new( Arc::new(tiles::Hostname::new(
1, 1,
sender.clone(), sender.clone(),
Uuid::new_v4().to_string().into_boxed_str(), Uuid::new_v4().to_string().into(),
)), )),
Arc::new(tiles::Time::new( Arc::new(tiles::Time::new(
2, 2,
sender, sender,
Uuid::new_v4().to_string().into_boxed_str(), Uuid::new_v4().to_string().into(),
)), )),
]; ];

View file

@ -1,6 +1,7 @@
use serde::Serialize; use serde::{Serialize, ser::Serializer};
use std::sync::Arc; use std::sync::Arc;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use smart_default::SmartDefault;
#[derive(Copy, Clone, Debug, Serialize)] #[derive(Copy, Clone, Debug, Serialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
@ -29,7 +30,11 @@ impl Default for Markup {
} }
} }
#[derive(Clone, Serialize, Default, Debug)] fn arc_default<S>(arc: &Arc<str>, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
serializer.serialize_str(arc)
}
#[derive(Clone, Serialize, Debug, SmartDefault)]
pub struct Block { pub struct Block {
pub full_text: Box<str>, pub full_text: Box<str>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
@ -53,7 +58,9 @@ pub struct Block {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub align: Option<Alignment>, pub align: Option<Alignment>,
pub name: Box<str>, pub name: Box<str>,
pub instance: Box<str>, #[serde(serialize_with = "arc_default")]
#[default = r#""".into()"#]
pub instance: Arc<str>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub urgent: Option<bool>, pub urgent: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]

View file

@ -9,11 +9,11 @@ use tokio::task::JoinHandle;
pub struct Hostname { pub struct Hostname {
sender_id: usize, sender_id: usize,
sender: RwLock<Sender<TileData>>, sender: RwLock<Sender<TileData>>,
instance: Box<str>, instance: Arc<str>,
} }
impl Hostname { impl Hostname {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Box<str>) -> Hostname { pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Hostname {
Hostname { Hostname {
sender_id, sender_id,
sender: RwLock::new(sender), sender: RwLock::new(sender),

View file

@ -11,11 +11,11 @@ use tokio::time::interval;
pub struct Load { pub struct Load {
sender_id: usize, sender_id: usize,
sender: RwLock<Sender<TileData>>, sender: RwLock<Sender<TileData>>,
instance: Box<str>, instance: Arc<str>,
} }
impl Load { impl Load {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Box<str>) -> Load { pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Load {
Load { Load {
sender_id, sender_id,
sender: RwLock::new(sender), sender: RwLock::new(sender),

View file

@ -11,13 +11,13 @@ use tokio::time::delay_for;
pub struct Time { pub struct Time {
sender_id: usize, sender_id: usize,
sender: RwLock<Sender<TileData>>, sender: RwLock<Sender<TileData>>,
instance: Box<str>, instance: Arc<str>,
format: Box<str>, format: Box<str>,
short_format: Box<str>, short_format: Box<str>,
} }
impl Time { impl Time {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Box<str>) -> Time { pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Time {
Time { Time {
sender_id, sender_id,
sender: RwLock::new(sender), sender: RwLock::new(sender),