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::{
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto},
iter,
sync::{Arc, RwLock},
time::{Duration, Instant},
};
@ -740,7 +741,7 @@ async fn join_room_by_id_helper(
db.rooms.append_pdu(
&pdu,
utils::to_canonical_object(&pdu).expect("Pdu is valid canonical object"),
&[pdu.event_id.clone()],
iter::once(&*pdu.event_id),
db,
)?;

View file

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

View file

@ -64,6 +64,7 @@ use std::{
future::Future,
mem,
net::{IpAddr, SocketAddr},
ops::Deref,
pin::Pin,
sync::{Arc, RwLock, RwLockWriteGuard},
time::{Duration, Instant, SystemTime},
@ -1636,7 +1637,7 @@ async fn upgrade_outlier_to_timeline_pdu(
db,
&incoming_pdu,
val,
extremities,
extremities.iter().map(Deref::deref),
state_ids_compressed,
soft_fail,
&state_lock,
@ -1821,7 +1822,7 @@ async fn upgrade_outlier_to_timeline_pdu(
db,
&incoming_pdu,
val,
extremities,
extremities.iter().map(Deref::deref),
state_ids_compressed,
soft_fail,
&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
/// server that sent the event.
#[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,
pdu: &PduEvent,
pdu_json: CanonicalJsonObject,
new_room_leaves: HashSet<Box<EventId>>,
new_room_leaves: impl IntoIterator<Item = &'a EventId> + Clone + Debug,
state_ids_compressed: HashSet<CompressedStateEvent>,
soft_fail: bool,
_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 {
db.rooms
.mark_as_referenced(&pdu.room_id, &pdu.prev_events)?;
db.rooms.replace_pdu_leaves(
&pdu.room_id,
&new_room_leaves.into_iter().collect::<Vec<_>>(),
)?;
db.rooms
.replace_pdu_leaves(&pdu.room_id, new_room_leaves.clone())?;
return Ok(None);
}
let pdu_id = db.rooms.append_pdu(
pdu,
pdu_json,
&new_room_leaves.into_iter().collect::<Vec<_>>(),
db,
)?;
let pdu_id = db.rooms.append_pdu(pdu, pdu_json, new_room_leaves, db)?;
for appservice in db.appservice.all()? {
if db.rooms.appservice_in_room(&pdu.room_id, &appservice, db)? {