Skip to content

Commit

Permalink
Use a static HashMap to speed up mount lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
daviessm committed Aug 25, 2021
1 parent 6e17b06 commit 8b10a8c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
29 changes: 7 additions & 22 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};

use log::*;

use proc_mounts::MOUNTS;

use crate::ALL_MOUNTS;
use crate::fs::dir::Dir;
use crate::fs::fields as f;

Expand Down Expand Up @@ -212,34 +211,20 @@ impl<'dir> File<'dir> {
self.metadata.file_type().is_socket()
}

// Whether this file is a mount point
/// Whether this file is a mount point
pub fn is_mount_point(&self) -> bool {
if cfg!(unix) {
if self.is_directory() {
let mounts = &MOUNTS.read().unwrap().0;
for mount in mounts {
if self.absolute_path.eq(&mount.dest) {
return true;
}
}
return ALL_MOUNTS.contains_key(&self.absolute_path);
}
}
return false;
}

// The filesystem device and type for a mount point
pub fn mount_point_info(&self) -> Option<MountedFs> {
if self.is_mount_point() {
let mounts = &MOUNTS.read().unwrap().0;
for mount_point in mounts {
if self.absolute_path.eq(&mount_point.dest) {
return Some(MountedFs {
dest: mount_point.dest.to_string_lossy().into_owned(),
fstype: mount_point.fstype.clone(),
source: mount_point.source.to_string_lossy().into_owned(),
})
}
}
/// The filesystem device and type for a mount point
pub fn mount_point_info(&self) -> Option<&MountedFs> {
if cfg!(unix) {
return ALL_MOUNTS.get(&self.absolute_path);
}
None
}
Expand Down
24 changes: 23 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::wildcard_imports)]

#[macro_use]
extern crate lazy_static;

use std::collections::HashMap;
use std::env;
use std::ffi::{OsStr, OsString};
use std::io::{self, Write, ErrorKind};
use std::path::{Component, PathBuf};

use ansi_term::{ANSIStrings, Style};
use proc_mounts::MOUNTS;

use log::*;

use crate::fs::{Dir, File};
use crate::fs::{Dir, File, MountedFs};
use crate::fs::feature::git::GitCache;
use crate::fs::filter::GitIgnore;
use crate::options::{Options, Vars, vars, OptionsResult};
Expand All @@ -46,6 +51,23 @@ mod options;
mod output;
mod theme;

lazy_static! {
static ref ALL_MOUNTS: HashMap<PathBuf, MountedFs> = {
let mut m = HashMap::new();
if cfg!(unix) {
for mount_point in &MOUNTS.read().unwrap().0 {
let mount = MountedFs {
dest: mount_point.dest.to_string_lossy().into_owned(),
fstype: mount_point.fstype.clone(),
source: mount_point.source.to_string_lossy().into_owned(),
};
m.insert(mount_point.dest.clone(), mount);
}
}
m
};
}


fn main() {
use std::process::exit;
Expand Down
2 changes: 1 addition & 1 deletion src/output/file_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub struct FileName<'a, 'dir, C> {
options: Options,

/// The filesystem details for a mounted filesystem.
mounted_fs: Option<MountedFs>,
mounted_fs: Option<&'a MountedFs>,

/// How to handle displaying a mounted filesystem.
mount_style: MountStyle,
Expand Down

0 comments on commit 8b10a8c

Please sign in to comment.