Remove lots of redundant string allocations

This commit is contained in:
Jonas Platte 2021-04-05 21:44:21 +02:00
parent fe744c856f
commit dbe8c2ce19
No known key found for this signature in database
GPG key ID: CC154DE0E30B7C67
7 changed files with 87 additions and 95 deletions

View file

@ -34,7 +34,7 @@ impl RoomEdus {
event: EduEvent,
globals: &super::super::globals::Globals,
) -> Result<()> {
let mut prefix = room_id.to_string().as_bytes().to_vec();
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
// Remove old entry
@ -49,7 +49,7 @@ impl RoomEdus {
key.rsplit(|&b| b == 0xff)
.next()
.expect("rsplit always returns an element")
== user_id.to_string().as_bytes()
== user_id.as_bytes()
})
{
// This is the old room_latest
@ -59,7 +59,7 @@ impl RoomEdus {
let mut room_latest_id = prefix;
room_latest_id.extend_from_slice(&globals.next_count()?.to_be_bytes());
room_latest_id.push(0xff);
room_latest_id.extend_from_slice(&user_id.to_string().as_bytes());
room_latest_id.extend_from_slice(&user_id.as_bytes());
self.readreceiptid_readreceipt.insert(
room_latest_id,
@ -76,7 +76,7 @@ impl RoomEdus {
room_id: &RoomId,
since: u64,
) -> Result<impl Iterator<Item = Result<Raw<ruma::events::AnySyncEphemeralRoomEvent>>>> {
let mut prefix = room_id.to_string().as_bytes().to_vec();
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
let mut first_possible_edu = prefix.clone();
@ -102,9 +102,9 @@ impl RoomEdus {
count: u64,
globals: &super::super::globals::Globals,
) -> Result<()> {
let mut key = room_id.to_string().as_bytes().to_vec();
let mut key = room_id.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(&user_id.to_string().as_bytes());
key.extend_from_slice(&user_id.as_bytes());
self.roomuserid_privateread
.insert(&key, &count.to_be_bytes())?;
@ -118,9 +118,9 @@ impl RoomEdus {
/// Returns the private read marker.
#[tracing::instrument(skip(self))]
pub fn private_read_get(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<u64>> {
let mut key = room_id.to_string().as_bytes().to_vec();
let mut key = room_id.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(&user_id.to_string().as_bytes());
key.extend_from_slice(&user_id.as_bytes());
self.roomuserid_privateread.get(key)?.map_or(Ok(None), |v| {
Ok(Some(utils::u64_from_bytes(&v).map_err(|_| {
@ -131,9 +131,9 @@ impl RoomEdus {
/// Returns the count of the last typing update in this room.
pub fn last_privateread_update(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
let mut key = room_id.to_string().as_bytes().to_vec();
let mut key = room_id.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(&user_id.to_string().as_bytes());
key.extend_from_slice(&user_id.as_bytes());
Ok(self
.roomuserid_lastprivatereadupdate
@ -155,7 +155,7 @@ impl RoomEdus {
timeout: u64,
globals: &super::super::globals::Globals,
) -> Result<()> {
let mut prefix = room_id.to_string().as_bytes().to_vec();
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
let count = globals.next_count()?.to_be_bytes();
@ -166,10 +166,10 @@ impl RoomEdus {
room_typing_id.extend_from_slice(&count);
self.typingid_userid
.insert(&room_typing_id, &*user_id.to_string().as_bytes())?;
.insert(&room_typing_id, &*user_id.as_bytes())?;
self.roomid_lasttypingupdate
.insert(&room_id.to_string().as_bytes(), &count)?;
.insert(&room_id.as_bytes(), &count)?;
Ok(())
}
@ -181,7 +181,7 @@ impl RoomEdus {
room_id: &RoomId,
globals: &super::super::globals::Globals,
) -> Result<()> {
let mut prefix = room_id.to_string().as_bytes().to_vec();
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
let user_id = user_id.to_string();
@ -200,10 +200,8 @@ impl RoomEdus {
}
if found_outdated {
self.roomid_lasttypingupdate.insert(
&room_id.to_string().as_bytes(),
&globals.next_count()?.to_be_bytes(),
)?;
self.roomid_lasttypingupdate
.insert(&room_id.as_bytes(), &globals.next_count()?.to_be_bytes())?;
}
Ok(())
@ -215,7 +213,7 @@ impl RoomEdus {
room_id: &RoomId,
globals: &super::super::globals::Globals,
) -> Result<()> {
let mut prefix = room_id.to_string().as_bytes().to_vec();
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
let current_timestamp = utils::millis_since_unix_epoch();
@ -248,10 +246,8 @@ impl RoomEdus {
}
if found_outdated {
self.roomid_lasttypingupdate.insert(
&room_id.to_string().as_bytes(),
&globals.next_count()?.to_be_bytes(),
)?;
self.roomid_lasttypingupdate
.insert(&room_id.as_bytes(), &globals.next_count()?.to_be_bytes())?;
}
Ok(())
@ -268,7 +264,7 @@ impl RoomEdus {
Ok(self
.roomid_lasttypingupdate
.get(&room_id.to_string().as_bytes())?
.get(&room_id.as_bytes())?
.map_or(Ok::<_, Error>(None), |bytes| {
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Count in roomid_lastroomactiveupdate is invalid.")
@ -281,7 +277,7 @@ impl RoomEdus {
&self,
room_id: &RoomId,
) -> Result<SyncEphemeralRoomEvent<ruma::events::typing::TypingEventContent>> {
let mut prefix = room_id.to_string().as_bytes().to_vec();
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
let mut user_ids = Vec::new();
@ -322,11 +318,11 @@ impl RoomEdus {
let count = globals.next_count()?.to_be_bytes();
let mut presence_id = room_id.to_string().as_bytes().to_vec();
let mut presence_id = room_id.as_bytes().to_vec();
presence_id.push(0xff);
presence_id.extend_from_slice(&count);
presence_id.push(0xff);
presence_id.extend_from_slice(&presence.sender.to_string().as_bytes());
presence_id.extend_from_slice(&presence.sender.as_bytes());
self.presenceid_presence.insert(
presence_id,
@ -334,7 +330,7 @@ impl RoomEdus {
)?;
self.userid_lastpresenceupdate.insert(
&user_id.to_string().as_bytes(),
&user_id.as_bytes(),
&utils::millis_since_unix_epoch().to_be_bytes(),
)?;
@ -345,7 +341,7 @@ impl RoomEdus {
#[tracing::instrument(skip(self))]
pub fn ping_presence(&self, user_id: &UserId) -> Result<()> {
self.userid_lastpresenceupdate.insert(
&user_id.to_string().as_bytes(),
&user_id.as_bytes(),
&utils::millis_since_unix_epoch().to_be_bytes(),
)?;
@ -355,7 +351,7 @@ impl RoomEdus {
/// Returns the timestamp of the last presence update of this user in millis since the unix epoch.
pub fn last_presence_update(&self, user_id: &UserId) -> Result<Option<u64>> {
self.userid_lastpresenceupdate
.get(&user_id.to_string().as_bytes())?
.get(&user_id.as_bytes())?
.map(|bytes| {
utils::u64_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Invalid timestamp in userid_lastpresenceupdate.")
@ -398,7 +394,7 @@ impl RoomEdus {
.try_into()
.map_err(|_| Error::bad_database("Invalid UserId in userid_lastpresenceupdate."))?;
for room_id in rooms.rooms_joined(&user_id).filter_map(|r| r.ok()) {
let mut presence_id = room_id.to_string().as_bytes().to_vec();
let mut presence_id = room_id.as_bytes().to_vec();
presence_id.push(0xff);
presence_id.extend_from_slice(&count);
presence_id.push(0xff);
@ -424,7 +420,7 @@ impl RoomEdus {
}
self.userid_lastpresenceupdate.insert(
&user_id.to_string().as_bytes(),
&user_id.as_bytes(),
&utils::millis_since_unix_epoch().to_be_bytes(),
)?;
}
@ -443,7 +439,7 @@ impl RoomEdus {
) -> Result<HashMap<UserId, PresenceEvent>> {
self.presence_maintain(rooms, globals)?;
let mut prefix = room_id.to_string().as_bytes().to_vec();
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
let mut first_possible_edu = prefix.clone();