diff options
| author | Zeyla Hellyer <[email protected]> | 2017-10-18 08:34:06 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-10-18 08:34:06 -0700 |
| commit | 9908999a6bae1585bb70b7814f13b49bf99b6c32 (patch) | |
| tree | d789f716400502ae0d124933f5c4d927867e7033 /src/client | |
| parent | Fix some compilation feature targets, fix lints (diff) | |
| download | serenity-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/client')
| -rw-r--r-- | src/client/context.rs | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/client/context.rs b/src/client/context.rs index 18500d6..33972c1 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -12,7 +12,9 @@ use internal::prelude::*; #[cfg(feature = "builder")] use builder::EditProfile; #[cfg(feature = "builder")] -use http; +use {http, utils}; +#[cfg(feature = "builder")] +use std::collections::HashMap; /// The context is a general utility struct provided on event dispatches, which /// helps with dealing with the current "context" of the event dispatch. @@ -79,29 +81,29 @@ impl Context { /// ``` #[cfg(feature = "builder")] pub fn edit_profile<F: FnOnce(EditProfile) -> EditProfile>(&self, f: F) -> Result<CurrentUser> { - let mut map = Map::new(); + let mut map = HashMap::new(); feature_cache! { { let cache = CACHE.read(); - map.insert("username".to_string(), Value::String(cache.user.name.clone())); + map.insert("username", Value::String(cache.user.name.clone())); if let Some(email) = cache.user.email.as_ref() { - map.insert("email".to_string(), Value::String(email.clone())); + map.insert("email", Value::String(email.clone())); } } else { let user = http::get_current_user()?; - map.insert("username".to_string(), Value::String(user.name.clone())); + map.insert("username", Value::String(user.name.clone())); if let Some(email) = user.email.as_ref() { - map.insert("email".to_string(), Value::String(email.clone())); + map.insert("email", Value::String(email.clone())); } } } - let edited = f(EditProfile(map)).0; + let edited = utils::hashmap_to_json_map(f(EditProfile(map)).0); http::edit_profile(&edited) } |