Replace std HashMap with hashbrown::HashMap

The hashbrown::HashMap is faster than the std::collections::HashMap,
though it does not protect as well against malicious hash collisions
(e.g., in a DDoS).  Since we don't expose the hashing externally,
we should switch to the faster implementation.
This commit is contained in:
Daniel Sockwell 2020-04-07 16:08:43 -04:00
parent f87cde6600
commit 17afe4697d
10 changed files with 45 additions and 13 deletions

26
Cargo.lock generated
View File

@ -8,6 +8,14 @@ dependencies = [
"const-random 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ahash"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"const-random 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aho-corasick"
version = "0.7.6"
@ -44,6 +52,11 @@ name = "autocfg"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "backtrace"
version = "0.3.15"
@ -445,6 +458,7 @@ dependencies = [
"criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dotenv 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)",
"hashbrown 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"lru 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"postgres 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -650,6 +664,15 @@ dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hashbrown"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ahash 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "headers"
version = "0.2.1"
@ -2378,11 +2401,13 @@ dependencies = [
[metadata]
"checksum ahash 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6f33b5018f120946c1dcf279194f238a9f146725593ead1c08fa47ff22b0b5d3"
"checksum ahash 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0989268a37e128d4d7a8028f1c60099430113fdbc70419010601ce51a228e4fe"
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
"checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5"
"checksum arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71"
"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
"checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
"checksum backtrace 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "f106c02a3604afcdc0df5d36cc47b44b55917dbaf3d808f71c163a0ddba64637"
"checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6"
"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
@ -2450,6 +2475,7 @@ dependencies = [
"checksum getrandom 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "473a1265acc8ff1e808cd0a1af8cee3c2ee5200916058a2ca113c29f2d903571"
"checksum h2 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "85ab6286db06040ddefb71641b50017c06874614001a134b423783e2db2920bd"
"checksum hashbrown 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8e6073d0ca812575946eb5f35ff68dbe519907b25c42530389ff946dc84c6ead"
"checksum hashbrown 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "479e9d9a1a3f8c489868a935b557ab5710e3e223836da2ecd52901d88935cb56"
"checksum headers 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6e2e51d356081258ef05ff4c648138b5d3fe64b7300aaad3b820554a2b7fb6"
"checksum headers-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51ae5b0b5417559ee1d2733b21d33b0868ae9e406bd32eb1a51d613f66ed472a"
"checksum headers-derive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97c462e8066bca4f0968ddf8d12de64c40f2c2187b3b9a2fa994d06e8ad444a9"

View File

@ -25,6 +25,7 @@ r2d2_postgres = "0.16.0"
r2d2 = "0.8.8"
lru = "0.4.3"
urlencoding = "1.0.0"
hashbrown = "0.7.1"
[dev-dependencies]
criterion = "0.3"
@ -44,4 +45,6 @@ production = []
[profile.release]
lto = "fat"
panic = "abort"
codegen-units = 1

View File

@ -1,4 +1,5 @@
use std::{collections::HashMap, fmt};
use hashbrown::HashMap;
use std::fmt;
pub struct EnvVar(pub HashMap<String, String>);
impl std::ops::Deref for EnvVar {

View File

@ -9,9 +9,10 @@ use {application::Application, attachment::Attachment, card::Card, poll::Poll};
use crate::log_fatal;
use crate::parse_client_request::Blocks;
use hashbrown::HashSet;
use serde::{Deserialize, Serialize};
use std::boxed::Box;
use std::{collections::HashSet, string::String};
use std::string::String;
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]

View File

@ -1,7 +1,7 @@
use crate::parse_client_request::Blocks;
use hashbrown::HashSet;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashSet;
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct DynamicEvent {

View File

@ -1,5 +1,5 @@
use super::*;
use std::collections::HashMap;
use hashbrown::HashMap;
#[serde(rename_all = "snake_case")]
#[derive(Deserialize, Debug, Clone, PartialEq)]

View File

@ -4,8 +4,8 @@ use crate::{
parse_client_request::subscription::{Scope, UserData},
};
use ::postgres;
use hashbrown::HashSet;
use r2d2_postgres::PostgresConnectionManager;
use std::collections::HashSet;
use warp::reject::Rejection;
#[derive(Clone, Debug)]

View File

@ -9,8 +9,8 @@ use super::postgres::PgPool;
use super::query::Query;
use crate::err::TimelineErr;
use crate::log_fatal;
use hashbrown::HashSet;
use lru::LruCache;
use std::collections::HashSet;
use uuid::Uuid;
use warp::reject::Rejection;

View File

@ -1,10 +1,8 @@
use crate::messages::Event;
use crate::parse_client_request::Timeline;
use std::{
collections::{HashMap, VecDeque},
fmt,
};
use hashbrown::HashMap;
use std::{collections::VecDeque, fmt};
use uuid::Uuid;
#[derive(Clone)]

View File

@ -15,14 +15,17 @@ use crate::{
parse_client_request::{Stream, Subscription, Timeline},
};
use futures::{Async, Poll};
use {
futures::{Async, Poll},
hashbrown::HashMap,
uuid::Uuid,
};
use std::{
collections::HashMap,
result,
sync::{Arc, Mutex},
time::{Duration, Instant},
};
use uuid::Uuid;
type Result<T> = result::Result<T, ReceiverErr>;