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/model/guild/guild_id.rs | |
| 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/model/guild/guild_id.rs')
| -rw-r--r-- | src/model/guild/guild_id.rs | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index efda6a8..2771fc8 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -5,11 +5,11 @@ use CACHE; #[cfg(feature = "model")] use builder::{EditGuild, EditMember, EditRole}; #[cfg(feature = "model")] -use http; -#[cfg(feature = "model")] use internal::prelude::*; #[cfg(feature = "model")] use model::guild::BanOptions; +#[cfg(feature = "model")] +use {http, utils}; #[cfg(feature = "model")] impl GuildId { @@ -168,7 +168,9 @@ impl GuildId { /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[inline] pub fn create_role<F: FnOnce(EditRole) -> EditRole>(&self, f: F) -> Result<Role> { - http::create_role(self.0, &f(EditRole::default()).0) + let map = utils::hashmap_to_json_map(f(EditRole::default()).0); + + http::create_role(self.0, &map) } /// Deletes the current guild if the current account is the owner of the @@ -229,7 +231,9 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] pub fn edit<F: FnOnce(EditGuild) -> EditGuild>(&mut self, f: F) -> Result<PartialGuild> { - http::edit_guild(self.0, &f(EditGuild::default()).0) + let map = utils::hashmap_to_json_map(f(EditGuild::default()).0); + + http::edit_guild(self.0, &map) } /// Edits an [`Emoji`]'s name in the guild. @@ -266,7 +270,9 @@ impl GuildId { #[inline] pub fn edit_member<F, U>(&self, user_id: U, f: F) -> Result<()> where F: FnOnce(EditMember) -> EditMember, U: Into<UserId> { - http::edit_member(self.0, user_id.into().0, &f(EditMember::default()).0) + let map = utils::hashmap_to_json_map(f(EditMember::default()).0); + + http::edit_member(self.0, user_id.into().0, &map) } /// Edits the current user's nickname for the guild. @@ -300,7 +306,9 @@ impl GuildId { #[inline] pub fn edit_role<F, R>(&self, role_id: R, f: F) -> Result<Role> where F: FnOnce(EditRole) -> EditRole, R: Into<RoleId> { - http::edit_role(self.0, role_id.into().0, &f(EditRole::default()).0) + let map = utils::hashmap_to_json_map(f(EditRole::default()).0); + + http::edit_role(self.0, role_id.into().0, &map) } /// Search the cache for the guild. |