aboutsummaryrefslogtreecommitdiff
path: root/src/model/channel
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-10-18 08:34:06 -0700
committerZeyla Hellyer <[email protected]>2017-10-18 08:34:06 -0700
commit9908999a6bae1585bb70b7814f13b49bf99b6c32 (patch)
treed789f716400502ae0d124933f5c4d927867e7033 /src/model/channel
parentFix some compilation feature targets, fix lints (diff)
downloadserenity-9908999a6bae1585bb70b7814f13b49bf99b6c32.tar.xz
serenity-9908999a6bae1585bb70b7814f13b49bf99b6c32.zip
Slightly improve performance of builders
Builders would keep a `serde_json::Map<String, Value>`, which would require re-creating owned strings for the same parameter multiple times in some cases, depending on builder defaults and keying strategies. This commit uses a `std::collections::HashMap<&'static str, Value>` internally, and moves over values to a `serde_json::Map<String, Value>` when it comes time to sending them to the appropriate `http` module function. This saves the number of heap-allocated string creations on most builders, with specific performance increase on `builder::CreateMessage` and `builder::CreateEmbed` & co.
Diffstat (limited to 'src/model/channel')
-rw-r--r--src/model/channel/channel_category.rs18
-rw-r--r--src/model/channel/channel_id.rs24
-rw-r--r--src/model/channel/embed.rs6
-rw-r--r--src/model/channel/guild_channel.rs22
-rw-r--r--src/model/channel/message.rs4
5 files changed, 39 insertions, 35 deletions
diff --git a/src/model/channel/channel_category.rs b/src/model/channel/channel_category.rs
index f567cee..73c50a7 100644
--- a/src/model/channel/channel_category.rs
+++ b/src/model/channel/channel_category.rs
@@ -93,20 +93,14 @@ impl ChannelCategory {
}
}
- let mut map = Map::new();
- map.insert("name".to_string(), Value::String(self.name.clone()));
- map.insert(
- "position".to_string(),
- Value::Number(Number::from(self.position)),
- );
- map.insert(
- "type".to_string(),
- Value::String(self.kind.name().to_string()),
- );
+ let mut map = HashMap::new();
+ map.insert("name", Value::String(self.name.clone()));
+ map.insert("position", Value::Number(Number::from(self.position)));
+ map.insert("type", Value::String(self.kind.name().to_string()));
- let edited = f(EditChannel(map)).0;
+ let map = serenity_utils::hashmap_to_json_map(f(EditChannel(map)).0);
- http::edit_channel(self.id.0, &edited).map(|channel| {
+ http::edit_channel(self.id.0, &map).map(|channel| {
let GuildChannel {
id,
category_id,
diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs
index 4034fb9..3bed660 100644
--- a/src/model/channel/channel_id.rs
+++ b/src/model/channel/channel_id.rs
@@ -11,6 +11,8 @@ use builder::{CreateMessage, EditChannel, GetMessages};
use CACHE;
#[cfg(feature = "model")]
use http::{self, AttachmentType};
+#[cfg(feature = "model")]
+use utils;
#[cfg(feature = "model")]
impl ChannelId {
@@ -185,7 +187,9 @@ impl ChannelId {
/// [Manage Channel]: permissions/constant.MANAGE_CHANNELS.html
#[inline]
pub fn edit<F: FnOnce(EditChannel) -> EditChannel>(&self, f: F) -> Result<GuildChannel> {
- http::edit_channel(self.0, &f(EditChannel::default()).0)
+ let map = utils::hashmap_to_json_map(f(EditChannel::default()).0);
+
+ http::edit_channel(self.0, &map)
}
/// Edits a [`Message`] in the channel given its Id.
@@ -209,9 +213,9 @@ impl ChannelId {
/// [`the limit`]: ../builder/struct.CreateMessage.html#method.content
pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message>
where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> {
- let map = f(CreateMessage::default()).0;
+ let msg = f(CreateMessage::default());
- if let Some(content) = map.get("content") {
+ if let Some(content) = msg.0.get("content") {
if let Value::String(ref content) = *content {
if let Some(length_over) = Message::overflow_length(content) {
return Err(Error::Model(ModelError::MessageTooLong(length_over)));
@@ -219,6 +223,8 @@ impl ChannelId {
}
}
+ let map = utils::hashmap_to_json_map(msg.0);
+
http::edit_message(self.0, message_id.into().0, &Value::Object(map))
}
@@ -441,9 +447,9 @@ impl ChannelId {
/// [Send Messages]: permissions/constant.SEND_MESSAGES.html
pub fn send_files<'a, F, T, It: IntoIterator<Item=T>>(&self, files: It, f: F) -> Result<Message>
where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> {
- let mut map = f(CreateMessage::default()).0;
+ let mut msg = f(CreateMessage::default());
- if let Some(content) = map.get("content") {
+ if let Some(content) = msg.0.get("content") {
if let Value::String(ref content) = *content {
if let Some(length_over) = Message::overflow_length(content) {
return Err(Error::Model(ModelError::MessageTooLong(length_over)));
@@ -451,7 +457,8 @@ impl ChannelId {
}
}
- let _ = map.remove("embed");
+ let _ = msg.0.remove("embed");
+ let map = utils::hashmap_to_json_map(msg.0);
http::send_files(self.0, files, map)
}
@@ -477,14 +484,15 @@ impl ChannelId {
/// [Send Messages]: permissions/constant.SEND_MESSAGES.html
pub fn send_message<F>(&self, f: F) -> Result<Message>
where F: FnOnce(CreateMessage) -> CreateMessage {
- let CreateMessage(map, reactions) = f(CreateMessage::default());
+ let msg = f(CreateMessage::default());
+ let map = utils::hashmap_to_json_map(msg.0);
Message::check_content_length(&map)?;
Message::check_embed_length(&map)?;
let message = http::send_message(self.0, &Value::Object(map))?;
- if let Some(reactions) = reactions {
+ if let Some(reactions) = msg.1 {
for reaction in reactions {
self.create_reaction(message.id, reaction)?;
}
diff --git a/src/model/channel/embed.rs b/src/model/channel/embed.rs
index 435f706..74c503d 100644
--- a/src/model/channel/embed.rs
+++ b/src/model/channel/embed.rs
@@ -4,6 +4,8 @@ use utils::Colour;
use internal::prelude::*;
#[cfg(feature = "model")]
use builder::CreateEmbed;
+#[cfg(feature = "model")]
+use utils;
/// Represents a rich embed which allows using richer markdown, multiple fields
/// and more. This was heavily inspired by [slack's attachments].
@@ -89,7 +91,9 @@ impl Embed {
#[inline]
pub fn fake<F>(f: F) -> Value
where F: FnOnce(CreateEmbed) -> CreateEmbed {
- Value::Object(f(CreateEmbed::default()).0)
+ let map = utils::hashmap_to_json_map(f(CreateEmbed::default()).0);
+
+ Value::Object(map)
}
}
diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs
index cc74b9d..90bb926 100644
--- a/src/model/channel/guild_channel.rs
+++ b/src/model/channel/guild_channel.rs
@@ -116,7 +116,9 @@ impl GuildChannel {
}
}
- http::create_invite(self.id.0, &f(CreateInvite::default()).0)
+ let map = serenity_utils::hashmap_to_json_map(f(CreateInvite::default()).0);
+
+ http::create_invite(self.id.0, &map)
}
/// Creates a [permission overwrite][`PermissionOverwrite`] for either a
@@ -312,18 +314,12 @@ impl GuildChannel {
}
}
- let mut map = Map::new();
- map.insert("name".to_string(), Value::String(self.name.clone()));
- map.insert(
- "position".to_string(),
- Value::Number(Number::from(self.position)),
- );
- map.insert(
- "type".to_string(),
- Value::String(self.kind.name().to_string()),
- );
-
- let edited = f(EditChannel(map)).0;
+ let mut map = HashMap::new();
+ map.insert("name", Value::String(self.name.clone()));
+ map.insert("position", Value::Number(Number::from(self.position)));
+ map.insert("type", Value::String(self.kind.name().to_string()));
+
+ let edited = serenity_utils::hashmap_to_json_map(f(EditChannel(map)).0);
match http::edit_channel(self.id.0, &edited) {
Ok(channel) => {
diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs
index 0e939b2..b5864fc 100644
--- a/src/model/channel/message.rs
+++ b/src/model/channel/message.rs
@@ -14,6 +14,8 @@ use constants;
use CACHE;
#[cfg(feature = "model")]
use http;
+#[cfg(feature = "model")]
+use utils as serenity_utils;
/// A representation of a message over a guild's text channel, a group, or a
/// private channel.
@@ -230,7 +232,7 @@ impl Message {
builder = builder.tts(true);
}
- let map = f(builder).0;
+ let map = serenity_utils::hashmap_to_json_map(f(builder).0);
match http::edit_message(self.channel_id.0, self.id.0, &Value::Object(map)) {
Ok(edited) => {