From 9bb4c3cd0160d08058d3be780ca70d99e1f4c108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Wed, 4 Aug 2021 12:54:26 +0200 Subject: [PATCH] improvement: batched inserts for tokenids --- src/database.rs | 4 ++-- src/database/rooms.rs | 20 +++++++++++--------- src/server_server.rs | 13 +++++++++++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/database.rs b/src/database.rs index 54abc53..4f3d332 100644 --- a/src/database.rs +++ b/src/database.rs @@ -271,8 +271,8 @@ impl Database { eventid_outlierpdu: builder.open_tree("eventid_outlierpdu")?, referencedevents: builder.open_tree("referencedevents")?, - pdu_cache: Mutex::new(LruCache::new(100_000)), - auth_chain_cache: Mutex::new(LruCache::new(100_000)), + pdu_cache: Mutex::new(LruCache::new(1_000_000)), + auth_chain_cache: Mutex::new(LruCache::new(1_000_000)), }, account_data: account_data::AccountData { roomuserdataid_accountdata: builder.open_tree("roomuserdataid_accountdata")?, diff --git a/src/database/rooms.rs b/src/database/rooms.rs index 48a135a..9f7a600 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -905,18 +905,20 @@ impl Rooms { } EventType::RoomMessage => { if let Some(body) = pdu.content.get("body").and_then(|b| b.as_str()) { - for word in body + let mut batch = body .split_terminator(|c: char| !c.is_alphanumeric()) .filter(|word| word.len() <= 50) .map(str::to_lowercase) - { - let mut key = pdu.room_id.as_bytes().to_vec(); - key.push(0xff); - key.extend_from_slice(word.as_bytes()); - key.push(0xff); - key.extend_from_slice(&pdu_id); - self.tokenids.insert(&key, &[])?; - } + .map(|word| { + let mut key = pdu.room_id.as_bytes().to_vec(); + key.push(0xff); + key.extend_from_slice(word.as_bytes()); + key.push(0xff); + key.extend_from_slice(&pdu_id); + (key, Vec::new()) + }); + + self.tokenids.insert_batch(&mut batch)?; if body.starts_with(&format!("@conduit:{}: ", db.globals.server_name())) && self diff --git a/src/server_server.rs b/src/server_server.rs index aa70ce0..3ea1c0a 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -1,6 +1,6 @@ use crate::{ client_server::{self, claim_keys_helper, get_keys_helper}, - database::DatabaseGuard, + database::{abstraction::sqlite::MILLI, DatabaseGuard}, utils, ConduitResult, Database, Error, PduEvent, Result, Ruma, }; use get_profile_information::v1::ProfileField; @@ -1732,7 +1732,12 @@ fn get_auth_chain(starting_events: Vec, db: &Database) -> Result MILLI { + println!("auth chain for {} took {:?}", &event_id, elapsed) + } cache = db.rooms.auth_chain_cache(); @@ -1747,7 +1752,11 @@ fn get_auth_chain(starting_events: Vec, db: &Database) -> Result, db: &Database) -> Result> { +fn get_auth_chain_recursive( + event_id: &EventId, + mut found: HashSet, + db: &Database, +) -> Result> { if let Some(pdu) = db.rooms.get_pdu(&event_id)? { for auth_event in &pdu.auth_events { if !found.contains(auth_event) {