Skip to content

Commit

Permalink
use egui_virtual_list for rendering
Browse files Browse the repository at this point in the history
absolutely insane performance increase

Fixes: #32
Suggested-by: @lucasmerlin
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Apr 28, 2024
1 parent d0cfeee commit 26128c3
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 102 deletions.
31 changes: 26 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ nostr-sdk = "0.29.0"
strum = "0.26"
strum_macros = "0.26"
bitflags = "2.5.0"
egui_virtual_list = "0.3.0"


[features]
Expand Down
2 changes: 1 addition & 1 deletion queries/hashtags.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[{"limit": 100,
[{"limit": 1000,
"kinds": [
1
],
Expand Down
2 changes: 1 addition & 1 deletion queries/notifications.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"limit": 100, "kinds":[1], "#p": ["32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"]}]
[{"limit": 1000, "kinds":[1], "#p": ["32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"]}]
101 changes: 6 additions & 95 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ use crate::frame_history::FrameHistory;
use crate::imgcache::ImageCache;
use crate::notecache::NoteCache;
use crate::timeline;
use crate::ui;
use crate::timeline::{NoteRef, Timeline};
use crate::ui::is_mobile;
use crate::Result;

use egui::containers::scroll_area::ScrollBarVisibility;
use egui::{Context, Frame, Margin, Style};
use egui_extras::{Size, StripBuilder};

use enostr::{ClientMessage, Filter, Pubkey, RelayEvent, RelayMessage};
use nostrdb::{BlockType, Config, Mention, Ndb, Note, NoteKey, Subscription, Transaction};
use nostrdb::{BlockType, Config, Mention, Ndb, Note, NoteKey, Transaction};

use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::path::Path;
Expand All @@ -31,47 +29,6 @@ pub enum DamusState {
Initialized,
}

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct NoteRef {
pub key: NoteKey,
pub created_at: u64,
}

impl Ord for NoteRef {
fn cmp(&self, other: &Self) -> Ordering {
match self.created_at.cmp(&other.created_at) {
Ordering::Equal => self.key.cmp(&other.key),
Ordering::Less => Ordering::Greater,
Ordering::Greater => Ordering::Less,
}
}
}

impl PartialOrd for NoteRef {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

struct Timeline {
pub filter: Vec<Filter>,
pub notes: Vec<NoteRef>,
pub subscription: Option<Subscription>,
}

impl Timeline {
pub fn new(filter: Vec<Filter>) -> Self {
let notes: Vec<NoteRef> = Vec::with_capacity(1000);
let subscription: Option<Subscription> = None;

Timeline {
filter,
notes,
subscription,
}
}
}

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
pub struct Damus {
state: DamusState,
Expand All @@ -80,7 +37,7 @@ pub struct Damus {
pool: RelayPool,
pub textmode: bool,

timelines: Vec<Timeline>,
pub timelines: Vec<Timeline>,

pub img_cache: ImageCache,
pub ndb: Ndb,
Expand Down Expand Up @@ -557,52 +514,6 @@ fn circle_icon(ui: &mut egui::Ui, openness: f32, response: &egui::Response) {
}
*/

fn render_notes(ui: &mut egui::Ui, damus: &mut Damus, timeline: usize) -> Result<()> {
#[cfg(feature = "profiling")]
puffin::profile_function!();

let num_notes = damus.timelines[timeline].notes.len();
let txn = Transaction::new(&damus.ndb)?;

for i in 0..num_notes {
let note_key = damus.timelines[timeline].notes[i].key;
let note = if let Ok(note) = damus.ndb.get_note_by_key(&txn, note_key) {
note
} else {
warn!("failed to query note {:?}", note_key);
continue;
};

let note_ui = ui::Note::new(damus, &note);
ui.add(note_ui);
ui.add(egui::Separator::default().spacing(0.0));
}

Ok(())
}

fn timeline_view(ui: &mut egui::Ui, app: &mut Damus, timeline: usize) {
//padding(4.0, ui, |ui| ui.heading("Notifications"));
/*
let font_id = egui::TextStyle::Body.resolve(ui.style());
let row_height = ui.fonts(|f| f.row_height(&font_id)) + ui.spacing().item_spacing.y;
*/

egui::ScrollArea::vertical()
.scroll_bar_visibility(ScrollBarVisibility::AlwaysVisible)
//.auto_shrink([false; 2])
/*
.show_viewport(ui, |ui, viewport| {
render_notes_in_viewport(ui, app, viewport, row_height, font_id);
});
*/
.show(ui, |ui| {
ui.spacing_mut().item_spacing.y = 0.0;
ui.spacing_mut().item_spacing.x = 4.0;
let _ = render_notes(ui, app, timeline);
});
}

fn top_panel(ctx: &egui::Context) -> egui::TopBottomPanel {
let top_margin = egui::Margin {
top: 4.0,
Expand Down Expand Up @@ -684,7 +595,7 @@ fn render_damus_mobile(ctx: &egui::Context, app: &mut Damus) {
puffin::profile_function!();

main_panel(&ctx.style()).show(ctx, |ui| {
timeline_view(ui, app, 0);
timeline::timeline_view(ui, app, 0);
});
}

Expand Down Expand Up @@ -713,7 +624,7 @@ fn render_damus_desktop(ctx: &egui::Context, app: &mut Damus) {

if app.timelines.len() == 1 {
main_panel(&ctx.style()).show(ctx, |ui| {
timeline_view(ui, app, 0);
timeline::timeline_view(ui, app, 0);
});

return;
Expand All @@ -737,7 +648,7 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus, timelines: us
.clip(true)
.horizontal(|mut strip| {
for timeline_ind in 0..timelines {
strip.cell(|ui| timeline_view(ui, app, timeline_ind));
strip.cell(|ui| timeline::timeline_view(ui, app, timeline_ind));
}
});
}
Expand Down
107 changes: 107 additions & 0 deletions src/timeline.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,110 @@
use crate::{ui, Damus};
use egui::containers::scroll_area::ScrollBarVisibility;
use egui_virtual_list::VirtualList;
use enostr::Filter;
use nostrdb::{NoteKey, Subscription, Transaction};
use std::cmp::Ordering;
use std::sync::{Arc, Mutex};

use log::warn;

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct NoteRef {
pub key: NoteKey,
pub created_at: u64,
}

impl Ord for NoteRef {
fn cmp(&self, other: &Self) -> Ordering {
match self.created_at.cmp(&other.created_at) {
Ordering::Equal => self.key.cmp(&other.key),
Ordering::Less => Ordering::Greater,
Ordering::Greater => Ordering::Less,
}
}
}

impl PartialOrd for NoteRef {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

pub struct Timeline {
pub filter: Vec<Filter>,
pub notes: Vec<NoteRef>,

/// Our nostrdb subscription
pub subscription: Option<Subscription>,

/// State for our virtual list egui widget
pub list: Arc<Mutex<VirtualList>>,
}

impl Timeline {
pub fn new(filter: Vec<Filter>) -> Self {
let notes: Vec<NoteRef> = Vec::with_capacity(1000);
let subscription: Option<Subscription> = None;
let list = Arc::new(Mutex::new(VirtualList::new()));

Timeline {
filter,
notes,
subscription,
list,
}
}
}

pub fn timeline_view(ui: &mut egui::Ui, app: &mut Damus, timeline: usize) {
//padding(4.0, ui, |ui| ui.heading("Notifications"));
/*
let font_id = egui::TextStyle::Body.resolve(ui.style());
let row_height = ui.fonts(|f| f.row_height(&font_id)) + ui.spacing().item_spacing.y;
*/

egui::ScrollArea::vertical()
.scroll_bar_visibility(ScrollBarVisibility::AlwaysVisible)
//.auto_shrink([false; 2])
/*
.show_viewport(ui, |ui, viewport| {
render_notes_in_viewport(ui, app, viewport, row_height, font_id);
});
*/
.show(ui, |ui| {
let len = app.timelines[timeline].notes.len();
let list = app.timelines[timeline].list.clone();
list.lock()
.unwrap()
.ui_custom_layout(ui, len, |ui, start_index| {
ui.spacing_mut().item_spacing.y = 0.0;
ui.spacing_mut().item_spacing.x = 4.0;

let note_key = app.timelines[timeline].notes[start_index].key;

let txn = if let Ok(txn) = Transaction::new(&app.ndb) {
txn
} else {
warn!("failed to create transaction for {:?}", note_key);
return 0;
};

let note = if let Ok(note) = app.ndb.get_note_by_key(&txn, note_key) {
note
} else {
warn!("failed to query note {:?}", note_key);
return 0;
};

let note_ui = ui::Note::new(app, &note);
ui.add(note_ui);
ui.add(egui::Separator::default().spacing(0.0));

1
});
});
}

pub fn merge_sorted_vecs<T: Ord + Copy>(vec1: &[T], vec2: &[T]) -> Vec<T> {
let mut merged = Vec::with_capacity(vec1.len() + vec2.len());
let mut i = 0;
Expand Down

0 comments on commit 26128c3

Please sign in to comment.