Przeglądaj źródła

bin/dnetview: use fxhash for HashMap and HashSet

ghassmo 4 lat temu
rodzic
commit
75d01fb0cb

+ 1 - 0
Cargo.lock

@@ -1854,6 +1854,7 @@ dependencies = [
  "clap 3.1.6",
  "darkfi",
  "easy-parallel",
+ "fxhash",
  "log",
  "num_cpus",
  "rand 0.6.5",

+ 1 - 0
bin/dnetview/Cargo.toml

@@ -25,6 +25,7 @@ simplelog = "0.11.2"
 log = "0.4.14"
 num_cpus = "1.13.1"
 url = "2.2.2"
+fxhash = "0.2.1"
 
 # Encoding and parsing
 serde_json = "1.0.79"

+ 18 - 21
bin/dnetview/src/main.rs

@@ -1,26 +1,13 @@
-use darkfi::{
-    error::{Error, Result},
-    rpc::{jsonrpc, jsonrpc::JsonResult},
-    util::{
-        async_util,
-        cli::{log_config, spawn_config, Config},
-        join_config_path,
-    },
-};
-
 use async_std::sync::Arc;
+use std::{fs::File, io, io::Read, path::PathBuf};
+
 use easy_parallel::Parallel;
+use fxhash::{FxHashMap, FxHashSet};
 use log::{debug, info};
 use serde_json::{json, Value};
 use simplelog::*;
 use smol::Executor;
-use std::{
-    collections::{HashMap, HashSet},
-    fs::File,
-    io,
-    io::Read,
-    path::PathBuf,
-};
+
 use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
 use tui::{
     backend::{Backend, TermionBackend},
@@ -28,6 +15,16 @@ use tui::{
 };
 use url::Url;
 
+use darkfi::{
+    error::{Error, Result},
+    rpc::{jsonrpc, jsonrpc::JsonResult},
+    util::{
+        async_util,
+        cli::{log_config, spawn_config, Config},
+        join_config_path,
+    },
+};
+
 use dnetview::{
     config::{DnvConfig, CONFIG_FILE_CONTENTS},
     model::{Channel, IdList, InboundInfo, InfoList, ManualInfo, NodeInfo, OutboundInfo, Slot},
@@ -110,7 +107,7 @@ async fn main() -> Result<()> {
     terminal.clear()?;
 
     let info_list = InfoList::new();
-    let ids = HashSet::new();
+    let ids = FxHashSet::default();
     let id_list = IdList::new(ids);
 
     let model = Arc::new(Model::new(id_list, info_list));
@@ -231,7 +228,7 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
             let oinfo = OutboundInfo::new(is_empty, slots.clone());
             oconnects.push(oinfo);
             let infos = NodeInfo { outbound: oconnects, manual: mconnects, inbound: iconnects };
-            let mut node_info = HashMap::new();
+            let mut node_info = FxHashMap::default();
 
             // TODO: here we are setting the RPC url as the node_id.
             // next step is to read the string variable 'name' from dnetview.toml
@@ -259,8 +256,8 @@ async fn render<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> io
 
     terminal.clear()?;
 
-    let id_list = IdListView::new(HashSet::new());
-    let info_list = InfoListView::new(HashMap::new());
+    let id_list = IdListView::new(FxHashSet::default());
+    let info_list = InfoListView::new(FxHashMap::default());
     let mut view = View::new(id_list.clone(), info_list.clone());
 
     view.id_list.state.select(Some(0));

+ 6 - 5
bin/dnetview/src/model.rs

@@ -1,6 +1,7 @@
 use async_std::sync::Mutex;
+
+use fxhash::{FxHashMap, FxHashSet};
 use serde::Deserialize;
-use std::collections::{HashMap, HashSet};
 use tui::widgets::ListState;
 
 pub struct Model {
@@ -16,11 +17,11 @@ impl Model {
 
 pub struct IdList {
     pub state: Mutex<ListState>,
-    pub node_id: Mutex<HashSet<String>>,
+    pub node_id: Mutex<FxHashSet<String>>,
 }
 
 impl IdList {
-    pub fn new(node_id: HashSet<String>) -> IdList {
+    pub fn new(node_id: FxHashSet<String>) -> IdList {
         let node_id = Mutex::new(node_id);
         IdList { state: Mutex::new(ListState::default()), node_id }
     }
@@ -28,14 +29,14 @@ impl IdList {
 
 pub struct InfoList {
     pub index: Mutex<usize>,
-    pub infos: Mutex<HashMap<String, NodeInfo>>,
+    pub infos: Mutex<FxHashMap<String, NodeInfo>>,
 }
 
 impl InfoList {
     pub fn new() -> InfoList {
         let index = 0;
         let index = Mutex::new(index);
-        let infos = Mutex::new(HashMap::new());
+        let infos = Mutex::new(FxHashMap::default());
 
         InfoList { index, infos }
     }

+ 8 - 7
bin/dnetview/src/view.rs

@@ -1,7 +1,8 @@
-use crate::model::NodeInfo;
-use std::collections::{HashMap, HashSet};
+use fxhash::{FxHashMap, FxHashSet};
 use tui::widgets::ListState;
 
+use crate::model::NodeInfo;
+
 #[derive(Clone)]
 pub struct View {
     pub id_list: IdListView,
@@ -13,7 +14,7 @@ impl View {
         View { id_list, info_list }
     }
 
-    pub fn update(&mut self, infos: HashMap<String, NodeInfo>) {
+    pub fn update(&mut self, infos: FxHashMap<String, NodeInfo>) {
         for (id, info) in infos {
             self.id_list.node_id.insert(id.clone());
             self.info_list.infos.insert(id, info);
@@ -24,11 +25,11 @@ impl View {
 #[derive(Clone)]
 pub struct IdListView {
     pub state: ListState,
-    pub node_id: HashSet<String>,
+    pub node_id: FxHashSet<String>,
 }
 
 impl IdListView {
-    pub fn new(node_id: HashSet<String>) -> IdListView {
+    pub fn new(node_id: FxHashSet<String>) -> IdListView {
         IdListView { state: ListState::default(), node_id }
     }
     pub fn next(&mut self) {
@@ -67,11 +68,11 @@ impl IdListView {
 #[derive(Clone)]
 pub struct InfoListView {
     pub index: usize,
-    pub infos: HashMap<String, NodeInfo>,
+    pub infos: FxHashMap<String, NodeInfo>,
 }
 
 impl InfoListView {
-    pub fn new(infos: HashMap<String, NodeInfo>) -> InfoListView {
+    pub fn new(infos: FxHashMap<String, NodeInfo>) -> InfoListView {
         let index = 0;
 
         InfoListView { index, infos }