Reduce EventId copying

This commit is contained in:
Jonas Platte 2021-11-27 16:35:59 +01:00
parent 58ea081762
commit f712455047
No known key found for this signature in database
GPG key ID: CC154DE0E30B7C67
3 changed files with 20 additions and 19 deletions

View file

@ -31,6 +31,7 @@ use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use std::{ use std::{
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet}, collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
iter,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@ -740,7 +741,7 @@ async fn join_room_by_id_helper(
db.rooms.append_pdu( db.rooms.append_pdu(
&pdu, &pdu,
utils::to_canonical_object(&pdu).expect("Pdu is valid canonical object"), utils::to_canonical_object(&pdu).expect("Pdu is valid canonical object"),
&[pdu.event_id.clone()], iter::once(&*pdu.event_id),
db, db,
)?; )?;

View file

@ -36,6 +36,8 @@ use std::{
borrow::Cow, borrow::Cow,
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
fmt::Debug,
iter,
mem::size_of, mem::size_of,
sync::{Arc, Mutex, RwLock}, sync::{Arc, Mutex, RwLock},
time::Instant, time::Instant,
@ -1191,7 +1193,11 @@ impl Rooms {
/// The provided `event_ids` become the new leaves, this allows a room to have multiple /// The provided `event_ids` become the new leaves, this allows a room to have multiple
/// `prev_events`. /// `prev_events`.
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn replace_pdu_leaves(&self, room_id: &RoomId, event_ids: &[Box<EventId>]) -> Result<()> { pub fn replace_pdu_leaves<'a>(
&self,
room_id: &RoomId,
event_ids: impl IntoIterator<Item = &'a EventId> + Debug,
) -> Result<()> {
let mut prefix = room_id.as_bytes().to_vec(); let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
@ -1255,11 +1261,11 @@ impl Rooms {
/// ///
/// Returns pdu id /// Returns pdu id
#[tracing::instrument(skip(self, pdu, pdu_json, leaves, db))] #[tracing::instrument(skip(self, pdu, pdu_json, leaves, db))]
pub fn append_pdu( pub fn append_pdu<'a>(
&self, &self,
pdu: &PduEvent, pdu: &PduEvent,
mut pdu_json: CanonicalJsonObject, mut pdu_json: CanonicalJsonObject,
leaves: &[Box<EventId>], leaves: impl IntoIterator<Item = &'a EventId> + Debug,
db: &Database, db: &Database,
) -> Result<Vec<u8>> { ) -> Result<Vec<u8>> {
let shortroomid = self.get_shortroomid(&pdu.room_id)?.expect("room exists"); let shortroomid = self.get_shortroomid(&pdu.room_id)?.expect("room exists");
@ -2104,7 +2110,7 @@ impl Rooms {
pdu_json, pdu_json,
// Since this PDU references all pdu_leaves we can update the leaves // Since this PDU references all pdu_leaves we can update the leaves
// of the room // of the room
&[pdu.event_id.clone()], iter::once(&*pdu.event_id),
db, db,
)?; )?;

View file

@ -64,6 +64,7 @@ use std::{
future::Future, future::Future,
mem, mem,
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
ops::Deref,
pin::Pin, pin::Pin,
sync::{Arc, RwLock, RwLockWriteGuard}, sync::{Arc, RwLock, RwLockWriteGuard},
time::{Duration, Instant, SystemTime}, time::{Duration, Instant, SystemTime},
@ -1636,7 +1637,7 @@ async fn upgrade_outlier_to_timeline_pdu(
db, db,
&incoming_pdu, &incoming_pdu,
val, val,
extremities, extremities.iter().map(Deref::deref),
state_ids_compressed, state_ids_compressed,
soft_fail, soft_fail,
&state_lock, &state_lock,
@ -1821,7 +1822,7 @@ async fn upgrade_outlier_to_timeline_pdu(
db, db,
&incoming_pdu, &incoming_pdu,
val, val,
extremities, extremities.iter().map(Deref::deref),
state_ids_compressed, state_ids_compressed,
soft_fail, soft_fail,
&state_lock, &state_lock,
@ -2114,11 +2115,11 @@ pub(crate) async fn fetch_signing_keys(
/// Append the incoming event setting the state snapshot to the state from the /// Append the incoming event setting the state snapshot to the state from the
/// server that sent the event. /// server that sent the event.
#[tracing::instrument(skip(db, pdu, pdu_json, new_room_leaves, state_ids_compressed, _mutex_lock))] #[tracing::instrument(skip(db, pdu, pdu_json, new_room_leaves, state_ids_compressed, _mutex_lock))]
fn append_incoming_pdu( fn append_incoming_pdu<'a>(
db: &Database, db: &Database,
pdu: &PduEvent, pdu: &PduEvent,
pdu_json: CanonicalJsonObject, pdu_json: CanonicalJsonObject,
new_room_leaves: HashSet<Box<EventId>>, new_room_leaves: impl IntoIterator<Item = &'a EventId> + Clone + Debug,
state_ids_compressed: HashSet<CompressedStateEvent>, state_ids_compressed: HashSet<CompressedStateEvent>,
soft_fail: bool, soft_fail: bool,
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room mutex _mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room mutex
@ -2135,19 +2136,12 @@ fn append_incoming_pdu(
if soft_fail { if soft_fail {
db.rooms db.rooms
.mark_as_referenced(&pdu.room_id, &pdu.prev_events)?; .mark_as_referenced(&pdu.room_id, &pdu.prev_events)?;
db.rooms.replace_pdu_leaves( db.rooms
&pdu.room_id, .replace_pdu_leaves(&pdu.room_id, new_room_leaves.clone())?;
&new_room_leaves.into_iter().collect::<Vec<_>>(),
)?;
return Ok(None); return Ok(None);
} }
let pdu_id = db.rooms.append_pdu( let pdu_id = db.rooms.append_pdu(pdu, pdu_json, new_room_leaves, db)?;
pdu,
pdu_json,
&new_room_leaves.into_iter().collect::<Vec<_>>(),
db,
)?;
for appservice in db.appservice.all()? { for appservice in db.appservice.all()? {
if db.rooms.appservice_in_room(&pdu.room_id, &appservice, db)? { if db.rooms.appservice_in_room(&pdu.room_id, &appservice, db)? {