use fast binary_search for some const slices

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-12-15 01:41:50 -05:00
parent 52693db477
commit 6c96acc482
No known key found for this signature in database
6 changed files with 41 additions and 39 deletions

View file

@ -27,23 +27,24 @@ use crate::Ruma;
pub(crate) type LazySet = HashSet<OwnedUserId>;
/// list of safe and common non-state events to ignore if the user is ignored
const IGNORED_MESSAGE_TYPES: &[TimelineEventType; 16] = &[
const IGNORED_MESSAGE_TYPES: &[TimelineEventType; 17] = &[
Audio,
CallInvite,
Emote,
File,
Image,
KeyVerificationStart,
Location,
PollStart,
UnstablePollStart,
Beacon,
Reaction,
RoomEncrypted,
RoomMessage,
Sticker,
CallInvite,
CallNotify,
RoomEncrypted,
Image,
File,
Audio,
Voice,
Video,
UnstablePollStart,
PollStart,
KeyVerificationStart,
Reaction,
Emote,
Location,
Voice,
CallNotify,
];
const LIMIT_MAX: usize = 100;
@ -59,6 +60,7 @@ pub(crate) async fn get_message_events_route(
State(services): State<crate::State>,
body: Ruma<get_message_events::v3::Request>,
) -> Result<get_message_events::v3::Response> {
debug_assert!(IGNORED_MESSAGE_TYPES.is_sorted(), "IGNORED_MESSAGE_TYPES is not sorted");
let sender = body.sender();
let (sender_user, sender_device) = sender;
let room_id = &body.room_id;
@ -193,7 +195,7 @@ pub(crate) async fn update_lazy(
let (_, event) = &item;
let (sender_user, sender_device) = sender;
/* TODO: Remove the not "element_hacks" check when these are resolved:
/* TODO: Remove the "element_hacks" check when these are resolved:
* https://github.com/vector-im/element-android/issues/3417
* https://github.com/vector-im/element-web/issues/21034
*/
@ -231,19 +233,14 @@ pub(crate) async fn ignored_filter(
return None;
}
if IGNORED_MESSAGE_TYPES.iter().any(is_equal_to!(&pdu.kind))
&& services.users.user_is_ignored(&pdu.sender, user_id).await
{
return None;
}
if IGNORED_MESSAGE_TYPES.iter().any(is_equal_to!(&pdu.kind))
&& services
.server
.config
.forbidden_remote_server_names
.iter()
.any(is_equal_to!(pdu.sender().server_name()))
if IGNORED_MESSAGE_TYPES.binary_search(&pdu.kind).is_ok()
&& (services.users.user_is_ignored(&pdu.sender, user_id).await
|| services
.server
.config
.forbidden_remote_server_names
.iter()
.any(is_equal_to!(pdu.sender().server_name())))
{
return None;
}

View file

@ -21,15 +21,15 @@ use crate::Ruma;
/// Recommended transferable state events list from the spec
const TRANSFERABLE_STATE_EVENTS: &[StateEventType; 9] = &[
StateEventType::RoomServerAcl,
StateEventType::RoomEncryption,
StateEventType::RoomName,
StateEventType::RoomAvatar,
StateEventType::RoomTopic,
StateEventType::RoomEncryption,
StateEventType::RoomGuestAccess,
StateEventType::RoomHistoryVisibility,
StateEventType::RoomJoinRules,
StateEventType::RoomName,
StateEventType::RoomPowerLevels,
StateEventType::RoomServerAcl,
StateEventType::RoomTopic,
];
/// # `POST /_matrix/client/r0/rooms/{roomId}/upgrade`
@ -46,6 +46,10 @@ pub(crate) async fn upgrade_room_route(
State(services): State<crate::State>,
body: Ruma<upgrade_room::v3::Request>,
) -> Result<upgrade_room::v3::Response> {
debug_assert!(
TRANSFERABLE_STATE_EVENTS.is_sorted(),
"TRANSFERABLE_STATE_EVENTS is not sorted"
);
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if !services.server.supported_room_version(&body.new_version) {

View file

@ -30,7 +30,6 @@ use ruma::{
TimelineEventType::{self, *},
},
serde::Raw,
state_res::Event,
uint, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, UInt, UserId,
};
use service::{rooms::read_receipt::pack_receipts, Services};
@ -39,8 +38,9 @@ use super::{load_timeline, share_encrypted_room};
use crate::{client::ignored_filter, Ruma};
const SINGLE_CONNECTION_SYNC: &str = "single_connection_sync";
const DEFAULT_BUMP_TYPES: &[TimelineEventType; 6] =
&[RoomMessage, RoomEncrypted, Sticker, CallInvite, PollStart, Beacon];
&[CallInvite, PollStart, Beacon, RoomEncrypted, RoomMessage, Sticker];
/// POST `/_matrix/client/unstable/org.matrix.msc3575/sync`
///
@ -49,6 +49,7 @@ pub(crate) async fn sync_events_v4_route(
State(services): State<crate::State>,
body: Ruma<sync_events::v4::Request>,
) -> Result<sync_events::v4::Response> {
debug_assert!(DEFAULT_BUMP_TYPES.is_sorted(), "DEFAULT_BUMP_TYPES is not sorted");
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.expect("user is authenticated");
let mut body = body.body;
@ -595,7 +596,7 @@ pub(crate) async fn sync_events_v4_route(
for (_, pdu) in timeline_pdus {
let ts = MilliSecondsSinceUnixEpoch(pdu.origin_server_ts);
if DEFAULT_BUMP_TYPES.contains(pdu.event_type())
if DEFAULT_BUMP_TYPES.binary_search(&pdu.kind).is_ok()
&& timestamp.is_none_or(|time| time <= ts)
{
timestamp = Some(ts);

View file

@ -11,7 +11,7 @@ use conduwuit::{
err, error,
pdu::PduBuilder,
utils::{math::usize_from_f64, ReadyExt},
Err, Error, Event, PduEvent, Result,
Err, Error, PduEvent, Result,
};
use futures::StreamExt;
use lru_cache::LruCache;
@ -507,7 +507,7 @@ impl Service {
if redacting_event
.as_ref()
.is_ok_and(|event| event.event_type() == &TimelineEventType::RoomCreate)
.is_ok_and(|pdu| pdu.kind == TimelineEventType::RoomCreate)
{
return Err!(Request(Forbidden("Redacting m.room.create is not safe, forbidding.")));
}

View file

@ -1262,7 +1262,7 @@ impl Service {
#[implement(Service)]
#[tracing::instrument(skip_all, level = "debug")]
async fn check_pdu_for_admin_room(&self, pdu: &PduEvent, sender: &UserId) -> Result<()> {
match pdu.event_type() {
match &pdu.kind {
| TimelineEventType::RoomEncryption => {
return Err!(Request(Forbidden(error!("Encryption not supported in admins room."))));
},

View file

@ -40,7 +40,7 @@ struct CheckForUpdatesResponseEntry {
const CHECK_FOR_UPDATES_URL: &str = "https://pupbrain.dev/check-for-updates/stable";
const CHECK_FOR_UPDATES_INTERVAL: u64 = 7200; // 2 hours
const LAST_CHECK_FOR_UPDATES_COUNT: &[u8] = b"u";
const LAST_CHECK_FOR_UPDATES_COUNT: &[u8; 1] = b"u";
#[async_trait]
impl crate::Service for Service {