optimize bytes conversion utils

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-30 18:20:39 +00:00
parent f43c09b05d
commit 0613140130
3 changed files with 26 additions and 18 deletions

View file

@ -28,6 +28,12 @@ pub enum Error {
Fmt(#[from] fmt::Error),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("{0}")]
Utf8Error(#[from] std::str::Utf8Error),
#[error("{0}")]
FromUtf8Error(#[from] std::string::FromUtf8Error),
#[error("{0}")]
TryFromSliceError(#[from] std::array::TryFromSliceError),
// third-party
#[error("Regex error: {0}")]

View file

@ -19,10 +19,10 @@ pub use json::{deserialize_from_str, to_canonical_object};
pub use mutex_map::MutexMap;
use rand::prelude::*;
use ring::digest;
use ruma::OwnedUserId;
use ruma::UserId;
pub use sys::available_parallelism;
use crate::{Error, Result};
use crate::Result;
pub fn clamp<T: Ord>(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) }
@ -38,9 +38,8 @@ pub fn millis_since_unix_epoch() -> u64 {
#[inline]
#[must_use]
pub fn increment(old: Option<&[u8]>) -> [u8; 8] {
const ZERO: u64 = 0;
old.map(TryInto::try_into)
.map_or(ZERO, |val| val.map_or(ZERO, u64::from_be_bytes))
.map_or(0_u64, |val| val.map_or(0_u64, u64::from_be_bytes))
.wrapping_add(1)
.to_be_bytes()
}
@ -55,23 +54,26 @@ pub fn generate_keypair() -> Vec<u8> {
value
}
/// Parses the bytes into an u64.
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64, std::array::TryFromSliceError> {
let array: [u8; 8] = bytes.try_into()?;
Ok(u64::from_be_bytes(array))
/// Parses a `UserId` from bytes.
pub fn user_id_from_bytes(bytes: &[u8]) -> Result<&UserId> {
let str: &str = str_from_bytes(bytes)?;
let user_id: &UserId = str.try_into()?;
Ok(user_id)
}
/// Parses the bytes into a string.
pub fn string_from_bytes(bytes: &[u8]) -> Result<String, std::string::FromUtf8Error> {
String::from_utf8(bytes.to_vec())
pub fn string_from_bytes(bytes: &[u8]) -> Result<String> {
let str: &str = str_from_bytes(bytes)?;
Ok(str.to_owned())
}
/// Parses a `OwnedUserId` from bytes.
pub fn user_id_from_bytes(bytes: &[u8]) -> Result<OwnedUserId> {
OwnedUserId::try_from(
string_from_bytes(bytes).map_err(|_| Error::bad_database("Failed to parse string from bytes"))?,
)
.map_err(|_| Error::bad_database("Failed to parse user id from bytes"))
/// Parses the bytes into a string.
pub fn str_from_bytes(bytes: &[u8]) -> Result<&str> { Ok(std::str::from_utf8(bytes)?) }
/// Parses the bytes into an u64.
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64> {
let array: [u8; 8] = bytes.try_into()?;
Ok(u64::from_be_bytes(array))
}
pub fn random_string(length: usize) -> String {

View file

@ -115,7 +115,7 @@ impl Data {
.iter()
.flat_map(|(key, presence_bytes)| -> Result<(OwnedUserId, u64, Vec<u8>)> {
let (count, user_id) = presenceid_parse(&key)?;
Ok((user_id, count, presence_bytes))
Ok((user_id.to_owned(), count, presence_bytes))
})
.filter(move |(_, count, _)| *count > since),
)
@ -128,7 +128,7 @@ fn presenceid_key(count: u64, user_id: &UserId) -> Vec<u8> {
}
#[inline]
fn presenceid_parse(key: &[u8]) -> Result<(u64, OwnedUserId)> {
fn presenceid_parse(key: &[u8]) -> Result<(u64, &UserId)> {
let (count, user_id) = key.split_at(8);
let user_id = utils::user_id_from_bytes(user_id)?;
let count = utils::u64_from_bytes(count).unwrap();