From c0de250953e6d2f57ce787829f514ed9a1a9f6d1 Mon Sep 17 00:00:00 2001 From: Skye Jensen Date: Tue, 2 Jun 2020 21:57:02 -0400 Subject: [PATCH] Convert `Block::instance` from a `Box` to an `Arc` This will reduce an allocation and deallocation everytime a message is sent --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/main.rs | 6 +++--- src/tile.rs | 13 ++++++++++--- src/tiles/hostname.rs | 4 ++-- src/tiles/load.rs | 4 ++-- src/tiles/time.rs | 4 ++-- 7 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5a6b0b2..f1ddc35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -501,6 +501,7 @@ dependencies = [ "dbus-tokio", "serde", "serde_json", + "smart-default", "tokio", "uuid", ] @@ -558,6 +559,17 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "socket2" version = "0.3.12" diff --git a/Cargo.toml b/Cargo.toml index 21eccf0..01c5078 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,6 @@ dbus = "0.8" dbus-tokio = "0.5" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +smart-default = "0.6" tokio = { version = "0.2", features = ["full"] } uuid = { version = "0.8", features = [ "v4" ] } diff --git a/src/main.rs b/src/main.rs index f565354..be04065 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,17 +25,17 @@ async fn main() -> Result<(), Box> { Arc::new(tiles::Load::new( 0, sender.clone(), - Uuid::new_v4().to_string().into_boxed_str(), + Uuid::new_v4().to_string().into(), )), Arc::new(tiles::Hostname::new( 1, sender.clone(), - Uuid::new_v4().to_string().into_boxed_str(), + Uuid::new_v4().to_string().into(), )), Arc::new(tiles::Time::new( 2, sender, - Uuid::new_v4().to_string().into_boxed_str(), + Uuid::new_v4().to_string().into(), )), ]; diff --git a/src/tile.rs b/src/tile.rs index e604365..1bd0159 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,6 +1,7 @@ -use serde::Serialize; +use serde::{Serialize, ser::Serializer}; use std::sync::Arc; use tokio::task::JoinHandle; +use smart_default::SmartDefault; #[derive(Copy, Clone, Debug, Serialize)] #[serde(rename_all = "lowercase")] @@ -29,7 +30,11 @@ impl Default for Markup { } } -#[derive(Clone, Serialize, Default, Debug)] +fn arc_default(arc: &Arc, serializer: S) -> Result where S: Serializer { + serializer.serialize_str(arc) +} + +#[derive(Clone, Serialize, Debug, SmartDefault)] pub struct Block { pub full_text: Box, #[serde(skip_serializing_if = "Option::is_none")] @@ -53,7 +58,9 @@ pub struct Block { #[serde(skip_serializing_if = "Option::is_none")] pub align: Option, pub name: Box, - pub instance: Box, + #[serde(serialize_with = "arc_default")] + #[default = r#""".into()"#] + pub instance: Arc, #[serde(skip_serializing_if = "Option::is_none")] pub urgent: Option, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/src/tiles/hostname.rs b/src/tiles/hostname.rs index d4e84ef..1476c47 100644 --- a/src/tiles/hostname.rs +++ b/src/tiles/hostname.rs @@ -9,11 +9,11 @@ use tokio::task::JoinHandle; pub struct Hostname { sender_id: usize, sender: RwLock>, - instance: Box, + instance: Arc, } impl Hostname { - pub fn new(sender_id: usize, sender: Sender, instance: Box) -> Hostname { + pub fn new(sender_id: usize, sender: Sender, instance: Arc) -> Hostname { Hostname { sender_id, sender: RwLock::new(sender), diff --git a/src/tiles/load.rs b/src/tiles/load.rs index 5dca26b..ee183f7 100644 --- a/src/tiles/load.rs +++ b/src/tiles/load.rs @@ -11,11 +11,11 @@ use tokio::time::interval; pub struct Load { sender_id: usize, sender: RwLock>, - instance: Box, + instance: Arc, } impl Load { - pub fn new(sender_id: usize, sender: Sender, instance: Box) -> Load { + pub fn new(sender_id: usize, sender: Sender, instance: Arc) -> Load { Load { sender_id, sender: RwLock::new(sender), diff --git a/src/tiles/time.rs b/src/tiles/time.rs index 556a20d..768b9c1 100644 --- a/src/tiles/time.rs +++ b/src/tiles/time.rs @@ -11,13 +11,13 @@ use tokio::time::delay_for; pub struct Time { sender_id: usize, sender: RwLock>, - instance: Box, + instance: Arc, format: Box, short_format: Box, } impl Time { - pub fn new(sender_id: usize, sender: Sender, instance: Box) -> Time { + pub fn new(sender_id: usize, sender: Sender, instance: Arc) -> Time { Time { sender_id, sender: RwLock::new(sender),