Skip to content

Commit

Permalink
use fast binary_search for some const slices
Browse files Browse the repository at this point in the history
Signed-off-by: strawberry <[email protected]>
  • Loading branch information
girlbossceo committed Dec 15, 2024
1 parent 52693db commit 6c96acc
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
51 changes: 24 additions & 27 deletions src/api/client/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] = &[
RoomMessage,
Sticker,
const IGNORED_MESSAGE_TYPES: &[TimelineEventType; 17] = &[
Audio,
CallInvite,
CallNotify,
RoomEncrypted,
Image,
Emote,
File,
Audio,
Voice,
Video,
UnstablePollStart,
PollStart,
Image,
KeyVerificationStart,
Reaction,
Emote,
Location,
PollStart,
UnstablePollStart,
Beacon,
Reaction,
RoomEncrypted,
RoomMessage,
Sticker,
Video,
Voice,
CallNotify,
];

const LIMIT_MAX: usize = 100;
Expand All @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 8 additions & 4 deletions src/api/client/room/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions src/api/client/sync/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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`
///
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/service/rooms/state_accessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.")));
}
Expand Down
2 changes: 1 addition & 1 deletion src/service/rooms/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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."))));
},
Expand Down
2 changes: 1 addition & 1 deletion src/service/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6c96acc

Please sign in to comment.