-
-
Notifications
You must be signed in to change notification settings - Fork 103
feat: Mark reactions as IMAP-seen in marknoticed_chat(), second try #6480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
6bbe9e7
3ef1787
8fc7eab
50ddc7c
359b208
b93635d
23f0dbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#![recursion_limit = "256"] | ||
use std::path::Path; | ||
|
||
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; | ||
use deltachat::chat::{self, ChatId}; | ||
use deltachat::chatlist::Chatlist; | ||
use deltachat::context::Context; | ||
use deltachat::stock_str::StockStrings; | ||
use deltachat::Events; | ||
use futures_lite::future::block_on; | ||
use tempfile::tempdir; | ||
|
||
async fn marknoticed_chat_benchmark(context: &Context, chats: &[ChatId]) { | ||
for c in chats.iter().take(20) { | ||
chat::marknoticed_chat(context, *c).await.unwrap(); | ||
} | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
// To enable this benchmark, set `DELTACHAT_BENCHMARK_DATABASE` to some large database with many | ||
// messages, such as your primary account. | ||
if let Ok(path) = std::env::var("DELTACHAT_BENCHMARK_DATABASE") { | ||
let rt = tokio::runtime::Runtime::new().unwrap(); | ||
|
||
let chats: Vec<_> = rt.block_on(async { | ||
let context = Context::new(Path::new(&path), 100, Events::new(), StockStrings::new()) | ||
.await | ||
.unwrap(); | ||
let chatlist = Chatlist::try_load(&context, 0, None, None).await.unwrap(); | ||
let len = chatlist.len(); | ||
(1..len).map(|i| chatlist.get_chat_id(i).unwrap()).collect() | ||
}); | ||
|
||
// This mainly tests the performance of marknoticed_chat() | ||
// when nothing has to be done | ||
c.bench_function( | ||
"chat::marknoticed_chat (mark 20 chats as noticed repeatedly)", | ||
|b| { | ||
let dir = tempdir().unwrap(); | ||
let dir = dir.path(); | ||
let new_db = dir.join("dc.db"); | ||
std::fs::copy(&path, &new_db).unwrap(); | ||
|
||
let context = block_on(async { | ||
Context::new(Path::new(&new_db), 100, Events::new(), StockStrings::new()) | ||
.await | ||
.unwrap() | ||
}); | ||
|
||
b.to_async(&rt) | ||
.iter(|| marknoticed_chat_benchmark(&context, black_box(&chats))) | ||
}, | ||
); | ||
|
||
// If the first 20 chats contain fresh messages or reactions, | ||
// this tests the performance of marking them as noticed. | ||
c.bench_function( | ||
"chat::marknoticed_chat (mark 20 chats as noticed, resetting after every iteration)", | ||
|b| { | ||
b.to_async(&rt).iter_batched( | ||
|| { | ||
let dir = tempdir().unwrap(); | ||
let new_db = dir.path().join("dc.db"); | ||
std::fs::copy(&path, &new_db).unwrap(); | ||
|
||
let context = block_on(async { | ||
Context::new( | ||
Path::new(&new_db), | ||
100, | ||
Events::new(), | ||
StockStrings::new(), | ||
) | ||
.await | ||
.unwrap() | ||
}); | ||
(dir, context) | ||
}, | ||
|(_dir, context)| { | ||
let chats = &chats; | ||
async move { | ||
marknoticed_chat_benchmark(black_box(&context), black_box(chats)).await | ||
} | ||
}, | ||
BatchSize::PerIteration, | ||
); | ||
}, | ||
); | ||
} else { | ||
println!("env var not set: DELTACHAT_BENCHMARK_DATABASE"); | ||
} | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ use tokio::task; | |
use crate::aheader::EncryptPreference; | ||
use crate::blob::BlobObject; | ||
use crate::chatlist::Chatlist; | ||
use crate::chatlist_events; | ||
use crate::color::str_to_color; | ||
use crate::config::Config; | ||
use crate::constants::{ | ||
|
@@ -51,6 +50,7 @@ use crate::tools::{ | |
truncate_msg_text, IsNoneOrEmpty, SystemTime, | ||
}; | ||
use crate::webxdc::StatusUpdateSerial; | ||
use crate::{chatlist_events, imap}; | ||
|
||
/// An chat item, such as a message or a marker. | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
|
@@ -3328,7 +3328,7 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()> | |
} else { | ||
start_chat_ephemeral_timers(context, chat_id).await?; | ||
|
||
if context | ||
let noticed_msgs_count = context | ||
.sql | ||
.execute( | ||
"UPDATE msgs | ||
|
@@ -3338,9 +3338,36 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()> | |
AND chat_id=?;", | ||
(MessageState::InNoticed, MessageState::InFresh, chat_id), | ||
) | ||
.await? | ||
== 0 | ||
{ | ||
.await?; | ||
|
||
// This is to trigger emitting `MsgsNoticed` on other devices when reactions are noticed | ||
// locally (i.e. when the chat was opened locally). | ||
let hidden_messages = context | ||
.sql | ||
.query_map( | ||
"SELECT id, rfc724_mid FROM msgs | ||
WHERE state=? | ||
AND hidden=1 | ||
AND chat_id=? | ||
ORDER BY id DESC LIMIT 100", // LIMIT to 100 in order to avoid blocking the UI too long, usually there will be less than 100 messages anyway | ||
(MessageState::InFresh, chat_id), // No need to check for InNoticed messages, because reactions are never InNoticed | ||
|row| { | ||
let msg_id: MsgId = row.get(0)?; | ||
let rfc724_mid: String = row.get(1)?; | ||
Ok((msg_id, rfc724_mid)) | ||
}, | ||
|rows| { | ||
rows.collect::<std::result::Result<Vec<_>, _>>() | ||
.map_err(Into::into) | ||
}, | ||
) | ||
.await?; | ||
for (msg_id, rfc724_mid) in &hidden_messages { | ||
message::update_msg_state(context, *msg_id, MessageState::InSeen).await?; | ||
imap::markseen_on_imap_table(context, rfc724_mid).await?; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance matters here because this is done in the "hot path" of the user opening a chat. This could be replaced by: context
.sql
.transaction(|trans| {
trans.execute(
"UPDATE msgs SET state=? WHERE state=? AND hidden=1 AND chat_id=?",
(MessageState::InSeen, MessageState::InNoticed, chat_id),
)?;
trans.execute(
"INSERT OR IGNORE INTO imap_markseen (id)
SELECT id FROM imap WHERE rfc724_mid IN
(SELECT rfc724_mid FROM msgs WHERE state=? AND hidden=1 AND chat_id=?)",
(MessageState::InNoticed, chat_id),
)?;
Ok(())
})
.await?;
context.scheduler.interrupt_inbox().await; However, in my benchmark, this actually has worse performance: Probably because the messages have to be loaded twice now. Also, this always opens a write connection, not only when messages actually have to be marked as noticed. If you know of a possibly more performant version of this, tell me & I'll benchmark it. But the most important part is the Maybe I'll make |
||
|
||
if noticed_msgs_count == 0 { | ||
return Ok(()); | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.