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/utils | |
| 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/utils')
| -rw-r--r-- | src/utils/mod.rs | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs index c6d27c3..5783c41 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -13,18 +13,32 @@ pub use self::message_builder::{Content, ContentModifier, MessageBuilder}; pub use super::builder; use base64; +use internal::prelude::*; +use model::{EmojiId, EmojiIdentifier}; +use std::collections::HashMap; use std::ffi::OsStr; use std::fs::File; +use std::hash::Hash; use std::io::Read; use std::path::Path; -use internal::prelude::*; -use model::{EmojiId, EmojiIdentifier}; #[cfg(feature = "cache")] use cache::Cache; #[cfg(feature = "cache")] use CACHE; +/// Converts a HashMap into a final `serde_json::Map` representation. +pub fn hashmap_to_json_map<T>(map: HashMap<T, Value>) -> Map<String, Value> + where T: Eq + Hash + ToString { + let mut json_map = Map::new(); + + for (key, value) in map.into_iter() { + json_map.insert(key.to_string(), value); + } + + json_map +} + /// Determines if a name is NSFW. /// /// This checks that the name is either `"nsfw"` or, for names longer than that, |