From 11fecd9cf1f15e755fa23fb5b8204b39fec461fd Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 11 Mar 2025 21:10:48 +0200 Subject: [PATCH] replace vector collection with event snap for performance reasons (much detailed history logs not planned) --- .../page/navigation/request/suggestion.rs | 2 +- src/profile/history/item.rs | 34 ++++++++--------- src/profile/history/item/event.rs | 37 +++++++++++++++++++ src/profile/history/memory.rs | 11 ++++-- 4 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 src/profile/history/item/event.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs b/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs index fbb08507..f9c4eef7 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs @@ -161,7 +161,7 @@ impl Suggestion { if !items.is_empty() { for item in items .into_iter() - .sorted_by(|a, b| Ord::cmp(&b.opened.len(), &a.opened.len())) + .sorted_by(|a, b| Ord::cmp(&b.opened.count, &a.opened.count)) { let subtitle = highlight(&item.request, &query); let title = match item.title { diff --git a/src/profile/history/item.rs b/src/profile/history/item.rs index d86d4c05..6eae228f 100644 --- a/src/profile/history/item.rs +++ b/src/profile/history/item.rs @@ -1,4 +1,7 @@ -use gtk::glib::{DateTime, GString}; +mod event; + +use event::Event; +use gtk::glib::GString; #[derive(Clone)] pub struct Item { @@ -9,12 +12,12 @@ pub struct Item { /// Some history items may contain title (e.g. gemtext documents and system tabs) /// * used as the additional criteria for search in the navbar suggestions widget pub title: Option, - /// Collect opened count with event time - /// * used for sort order search results in the navbar suggestions widget - pub opened: Vec, - /// Collect tab closed count with event time - /// * used in recently closed pages menu - pub closed: Vec, + /// Collect `Item` open events + /// * used for sort order search results in the navbar suggestions widget and history page + pub opened: Event, + /// Collect `Item` close events + /// * used in recently closed pages menu and history page + pub closed: Option, } impl Item { @@ -25,24 +28,21 @@ impl Item { id: None, request, title, - opened: vec![now()], - closed: vec![], + opened: Event::new(), + closed: None, } } // Actions pub fn open(&mut self) { - self.opened.push(now()) + self.opened.pulse() } pub fn close(&mut self) { - self.closed.push(now()) + match self.closed { + Some(ref mut closed) => closed.pulse(), + None => self.closed = Some(Event::new()), + } } } - -// Tools - -fn now() -> DateTime { - DateTime::now_local().unwrap() -} diff --git a/src/profile/history/item/event.rs b/src/profile/history/item/event.rs new file mode 100644 index 00000000..14b5f867 --- /dev/null +++ b/src/profile/history/item/event.rs @@ -0,0 +1,37 @@ +use gtk::glib::DateTime; + +#[derive(Clone)] +pub struct Event { + pub time: DateTime, + pub count: usize, +} + +impl Event { + // Constructors + + pub fn new() -> Self { + Self { + time: now(), + count: 0, + } + } + + // Actions + + pub fn pulse(&mut self) { + self.time = now(); + self.count += 1; + } +} + +impl Default for Event { + fn default() -> Self { + Self::new() + } +} + +// Tools + +fn now() -> DateTime { + DateTime::now_local().unwrap() +} diff --git a/src/profile/history/memory.rs b/src/profile/history/memory.rs index 832deace..2edabcf8 100644 --- a/src/profile/history/memory.rs +++ b/src/profile/history/memory.rs @@ -48,8 +48,13 @@ impl Memory { for (i, item) in self .0 .iter() - .filter(|x| !x.closed.is_empty()) - .sorted_by(|a, b| Ord::cmp(&b.closed.last(), &a.closed.last())) + .filter(|x| x.closed.is_some()) + .sorted_by(|a, b| { + Ord::cmp( + &b.closed.as_ref().unwrap().time, + &a.closed.as_ref().unwrap().time, + ) + }) .enumerate() { if limit.is_some_and(|l| i > l) { @@ -66,7 +71,7 @@ impl Memory { for (i, item) in self .0 .iter() - .sorted_by(|a, b| Ord::cmp(&b.opened.last(), &a.opened.last())) + .sorted_by(|a, b| Ord::cmp(&b.opened.time, &a.opened.time)) .enumerate() { if limit.is_some_and(|l| i > l) {