Skip to content

Commit

Permalink
dont since-optimize when we don't have enough notes
Browse files Browse the repository at this point in the history
If our limit says we're ok with many more notes than we have, then don't
since-optimize, otherwise we may be missing notes. This results in a

Changelog-Changed: Don't since-optimize if we don't have enough notes
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed May 31, 2024
1 parent 2d9f456 commit 0eec8c8
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ fn relay_setup(pool: &mut RelayPool, ctx: &egui::Context) {
}
}

/// Should we since optimize? Not always. For examplem if we only have a few
/// notes locally. One way to determine this is by looking at the current filter
/// and seeing what its limit is. If we have less notes than the limit,
/// we might want to backfill older notes
fn should_since_optimize(limit: Option<u16>, num_notes: usize) -> bool {
let limit = limit.unwrap_or(enostr::Filter::default_limit()) as usize;

// rough heuristic for bailing since optimization if we don't have enough notes
limit <= num_notes
}

fn since_optimize_filter(filter: &mut enostr::Filter, notes: &[NoteRef]) {
// Get the latest entry in the events
if notes.is_empty() {
Expand All @@ -104,14 +115,19 @@ fn send_initial_filters(damus: &mut Damus, relay_url: &str) {
for timeline in &damus.timelines {
let mut filter = timeline.filter.clone();
for f in &mut filter {
since_optimize_filter(f, timeline.notes(ViewFilter::NotesAndReplies));

// limit the size of remote filters
let default_limit = enostr::Filter::default_remote_limit();
let lim = f.limit.unwrap_or(default_limit);
if lim > default_limit {
f.limit = Some(default_limit);
}

let notes = timeline.notes(ViewFilter::NotesAndReplies);
if should_since_optimize(f.limit, notes.len()) {
since_optimize_filter(f, notes);
} else {
warn!("Skipping since optimization for {:?}: number of local notes is less than limit, attempting to backfill.", f);
}
}
relay.subscribe(format!("initial{}", c), filter);
c += 1;
Expand Down

0 comments on commit 0eec8c8

Please sign in to comment.