Skip to content

Commit 471e3c3

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): add timers to all EventCacheStore functions for easy performance tracking
Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
1 parent 74972d8 commit 471e3c3

File tree

1 file changed

+60
-1
lines changed
  • crates/matrix-sdk-indexeddb/src/event_cache_store

1 file changed

+60
-1
lines changed

crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use matrix_sdk_base::{
2828
RawChunk, Update,
2929
},
3030
media::MediaRequestParameters,
31+
timer,
3132
};
3233
use ruma::{events::relation::RelationType, EventId, MxcUri, OwnedEventId, RoomId};
33-
use tracing::trace;
34+
use tracing::{instrument, trace};
3435
use web_sys::IdbTransactionMode;
3536

3637
use crate::event_cache_store::{
@@ -123,23 +124,28 @@ macro_rules! impl_event_cache_store {
123124
}
124125

125126
impl_event_cache_store! {
127+
#[instrument(skip(self))]
126128
async fn try_take_leased_lock(
127129
&self,
128130
lease_duration_ms: u32,
129131
key: &str,
130132
holder: &str,
131133
) -> Result<bool, IndexeddbEventCacheStoreError> {
134+
let _timer = timer!("method");
132135
self.memory_store
133136
.try_take_leased_lock(lease_duration_ms, key, holder)
134137
.await
135138
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
136139
}
137140

141+
#[instrument(skip(self, updates))]
138142
async fn handle_linked_chunk_updates(
139143
&self,
140144
linked_chunk_id: LinkedChunkId<'_>,
141145
updates: Vec<Update<Event, Gap>>,
142146
) -> Result<(), IndexeddbEventCacheStoreError> {
147+
let _timer = timer!("method");
148+
143149
let linked_chunk_id = linked_chunk_id.to_owned();
144150
let room_id = linked_chunk_id.room_id();
145151

@@ -258,10 +264,13 @@ impl_event_cache_store! {
258264
Ok(())
259265
}
260266

267+
#[instrument(skip(self))]
261268
async fn load_all_chunks(
262269
&self,
263270
linked_chunk_id: LinkedChunkId<'_>,
264271
) -> Result<Vec<RawChunk<Event, Gap>>, IndexeddbEventCacheStoreError> {
272+
let _ = timer!("method");
273+
265274
let linked_chunk_id = linked_chunk_id.to_owned();
266275
let room_id = linked_chunk_id.room_id();
267276

@@ -283,10 +292,23 @@ impl_event_cache_store! {
283292
Ok(raw_chunks)
284293
}
285294

295+
#[instrument(skip(self))]
286296
async fn load_all_chunks_metadata(
287297
&self,
288298
linked_chunk_id: LinkedChunkId<'_>,
289299
) -> Result<Vec<ChunkMetadata>, IndexeddbEventCacheStoreError> {
300+
// TODO: This call could possibly take a very long time and the
301+
// amount of time increases linearly with the number of chunks
302+
// it needs to load from the database. This will likely require
303+
// some refactoring to deal with performance issues.
304+
//
305+
// For details on the performance penalties associated with this
306+
// call, see https://github.com/matrix-org/matrix-rust-sdk/pull/5407.
307+
//
308+
// For how this was improved in the SQLite implementation, see
309+
// https://github.com/matrix-org/matrix-rust-sdk/pull/5382.
310+
let _ = timer!("method");
311+
290312
let linked_chunk_id = linked_chunk_id.to_owned();
291313
let room_id = linked_chunk_id.room_id();
292314

@@ -310,13 +332,16 @@ impl_event_cache_store! {
310332
Ok(raw_chunks)
311333
}
312334

335+
#[instrument(skip(self))]
313336
async fn load_last_chunk(
314337
&self,
315338
linked_chunk_id: LinkedChunkId<'_>,
316339
) -> Result<
317340
(Option<RawChunk<Event, Gap>>, ChunkIdentifierGenerator),
318341
IndexeddbEventCacheStoreError,
319342
> {
343+
let _timer = timer!("method");
344+
320345
let linked_chunk_id = linked_chunk_id.to_owned();
321346
let room_id = linked_chunk_id.room_id();
322347
let transaction = self.transaction(
@@ -365,11 +390,14 @@ impl_event_cache_store! {
365390
}
366391
}
367392

393+
#[instrument(skip(self))]
368394
async fn load_previous_chunk(
369395
&self,
370396
linked_chunk_id: LinkedChunkId<'_>,
371397
before_chunk_identifier: ChunkIdentifier,
372398
) -> Result<Option<RawChunk<Event, Gap>>, IndexeddbEventCacheStoreError> {
399+
let _timer = timer!("method");
400+
373401
let linked_chunk_id = linked_chunk_id.to_owned();
374402
let room_id = linked_chunk_id.room_id();
375403
let transaction = self.transaction(
@@ -385,7 +413,10 @@ impl_event_cache_store! {
385413
Ok(None)
386414
}
387415

416+
#[instrument(skip(self))]
388417
async fn clear_all_linked_chunks(&self) -> Result<(), IndexeddbEventCacheStoreError> {
418+
let _timer = timer!("method");
419+
389420
let transaction = self.transaction(
390421
&[keys::LINKED_CHUNKS, keys::EVENTS, keys::GAPS],
391422
IdbTransactionMode::Readwrite,
@@ -397,140 +428,168 @@ impl_event_cache_store! {
397428
Ok(())
398429
}
399430

431+
#[instrument(skip(self, events))]
400432
async fn filter_duplicated_events(
401433
&self,
402434
linked_chunk_id: LinkedChunkId<'_>,
403435
events: Vec<OwnedEventId>,
404436
) -> Result<Vec<(OwnedEventId, Position)>, IndexeddbEventCacheStoreError> {
437+
let _timer = timer!("method");
405438
self.memory_store
406439
.filter_duplicated_events(linked_chunk_id, events)
407440
.await
408441
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
409442
}
410443

444+
#[instrument(skip(self, event_id))]
411445
async fn find_event(
412446
&self,
413447
room_id: &RoomId,
414448
event_id: &EventId,
415449
) -> Result<Option<Event>, IndexeddbEventCacheStoreError> {
450+
let _timer = timer!("method");
416451
self.memory_store
417452
.find_event(room_id, event_id)
418453
.await
419454
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
420455
}
421456

457+
#[instrument(skip(self, event_id, filters))]
422458
async fn find_event_relations(
423459
&self,
424460
room_id: &RoomId,
425461
event_id: &EventId,
426462
filters: Option<&[RelationType]>,
427463
) -> Result<Vec<(Event, Option<Position>)>, IndexeddbEventCacheStoreError> {
464+
let _timer = timer!("method");
428465
self.memory_store
429466
.find_event_relations(room_id, event_id, filters)
430467
.await
431468
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
432469
}
433470

471+
#[instrument(skip(self, event))]
434472
async fn save_event(
435473
&self,
436474
room_id: &RoomId,
437475
event: Event,
438476
) -> Result<(), IndexeddbEventCacheStoreError> {
477+
let _timer = timer!("method");
439478
self.memory_store
440479
.save_event(room_id, event)
441480
.await
442481
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
443482
}
444483

484+
#[instrument(skip_all)]
445485
async fn add_media_content(
446486
&self,
447487
request: &MediaRequestParameters,
448488
content: Vec<u8>,
449489
ignore_policy: IgnoreMediaRetentionPolicy,
450490
) -> Result<(), IndexeddbEventCacheStoreError> {
491+
let _timer = timer!("method");
451492
self.memory_store
452493
.add_media_content(request, content, ignore_policy)
453494
.await
454495
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
455496
}
456497

498+
#[instrument(skip_all)]
457499
async fn replace_media_key(
458500
&self,
459501
from: &MediaRequestParameters,
460502
to: &MediaRequestParameters,
461503
) -> Result<(), IndexeddbEventCacheStoreError> {
504+
let _timer = timer!("method");
462505
self.memory_store
463506
.replace_media_key(from, to)
464507
.await
465508
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
466509
}
467510

511+
#[instrument(skip_all)]
468512
async fn get_media_content(
469513
&self,
470514
request: &MediaRequestParameters,
471515
) -> Result<Option<Vec<u8>>, IndexeddbEventCacheStoreError> {
516+
let _timer = timer!("method");
472517
self.memory_store
473518
.get_media_content(request)
474519
.await
475520
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
476521
}
477522

523+
#[instrument(skip_all)]
478524
async fn remove_media_content(
479525
&self,
480526
request: &MediaRequestParameters,
481527
) -> Result<(), IndexeddbEventCacheStoreError> {
528+
let _timer = timer!("method");
482529
self.memory_store
483530
.remove_media_content(request)
484531
.await
485532
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
486533
}
487534

535+
#[instrument(skip(self))]
488536
async fn get_media_content_for_uri(
489537
&self,
490538
uri: &MxcUri,
491539
) -> Result<Option<Vec<u8>>, IndexeddbEventCacheStoreError> {
540+
let _timer = timer!("method");
492541
self.memory_store
493542
.get_media_content_for_uri(uri)
494543
.await
495544
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
496545
}
497546

547+
#[instrument(skip(self))]
498548
async fn remove_media_content_for_uri(
499549
&self,
500550
uri: &MxcUri,
501551
) -> Result<(), IndexeddbEventCacheStoreError> {
552+
let _timer = timer!("method");
502553
self.memory_store
503554
.remove_media_content_for_uri(uri)
504555
.await
505556
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
506557
}
507558

559+
#[instrument(skip_all)]
508560
async fn set_media_retention_policy(
509561
&self,
510562
policy: MediaRetentionPolicy,
511563
) -> Result<(), IndexeddbEventCacheStoreError> {
564+
let _timer = timer!("method");
512565
self.memory_store
513566
.set_media_retention_policy(policy)
514567
.await
515568
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
516569
}
517570

571+
#[instrument(skip_all)]
518572
fn media_retention_policy(&self) -> MediaRetentionPolicy {
573+
let _timer = timer!("method");
519574
self.memory_store.media_retention_policy()
520575
}
521576

577+
#[instrument(skip_all)]
522578
async fn set_ignore_media_retention_policy(
523579
&self,
524580
request: &MediaRequestParameters,
525581
ignore_policy: IgnoreMediaRetentionPolicy,
526582
) -> Result<(), IndexeddbEventCacheStoreError> {
583+
let _timer = timer!("method");
527584
self.memory_store
528585
.set_ignore_media_retention_policy(request, ignore_policy)
529586
.await
530587
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
531588
}
532589

590+
#[instrument(skip_all)]
533591
async fn clean_up_media_cache(&self) -> Result<(), IndexeddbEventCacheStoreError> {
592+
let _timer = timer!("method");
534593
self.memory_store
535594
.clean_up_media_cache()
536595
.await

0 commit comments

Comments
 (0)