aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/fi/fish-patched/patches/0009-history-turtle-Do-more-work-in-the-command-handler-w.patch
blob: 9e5fbecc2762c023c3ab8d1cb6435b40b09a77e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
From b316b4952d46ad964aa8397f9a3e57de8ba98b3b Mon Sep 17 00:00:00 2001
From: Benedikt Peetz <benedikt.peetz@b-peetz.de>
Date: Mon, 24 Aug 2026 21:43:45 +0200
Subject: [PATCH 09/10] history/turtle: Do more work in the command handler,
 when loading history

---
 src/history/turtle.rs | 94 +++++++++++++++++++++++++++++--------------
 1 file changed, 63 insertions(+), 31 deletions(-)

diff --git a/src/history/turtle.rs b/src/history/turtle.rs
index 4ac931a19..15515fe15 100644
--- a/src/history/turtle.rs
+++ b/src/history/turtle.rs
@@ -5,7 +5,7 @@
         atomic::{AtomicBool, Ordering},
         mpsc,
     },
-    thread::{self, JoinHandle, Thread},
+    thread::JoinHandle,
     time::{Duration, SystemTime, UNIX_EPOCH},
 };
 
@@ -19,30 +19,40 @@
 
 pub(crate) struct HistoryDb;
 
+#[derive(Debug, Clone)]
+struct LoadedHistory {
+    map: HashMap<HistoryId, HistoryItem>,
+
+    /// This field is effectively the same as `map.keys().collect()`.
+    ///
+    /// It just a cache.
+    keys: Vec<HistoryId>,
+}
+
 #[derive(Debug)]
 struct HistoryDbInner {
     range: Option<Range>,
     session: uuid::Uuid,
 
-    loaded_history: Vec<History>,
+    loaded_history: LoadedHistory,
 
     handler: Handler,
 }
 
 impl HistoryDbInner {
-    fn loaded_history(&mut self) -> &[History] {
+    fn loaded_history(&mut self) -> &LoadedHistory {
         if NEW_LOADED_HISTORY_AVAILABLE.load(Ordering::Relaxed) {
             if let Some(pre_loaded_history) = self.handler.load_history_resp() {
                 self.loaded_history = pre_loaded_history;
 
                 // PERFORMANCE: That seems to improve performance? <2026-08-24>
-                self.loaded_history.reverse();
+                self.loaded_history.keys.reverse();
             }
 
             flogf!(
                 history,
                 "Loaded history was requested, returning %d entries.",
-                self.loaded_history.len()
+                self.loaded_history.map.len()
             );
 
             NEW_LOADED_HISTORY_AVAILABLE.store(false, Ordering::Relaxed);
@@ -73,14 +83,7 @@ pub(super) fn create_empty() -> Self {
 
     /// Return the offsets of items in this file.
     pub(super) fn offsets(&self) -> Vec<HistoryId> {
-        let out: Vec<_> = Self::with_inner_mut(|inner| {
-            inner
-                .loaded_history()
-                .iter()
-                .map(|h| h.id.clone())
-                .collect()
-        });
-        out
+        Self::with_inner_mut(|inner| inner.loaded_history().keys.clone())
     }
 
     /// Return whether this file is empty.
@@ -111,7 +114,10 @@ pub(super) fn load(history_path: &WString, _boundary_timestamp: SystemTime) -> S
             let inner = HistoryDbInner {
                 session: uuid::Uuid::now_v7(),
                 range,
-                loaded_history: vec![],
+                loaded_history: LoadedHistory {
+                    map: HashMap::new(),
+                    keys: vec![],
+                },
                 handler,
             };
 
@@ -128,20 +134,7 @@ pub(super) fn load(history_path: &WString, _boundary_timestamp: SystemTime) -> S
 
     /// Decode an item at a given offset.
     pub(super) fn decode_item(&self, id: HistoryId) -> Option<HistoryItem> {
-        Self::with_inner_mut(|inner| {
-            inner.loaded_history().iter().find(|h| h.id == id).map(|h| {
-                HistoryItem::new(
-                    WString::from_str(&h.command),
-                    super::Timestamps {
-                        last_added: UNIX_EPOCH
-                            + Duration::from_nanos_u128(h.timestamp.unix_timestamp_nanos() as u128),
-                        first_added: UNIX_EPOCH
-                            + Duration::from_nanos_u128(h.timestamp.unix_timestamp_nanos() as u128),
-                    },
-                    super::PersistenceMode::Disk,
-                )
-            })
-        })
+        Self::with_inner_mut(|inner| inner.loaded_history().map.get(&id).map(ToOwned::to_owned))
     }
 }
 
@@ -209,7 +202,7 @@ struct Handler {
 
     cmd_tx: Option<mpsc::Sender<HandleHistoryCmd>>,
 
-    returned_loaded_history: Arc<RwLock<Option<Vec<History>>>>,
+    returned_loaded_history: Arc<RwLock<Option<LoadedHistory>>>,
 }
 
 #[derive(Debug)]
@@ -311,8 +304,47 @@ fn start(daemon_socket: String) -> Self {
                                 base.expect("the client to still work")
                             };
 
+                            let mut loaded_history_map = HashMap::new();
+
+                            for item in loaded_history {
+                                loaded_history_map.insert(item.id, {
+                                    HistoryItem::new(
+                                        WString::from_str(&item.command),
+                                        super::Timestamps {
+                                            last_added: UNIX_EPOCH
+                                                + Duration::from_nanos_u128(
+                                                    item.timestamp.unix_timestamp_nanos() as u128,
+                                                ),
+                                            first_added: UNIX_EPOCH
+                                                + Duration::from_nanos_u128(
+                                                    item.timestamp.unix_timestamp_nanos() as u128,
+                                                ),
+                                        },
+                                        super::PersistenceMode::Disk,
+                                    )
+                                });
+                            }
+
+                            let keys = {
+                                let mut base: Vec<_> = loaded_history_map
+                                    .iter()
+                                    .map(|(key, h)| (key, h.first_added_timestamp()))
+                                    .collect();
+
+                                base.sort_by_key(|(_, time)| *time);
+
+                                // reverse this first here, so we can re-reverse it in the
+                                // `loaded_history` function.
+                                base.reverse();
+
+                                base.into_iter().map(|(key, _)| key).copied().collect()
+                            };
+
                             let mut output = loaded_history_return.write().expect("not poisioned");
-                            (*output) = Some(loaded_history);
+                            (*output) = Some(LoadedHistory {
+                                keys,
+                                map: loaded_history_map,
+                            });
                             NEW_LOADED_HISTORY_AVAILABLE.store(true, Ordering::Relaxed);
                         }
                     }
@@ -343,7 +375,7 @@ fn stop(&mut self) {
         flog!(history, "History db shutdown completed.");
     }
 
-    fn load_history_resp(&self) -> Option<Vec<History>> {
+    fn load_history_resp(&self) -> Option<LoadedHistory> {
         let mut output = None;
         let rx = &self.returned_loaded_history;
 
-- 
2.55.0