Skip to content

Commit 0220689

Browse files
jplattepoljar
authored andcommitted
refactor: Wrap EncryptionInfo in Arc
It's >100 bytes large and often optional, so it makes sense to put it on the heap to reduce the size of structs with such optional fields, and the stack size of functions with such optional parameters. It's also cloned in a couple places in the UI crate, so it probably makes sense to just always refcount it. This started as a clippy suggestion to box PendingEdit inside AggregationKind::Edit.
1 parent d8969db commit 0220689

File tree

16 files changed

+62
-52
lines changed

16 files changed

+62
-52
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ ruma = { git = "https://github.com/ruma/ruma", rev = "b4941a991919685345646a230b
7979
ruma-common = { git = "https://github.com/ruma/ruma", rev = "b4941a991919685345646a230b05b985338ec3f8" }
8080
sentry = "0.36.0"
8181
sentry-tracing = "0.36.0"
82-
serde = "1.0.217"
82+
serde = { version = "1.0.217", features = ["rc"] }
8383
serde_html_form = "0.2.7"
8484
serde_json = "1.0.138"
8585
sha2 = "0.10.8"

crates/matrix-sdk-base/src/event_cache/store/integration_tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
//! Trait and macro of integration tests for `EventCacheStore` implementations.
1616
17+
use std::sync::Arc;
18+
1719
use assert_matches::assert_matches;
1820
use matrix_sdk_common::{
1921
deserialized_responses::{
@@ -55,7 +57,7 @@ pub fn make_test_event_with_event_id(
5557
content: &str,
5658
event_id: Option<&EventId>,
5759
) -> TimelineEvent {
58-
let encryption_info = EncryptionInfo {
60+
let encryption_info = Arc::new(EncryptionInfo {
5961
sender: (*ALICE).into(),
6062
sender_device: None,
6163
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
@@ -64,7 +66,7 @@ pub fn make_test_event_with_event_id(
6466
session_id: Some("mysessionid9".to_owned()),
6567
},
6668
verification_state: VerificationState::Verified,
67-
};
69+
});
6870

6971
let mut builder = EventFactory::new().text_msg(content).room(room_id).sender(*ALICE);
7072
if let Some(event_id) = event_id {

crates/matrix-sdk-common/src/deserialized_responses.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{collections::BTreeMap, fmt};
15+
use std::{collections::BTreeMap, fmt, sync::Arc};
1616

1717
#[cfg(doc)]
1818
use ruma::events::AnyTimelineEvent;
@@ -462,7 +462,7 @@ impl TimelineEvent {
462462

463463
/// If the event was a decrypted event that was successfully decrypted, get
464464
/// its encryption info. Otherwise, `None`.
465-
pub fn encryption_info(&self) -> Option<&EncryptionInfo> {
465+
pub fn encryption_info(&self) -> Option<&Arc<EncryptionInfo>> {
466466
self.kind.encryption_info()
467467
}
468468

@@ -569,7 +569,7 @@ impl TimelineEventKind {
569569

570570
/// If the event was a decrypted event that was successfully decrypted, get
571571
/// its encryption info. Otherwise, `None`.
572-
pub fn encryption_info(&self) -> Option<&EncryptionInfo> {
572+
pub fn encryption_info(&self) -> Option<&Arc<EncryptionInfo>> {
573573
match self {
574574
TimelineEventKind::Decrypted(d) => Some(&d.encryption_info),
575575
TimelineEventKind::UnableToDecrypt { .. } | TimelineEventKind::PlainText { .. } => None,
@@ -649,7 +649,7 @@ pub struct DecryptedRoomEvent {
649649
pub event: Raw<AnyMessageLikeEvent>,
650650

651651
/// The encryption info about the event.
652-
pub encryption_info: EncryptionInfo,
652+
pub encryption_info: Arc<EncryptionInfo>,
653653

654654
/// The encryption info about the events bundled in the `unsigned`
655655
/// object.
@@ -706,15 +706,15 @@ impl UnsignedEventLocation {
706706
#[derive(Debug, Clone, Serialize, Deserialize)]
707707
pub enum UnsignedDecryptionResult {
708708
/// The event was successfully decrypted.
709-
Decrypted(EncryptionInfo),
709+
Decrypted(Arc<EncryptionInfo>),
710710
/// The event failed to be decrypted.
711711
UnableToDecrypt(UnableToDecryptInfo),
712712
}
713713

714714
impl UnsignedDecryptionResult {
715715
/// Returns the encryption info for this bundled event if it was
716716
/// successfully decrypted.
717-
pub fn encryption_info(&self) -> Option<&EncryptionInfo> {
717+
pub fn encryption_info(&self) -> Option<&Arc<EncryptionInfo>> {
718718
match self {
719719
Self::Decrypted(info) => Some(info),
720720
Self::UnableToDecrypt(_) => None,
@@ -918,9 +918,10 @@ struct SyncTimelineEventDeserializationHelperV0 {
918918
/// The actual event.
919919
event: Raw<AnySyncTimelineEvent>,
920920

921-
/// The encryption info about the event. Will be `None` if the event
922-
/// was not encrypted.
923-
encryption_info: Option<EncryptionInfo>,
921+
/// The encryption info about the event.
922+
///
923+
/// Will be `None` if the event was not encrypted.
924+
encryption_info: Option<Arc<EncryptionInfo>>,
924925

925926
/// The push actions associated with this event.
926927
#[serde(default)]
@@ -966,7 +967,7 @@ impl From<SyncTimelineEventDeserializationHelperV0> for TimelineEvent {
966967

967968
#[cfg(test)]
968969
mod tests {
969-
use std::collections::BTreeMap;
970+
use std::{collections::BTreeMap, sync::Arc};
970971

971972
use assert_matches::assert_matches;
972973
use insta::{assert_json_snapshot, with_settings};
@@ -1118,7 +1119,7 @@ mod tests {
11181119
let room_event = TimelineEvent {
11191120
kind: TimelineEventKind::Decrypted(DecryptedRoomEvent {
11201121
event: Raw::new(&example_event()).unwrap().cast(),
1121-
encryption_info: EncryptionInfo {
1122+
encryption_info: Arc::new(EncryptionInfo {
11221123
sender: user_id!("@sender:example.com").to_owned(),
11231124
sender_device: None,
11241125
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
@@ -1127,7 +1128,7 @@ mod tests {
11271128
session_id: Some("xyz".to_owned()),
11281129
},
11291130
verification_state: VerificationState::Verified,
1130-
},
1131+
}),
11311132
unsigned_encryption_info: Some(BTreeMap::from([(
11321133
UnsignedEventLocation::RelationsReplace,
11331134
UnsignedDecryptionResult::UnableToDecrypt(UnableToDecryptInfo {
@@ -1490,7 +1491,7 @@ mod tests {
14901491
let room_event = TimelineEvent {
14911492
kind: TimelineEventKind::Decrypted(DecryptedRoomEvent {
14921493
event: Raw::new(&example_event()).unwrap().cast(),
1493-
encryption_info: EncryptionInfo {
1494+
encryption_info: Arc::new(EncryptionInfo {
14941495
sender: user_id!("@sender:example.com").to_owned(),
14951496
sender_device: Some(device_id!("ABCDEFGHIJ").to_owned()),
14961497
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
@@ -1508,7 +1509,7 @@ mod tests {
15081509
session_id: Some("mysessionid112".to_owned()),
15091510
},
15101511
verification_state: VerificationState::Verified,
1511-
},
1512+
}),
15121513
unsigned_encryption_info: Some(BTreeMap::from([(
15131514
UnsignedEventLocation::RelationsThreadLatestEvent,
15141515
UnsignedDecryptionResult::UnableToDecrypt(UnableToDecryptInfo {

crates/matrix-sdk-crypto/src/machine/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,13 +1760,13 @@ impl OlmMachine {
17601760
&self,
17611761
session: &InboundGroupSession,
17621762
sender: &UserId,
1763-
) -> MegolmResult<EncryptionInfo> {
1763+
) -> MegolmResult<Arc<EncryptionInfo>> {
17641764
let (verification_state, device_id) =
17651765
self.get_or_update_verification_state(session, sender).await?;
17661766

17671767
let sender = sender.to_owned();
17681768

1769-
Ok(EncryptionInfo {
1769+
Ok(Arc::new(EncryptionInfo {
17701770
sender,
17711771
sender_device: device_id,
17721772
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
@@ -1779,7 +1779,7 @@ impl OlmMachine {
17791779
session_id: Some(session.session_id().to_owned()),
17801780
},
17811781
verification_state,
1782-
})
1782+
}))
17831783
}
17841784

17851785
async fn decrypt_megolm_events(
@@ -1788,7 +1788,7 @@ impl OlmMachine {
17881788
event: &EncryptedEvent,
17891789
content: &SupportedEventEncryptionSchemes<'_>,
17901790
decryption_settings: &DecryptionSettings,
1791-
) -> MegolmResult<(JsonObject, EncryptionInfo)> {
1791+
) -> MegolmResult<(JsonObject, Arc<EncryptionInfo>)> {
17921792
let session =
17931793
self.get_inbound_group_session_or_error(room_id, content.session_id()).await?;
17941794

@@ -2200,7 +2200,7 @@ impl OlmMachine {
22002200
&self,
22012201
event: &Raw<EncryptedEvent>,
22022202
room_id: &RoomId,
2203-
) -> MegolmResult<EncryptionInfo> {
2203+
) -> MegolmResult<Arc<EncryptionInfo>> {
22042204
let event = event.deserialize()?;
22052205

22062206
let content: SupportedEventEncryptionSchemes<'_> = match &event.content.scheme {
@@ -2233,7 +2233,7 @@ impl OlmMachine {
22332233
room_id: &RoomId,
22342234
session_id: &str,
22352235
sender: &UserId,
2236-
) -> MegolmResult<EncryptionInfo> {
2236+
) -> MegolmResult<Arc<EncryptionInfo>> {
22372237
let session = self.get_inbound_group_session_or_error(room_id, session_id).await?;
22382238
self.get_encryption_info(&session, sender).await
22392239
}

crates/matrix-sdk-crypto/src/machine/tests/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,12 @@ async fn test_session_encryption_info_can_be_fetched() {
443443

444444
// Then the expected info is returned
445445
assert_eq!(encryption_info.sender, alice_id());
446-
assert_eq!(encryption_info.sender_device.unwrap(), alice_device_id());
446+
assert_eq!(encryption_info.sender_device.as_deref(), Some(alice_device_id()));
447447
assert_matches!(
448-
encryption_info.algorithm_info,
448+
&encryption_info.algorithm_info,
449449
AlgorithmInfo::MegolmV1AesSha2 { curve25519_key, .. }
450450
);
451-
assert_eq!(curve25519_key, alice_session.sender_key().to_string());
451+
assert_eq!(*curve25519_key, alice_session.sender_key().to_string());
452452
assert_eq!(
453453
encryption_info.verification_state,
454454
VerificationState::Unverified(VerificationLevel::UnsignedDevice)

crates/matrix-sdk-ui/src/timeline/controller/aggregations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
//! to cater for the first use case, and to never lose any aggregations in the
3838
//! second use case.
3939
40-
use std::{borrow::Cow, collections::HashMap};
40+
use std::{borrow::Cow, collections::HashMap, sync::Arc};
4141

4242
use as_variant::as_variant;
4343
use matrix_sdk::deserialized_responses::EncryptionInfo;
@@ -82,7 +82,7 @@ pub(in crate::timeline) struct PendingEdit {
8282
pub edit_json: Option<Raw<AnySyncTimelineEvent>>,
8383

8484
/// The encryption info for this edit.
85-
pub encryption_info: Option<EncryptionInfo>,
85+
pub encryption_info: Option<Arc<EncryptionInfo>>,
8686
}
8787

8888
/// Which kind of aggregation (related event) is this?

crates/matrix-sdk-ui/src/timeline/controller/decryption_retry_task.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ mod tests {
506506
read_receipts: Default::default(),
507507
is_own: false,
508508
is_highlighted: false,
509-
encryption_info: Some(EncryptionInfo {
509+
encryption_info: Some(Arc::new(EncryptionInfo {
510510
sender: owned_user_id!("@u:s.co"),
511511
sender_device: None,
512512
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
@@ -515,7 +515,7 @@ mod tests {
515515
session_id: Some(session_id.to_owned()),
516516
},
517517
verification_state: VerificationState::Verified,
518-
}),
518+
})),
519519
original_json: None,
520520
latest_edit_json: None,
521521
origin: RemoteEventOrigin::Sync,

crates/matrix-sdk-ui/src/timeline/event_handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub(super) enum Flow {
8585
/// Where should this be added in the timeline.
8686
position: TimelineItemPosition,
8787
/// Information about the encryption for this event.
88-
encryption_info: Option<EncryptionInfo>,
88+
encryption_info: Option<Arc<EncryptionInfo>>,
8989
},
9090
}
9191

@@ -150,7 +150,7 @@ pub(super) struct RemoteEventContext<'a> {
150150
event_id: &'a EventId,
151151
raw_event: &'a Raw<AnySyncTimelineEvent>,
152152
relations: BundledMessageLikeRelations<AnySyncMessageLikeEvent>,
153-
bundled_edit_encryption_info: Option<EncryptionInfo>,
153+
bundled_edit_encryption_info: Option<Arc<EncryptionInfo>>,
154154
}
155155

156156
/// An action that we want to cause on the timeline.
@@ -195,7 +195,7 @@ impl TimelineAction {
195195
raw_event: &Raw<AnySyncTimelineEvent>,
196196
room_data_provider: &P,
197197
unable_to_decrypt_info: Option<UnableToDecryptInfo>,
198-
bundled_edit_encryption_info: Option<EncryptionInfo>,
198+
bundled_edit_encryption_info: Option<Arc<EncryptionInfo>>,
199199
timeline_items: &Vector<Arc<TimelineItem>>,
200200
meta: &mut TimelineMetadata,
201201
) -> Option<Self> {

crates/matrix-sdk-ui/src/timeline/event_item/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl EventTimelineItem {
398398
pub fn encryption_info(&self) -> Option<&EncryptionInfo> {
399399
match &self.kind {
400400
EventTimelineItemKind::Local(_) => None,
401-
EventTimelineItemKind::Remote(remote_event) => remote_event.encryption_info.as_ref(),
401+
EventTimelineItemKind::Remote(remote_event) => remote_event.encryption_info.as_deref(),
402402
}
403403
}
404404

@@ -521,7 +521,10 @@ impl EventTimelineItem {
521521
}
522522

523523
/// Clone the current event item, and update its `encryption_info`.
524-
pub(super) fn with_encryption_info(&self, encryption_info: Option<EncryptionInfo>) -> Self {
524+
pub(super) fn with_encryption_info(
525+
&self,
526+
encryption_info: Option<Arc<EncryptionInfo>>,
527+
) -> Self {
525528
let mut new = self.clone();
526529
if let EventTimelineItemKind::Remote(r) = &mut new.kind {
527530
r.encryption_info = encryption_info;

crates/matrix-sdk-ui/src/timeline/event_item/remote.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::fmt;
15+
use std::{fmt, sync::Arc};
1616

1717
use indexmap::IndexMap;
1818
use matrix_sdk::deserialized_responses::EncryptionInfo;
@@ -46,7 +46,7 @@ pub(in crate::timeline) struct RemoteEventTimelineItem {
4646
pub is_highlighted: bool,
4747

4848
/// Encryption information.
49-
pub encryption_info: Option<EncryptionInfo>,
49+
pub encryption_info: Option<Arc<EncryptionInfo>>,
5050

5151
/// JSON of the original event.
5252
///

0 commit comments

Comments
 (0)