aboutsummaryrefslogtreecommitdiff
path: root/src/model/user.rs
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/user.rs
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/user.rs')
-rw-r--r--src/model/user.rs12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/model/user.rs b/src/model/user.rs
index bf5ee36..d01eb88 100644
--- a/src/model/user.rs
+++ b/src/model/user.rs
@@ -21,6 +21,8 @@ use builder::{CreateMessage, EditProfile};
use CACHE;
#[cfg(feature = "model")]
use http::{self, GuildPagination};
+#[cfg(feature = "model")]
+use utils;
/// Information about the current user.
#[derive(Clone, Default, Debug, Deserialize)]
@@ -86,14 +88,16 @@ impl CurrentUser {
/// ```
pub fn edit<F>(&mut self, f: F) -> Result<()>
where F: FnOnce(EditProfile) -> EditProfile {
- let mut map = Map::new();
- map.insert("username".to_string(), Value::String(self.name.clone()));
+ let mut map = HashMap::new();
+ map.insert("username", Value::String(self.name.clone()));
if let Some(email) = self.email.as_ref() {
- map.insert("email".to_string(), Value::String(email.clone()));
+ map.insert("email", Value::String(email.clone()));
}
- match http::edit_profile(&f(EditProfile(map)).0) {
+ let map = utils::hashmap_to_json_map(f(EditProfile(map)).0);
+
+ match http::edit_profile(&map) {
Ok(new) => {
let _ = mem::replace(self, new);