Skip to content

Commit 8b10a8c

Browse files
committed
Use a static HashMap to speed up mount lookups
1 parent 6e17b06 commit 8b10a8c

3 files changed

Lines changed: 31 additions & 24 deletions

File tree

src/fs/file.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
77

88
use log::*;
99

10-
use proc_mounts::MOUNTS;
11-
10+
use crate::ALL_MOUNTS;
1211
use crate::fs::dir::Dir;
1312
use crate::fs::fields as f;
1413

@@ -212,34 +211,20 @@ impl<'dir> File<'dir> {
212211
self.metadata.file_type().is_socket()
213212
}
214213

215-
// Whether this file is a mount point
214+
/// Whether this file is a mount point
216215
pub fn is_mount_point(&self) -> bool {
217216
if cfg!(unix) {
218217
if self.is_directory() {
219-
let mounts = &MOUNTS.read().unwrap().0;
220-
for mount in mounts {
221-
if self.absolute_path.eq(&mount.dest) {
222-
return true;
223-
}
224-
}
218+
return ALL_MOUNTS.contains_key(&self.absolute_path);
225219
}
226220
}
227221
return false;
228222
}
229223

230-
// The filesystem device and type for a mount point
231-
pub fn mount_point_info(&self) -> Option<MountedFs> {
232-
if self.is_mount_point() {
233-
let mounts = &MOUNTS.read().unwrap().0;
234-
for mount_point in mounts {
235-
if self.absolute_path.eq(&mount_point.dest) {
236-
return Some(MountedFs {
237-
dest: mount_point.dest.to_string_lossy().into_owned(),
238-
fstype: mount_point.fstype.clone(),
239-
source: mount_point.source.to_string_lossy().into_owned(),
240-
})
241-
}
242-
}
224+
/// The filesystem device and type for a mount point
225+
pub fn mount_point_info(&self) -> Option<&MountedFs> {
226+
if cfg!(unix) {
227+
return ALL_MOUNTS.get(&self.absolute_path);
243228
}
244229
None
245230
}

src/main.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@
2323
#![allow(clippy::upper_case_acronyms)]
2424
#![allow(clippy::wildcard_imports)]
2525

26+
#[macro_use]
27+
extern crate lazy_static;
28+
29+
use std::collections::HashMap;
2630
use std::env;
2731
use std::ffi::{OsStr, OsString};
2832
use std::io::{self, Write, ErrorKind};
2933
use std::path::{Component, PathBuf};
3034

3135
use ansi_term::{ANSIStrings, Style};
36+
use proc_mounts::MOUNTS;
3237

3338
use log::*;
3439

35-
use crate::fs::{Dir, File};
40+
use crate::fs::{Dir, File, MountedFs};
3641
use crate::fs::feature::git::GitCache;
3742
use crate::fs::filter::GitIgnore;
3843
use crate::options::{Options, Vars, vars, OptionsResult};
@@ -46,6 +51,23 @@ mod options;
4651
mod output;
4752
mod theme;
4853

54+
lazy_static! {
55+
static ref ALL_MOUNTS: HashMap<PathBuf, MountedFs> = {
56+
let mut m = HashMap::new();
57+
if cfg!(unix) {
58+
for mount_point in &MOUNTS.read().unwrap().0 {
59+
let mount = MountedFs {
60+
dest: mount_point.dest.to_string_lossy().into_owned(),
61+
fstype: mount_point.fstype.clone(),
62+
source: mount_point.source.to_string_lossy().into_owned(),
63+
};
64+
m.insert(mount_point.dest.clone(), mount);
65+
}
66+
}
67+
m
68+
};
69+
}
70+
4971

5072
fn main() {
5173
use std::process::exit;

src/output/file_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub struct FileName<'a, 'dir, C> {
119119
options: Options,
120120

121121
/// The filesystem details for a mounted filesystem.
122-
mounted_fs: Option<MountedFs>,
122+
mounted_fs: Option<&'a MountedFs>,
123123

124124
/// How to handle displaying a mounted filesystem.
125125
mount_style: MountStyle,

0 commit comments

Comments
 (0)