Skip to content
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

fix(event cache): give looser parameters for the deduplicator's bloom filters #4231

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions crates/matrix-sdk/src/event_cache/deduplicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ impl fmt::Debug for Deduplicator {
}

impl Deduplicator {
const APPROXIMATED_MAXIMUM_NUMBER_OF_EVENTS: usize = 800_000;
const DESIRED_FALSE_POSITIVE_RATE: f64 = 0.001;
// Note: don't use too high numbers here, or the amount of allocated memory will
// explode. See https://github.com/matrix-org/matrix-rust-sdk/pull/4231 for details.
const APPROXIMATED_MAXIMUM_NUMBER_OF_EVENTS: usize = 1_000;
poljar marked this conversation as resolved.
Show resolved Hide resolved
const DESIRED_FALSE_POSITIVE_RATE: f64 = 0.01;

/// Create a new `Deduplicator`.
pub fn new() -> Self {
Expand Down Expand Up @@ -138,7 +140,7 @@ pub enum Decoration<I> {

#[cfg(test)]
mod tests {
use assert_matches2::assert_let;
use assert_matches2::{assert_let, assert_matches};
use matrix_sdk_base::deserialized_responses::SyncTimelineEvent;
use ruma::{owned_event_id, user_id, EventId};

Expand Down Expand Up @@ -267,4 +269,40 @@ mod tests {
assert!(events.next().is_none());
}
}

#[test]
fn test_bloom_filter_growth() {
// This test was used as a testbed to observe, using `valgrind --tool=massive`,
// the total memory allocated by the deduplicator. We keep it checked in
// to revive this experiment in the future, if needs be.

let num_rooms = if let Ok(num_rooms) = std::env::var("ROOMS") {
num_rooms.parse().unwrap()
} else {
10
};

let num_events = if let Ok(num_events) = std::env::var("EVENTS") {
num_events.parse().unwrap()
} else {
100
};

let mut dedups = Vec::with_capacity(num_rooms);
jmartinesp marked this conversation as resolved.
Show resolved Hide resolved

for _ in 0..num_rooms {
let dedup = Deduplicator::new();
let existing_events = RoomEvents::new();

for i in 0..num_events {
let event = sync_timeline_event(&EventId::parse(format!("$event{i}")).unwrap());
let mut it = dedup.scan_and_learn([event].into_iter(), &existing_events);

assert_matches!(it.next(), Some(Decoration::Unique(..)));
assert_matches!(it.next(), None);
}

dedups.push(dedup);
}
}
}
Loading