Skip to content

Commit b22bb3e

Browse files
committed
fix(sqlite): Use a prepared statement to insert events.
This patch uses a prepared statement to insert events in the linked chunks. It offers more predictable performance, and SQLite prefers that.
1 parent 7f17b4b commit b22bb3e

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

crates/matrix-sdk-sqlite/src/event_cache_store.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -486,24 +486,27 @@ impl EventCacheStore for SqliteEventCacheStore {
486486
}
487487

488488
Update::PushItems { at, items } => {
489+
if items.is_empty() {
490+
// Should never happens, but better be safe.
491+
continue;
492+
}
493+
489494
let chunk_id = at.chunk_identifier().index();
490495

491496
trace!(%room_id, "pushing {} items @ {chunk_id}", items.len());
492497

498+
let mut statement = txn.prepare(
499+
"INSERT INTO events(chunk_id, room_id, event_id, content, position) VALUES (?, ?, ?, ?, ?)"
500+
)?;
501+
493502
for (i, event) in items.into_iter().enumerate() {
494503
let serialized = serde_json::to_vec(&event)?;
495504
let content = this.encode_value(serialized)?;
496505

497506
let event_id = event.event_id().map(|event_id| event_id.to_string());
498507
let index = at.index() + i;
499508

500-
txn.execute(
501-
r#"
502-
INSERT INTO events(chunk_id, room_id, event_id, content, position)
503-
VALUES (?, ?, ?, ?, ?)
504-
"#,
505-
(chunk_id, &hashed_room_id, event_id, content, index),
506-
)?;
509+
statement.execute((chunk_id, &hashed_room_id, event_id, content, index))?;
507510
}
508511
}
509512

0 commit comments

Comments
 (0)