tracks lexicons and how many times they appeared on the jetstream

feat(server): use ahash brrr

ptr.pet 78fdb32d 9da170a1

verified
Changed files
+45 -9
server
+35
server/Cargo.lock
···
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
+
name = "ahash"
+
version = "0.8.12"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
+
dependencies = [
+
"cfg-if",
+
"getrandom 0.3.3",
+
"once_cell",
+
"serde",
+
"version_check",
+
"zerocopy",
+
]
+
+
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
···
name = "server"
version = "0.1.0"
dependencies = [
+
"ahash",
"anyhow",
"arc-swap",
"async-trait",
···
version = "0.8.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdd20c5420375476fbd4394763288da7eb0cc0b8c11deed431a91562af7335d3"
+
+
[[package]]
+
name = "zerocopy"
+
version = "0.8.26"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
+
dependencies = [
+
"zerocopy-derive",
+
]
+
+
[[package]]
+
name = "zerocopy-derive"
+
version = "0.8.26"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
+
dependencies = [
+
"proc-macro2",
+
"quote",
+
"syn",
+
]
[[package]]
name = "zeroize"
+1
server/Cargo.toml
···
rclite = "0.2.7"
snmalloc-rs = "0.3.8"
arc-swap = "1.7.1"
+
ahash = { version = "0.8.12", features = ["serde"] }
+4 -4
server/src/api.rs
···
use std::{
-
collections::HashMap,
fmt::Display,
net::SocketAddr,
ops::{Bound, Deref, RangeBounds},
time::Duration,
};
+
use ahash::AHashMap;
use anyhow::anyhow;
use axum::{
Json, Router,
···
#[derive(Serialize)]
struct Events {
per_second: usize,
-
events: HashMap<SmolStr, NsidCount>,
+
events: AHashMap<SmolStr, NsidCount>,
}
async fn events(db: State<Arc<Db>>) -> AppResult<Json<Events>> {
-
let mut events = HashMap::new();
+
let mut events = AHashMap::new();
for result in db.get_counts() {
let (nsid, counts) = result?;
events.insert(
···
(async move {
let mut listener = db.new_listener();
let mut data = Events {
-
events: HashMap::<SmolStr, NsidCount>::with_capacity(10),
+
events: AHashMap::<SmolStr, NsidCount>::with_capacity(10),
per_second: 0,
};
let mut updates = 0;
+5 -5
server/src/db/mod.rs
···
use std::{
-
collections::{HashMap, HashSet},
fmt::Debug,
io::Cursor,
ops::{Bound, Deref, RangeBounds},
···
u64,
};
+
use ahash::{AHashMap, AHashSet};
use byteview::StrView;
use fjall::{Keyspace, Partition, PartitionCreateOptions};
use itertools::{Either, Itertools};
···
}
pub struct DbInfo {
-
pub nsids: HashMap<SmolStr, Vec<usize>>,
+
pub nsids: AHashMap<SmolStr, Vec<usize>>,
pub disk_size: u64,
}
···
pub cfg: DbConfig,
pub ks: Keyspace,
counts: Partition,
-
hits: scc::HashIndex<SmolStr, Arc<LexiconHandle>>,
+
hits: scc::HashIndex<SmolStr, Arc<LexiconHandle>, ahash::RandomState>,
sync_pool: threadpool::ThreadPool,
event_broadcaster: broadcast::Sender<(SmolStr, NsidCounts)>,
eps: RateTracker<100>, // 100 millis buckets
···
// prepare all the data
let nsids_len = self.hits.len();
let mut data = Vec::with_capacity(nsids_len);
-
let mut nsids = HashSet::with_capacity(nsids_len);
+
let mut nsids = AHashSet::with_capacity(nsids_len);
let _guard = scc::ebr::Guard::new();
for (nsid, handle) in self.hits.iter(&_guard) {
let mut nsid_data = Vec::with_capacity(2);
···
}
pub fn info(&self) -> AppResult<DbInfo> {
-
let mut nsids = HashMap::new();
+
let mut nsids = AHashMap::new();
for nsid in self.get_nsids() {
let Some(handle) = self.get_handle(&nsid) else {
continue;