🔨 🎉 use cache for user and group in unix, gain 60% improve

Signed-off-by: Wei Zhang <kweizh@gmail.com>
This commit is contained in:
Wei Zhang 2024-04-01 14:13:06 +08:00
parent 126acf7762
commit 240d513338
5 changed files with 120 additions and 88 deletions

109
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]]
name = "aho-corasick"
version = "1.1.2"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
@ -107,9 +107,9 @@ dependencies = [
[[package]]
name = "autocfg"
version = "1.1.0"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80"
[[package]]
name = "bitflags"
@ -119,9 +119,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.2"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
dependencies = [
"serde",
]
@ -161,9 +161,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.35"
version = "0.4.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a"
checksum = "8a0d04d43504c61aa6c7531f1871dd0d418d91130162063b789da00fd7057a5e"
dependencies = [
"android-tzdata",
"iana-time-zone",
@ -277,7 +277,7 @@ version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df"
dependencies = [
"bitflags 2.4.2",
"bitflags 2.5.0",
"crossterm_winapi",
"libc",
"mio",
@ -361,9 +361,9 @@ dependencies = [
[[package]]
name = "fastrand"
version = "2.0.1"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984"
[[package]]
name = "float-cmp"
@ -473,11 +473,11 @@ dependencies = [
[[package]]
name = "git2"
version = "0.18.2"
version = "0.18.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b3ba52851e73b46a4c3df1d89343741112003f0f6f13beb0dfac9e457c3fdcd"
checksum = "232e6a7bfe35766bf715e55a88b39a700596c0ccfd88cd3680b4cdb40d66ef70"
dependencies = [
"bitflags 2.4.2",
"bitflags 2.5.0",
"libc",
"libgit2-sys",
"log",
@ -509,7 +509,7 @@ version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf760ebf69878d9fd8f110c89703d90ce35095324d1f1edcb595c63945ee757"
dependencies = [
"bitflags 2.4.2",
"bitflags 2.5.0",
"ignore",
"walkdir",
]
@ -589,9 +589,9 @@ dependencies = [
[[package]]
name = "indexmap"
version = "2.2.5"
version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b0b929d511467233429c45a44ac1dcaa21ba0f5ba11e4879e6ed28ddb4f9df4"
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [
"equivalent",
"hashbrown",
@ -621,9 +621,9 @@ dependencies = [
[[package]]
name = "itoa"
version = "1.0.10"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "jobserver"
@ -669,20 +669,19 @@ dependencies = [
[[package]]
name = "libredox"
version = "0.0.1"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8"
checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"
dependencies = [
"bitflags 2.4.2",
"bitflags 2.5.0",
"libc",
"redox_syscall",
]
[[package]]
name = "libz-sys"
version = "1.1.15"
version = "1.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "037731f5d3aaa87a5675e895b63ddff1a87624bc29f77004ea829809654e48f6"
checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9"
dependencies = [
"cc",
"libc",
@ -774,9 +773,9 @@ dependencies = [
[[package]]
name = "memchr"
version = "2.7.1"
version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[package]]
name = "mio"
@ -857,9 +856,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "pin-project-lite"
version = "0.2.13"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]]
name = "pin-utils"
@ -905,9 +904,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.78"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
@ -938,9 +937,9 @@ dependencies = [
[[package]]
name = "redox_users"
version = "0.4.4"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4"
checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891"
dependencies = [
"getrandom",
"libredox",
@ -949,9 +948,9 @@ dependencies = [
[[package]]
name = "regex"
version = "1.10.3"
version = "1.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
dependencies = [
"aho-corasick",
"memchr",
@ -972,9 +971,9 @@ dependencies = [
[[package]]
name = "regex-syntax"
version = "0.8.2"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
[[package]]
name = "rustix"
@ -992,11 +991,11 @@ dependencies = [
[[package]]
name = "rustix"
version = "0.38.31"
version = "0.38.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949"
checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89"
dependencies = [
"bitflags 2.4.2",
"bitflags 2.5.0",
"errno",
"libc",
"linux-raw-sys 0.4.13",
@ -1046,9 +1045,9 @@ dependencies = [
[[package]]
name = "serde_yaml"
version = "0.9.32"
version = "0.9.34+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd075d994154d4a774f95b51fb96bdc2832b0ea48425c92546073816cda1f2f"
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
dependencies = [
"indexmap",
"itoa",
@ -1123,9 +1122,9 @@ dependencies = [
[[package]]
name = "smallvec"
version = "1.13.1"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "strsim"
@ -1135,9 +1134,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.52"
version = "2.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07"
checksum = "11a6ae1e52eb25aab8f3fb9fca13be982a373b8f1157ca14b897a825ba4a2d35"
dependencies = [
"proc-macro2",
"quote",
@ -1161,7 +1160,7 @@ checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1"
dependencies = [
"cfg-if",
"fastrand",
"rustix 0.38.31",
"rustix 0.38.32",
"windows-sys 0.52.0",
]
@ -1190,7 +1189,7 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7"
dependencies = [
"rustix 0.38.31",
"rustix 0.38.32",
"windows-sys 0.48.0",
]
@ -1202,18 +1201,18 @@ checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76"
[[package]]
name = "thiserror"
version = "1.0.57"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b"
checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.57"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81"
checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
dependencies = [
"proc-macro2",
"quote",
@ -1264,9 +1263,9 @@ checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]]
name = "unsafe-libyaml"
version = "0.2.10"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]]
name = "url"
@ -1638,7 +1637,7 @@ checksum = "8da84f1a25939b27f6820d92aed108f83ff920fdf11a7b19366c27c4cda81d4f"
dependencies = [
"libc",
"linux-raw-sys 0.4.13",
"rustix 0.38.31",
"rustix 0.38.32",
]
[[package]]

View file

@ -4,7 +4,7 @@ use crate::flags::{Display, Flags, HyperlinkOption, Layout};
use crate::git_theme::GitTheme;
use crate::icon::Icons;
use crate::meta::name::DisplayOption;
use crate::meta::{FileType, Meta};
use crate::meta::{FileType, Meta, OwnerCache};
use std::collections::HashMap;
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use terminal_size::terminal_size;
@ -23,10 +23,12 @@ pub fn grid(
git_theme: &GitTheme,
) -> String {
let term_width = terminal_size().map(|(w, _)| w.0 as usize);
let owner_cache = OwnerCache::default();
inner_display_grid(
&DisplayOption::None,
metas,
&owner_cache,
flags,
colors,
icons,
@ -57,8 +59,11 @@ pub fn tree(
}
}
let owner_cache = OwnerCache::default();
for cell in inner_display_tree(
metas,
&owner_cache,
flags,
colors,
icons,
@ -77,6 +82,7 @@ pub fn tree(
fn inner_display_grid(
display_option: &DisplayOption,
metas: &[Meta],
owner_cache: &OwnerCache,
flags: &Flags,
colors: &Colors,
icons: &Icons,
@ -117,6 +123,7 @@ fn inner_display_grid(
let blocks = get_output(
meta,
owner_cache,
colors,
icons,
git_theme,
@ -176,6 +183,7 @@ fn inner_display_grid(
output += &inner_display_grid(
&display_option,
content,
owner_cache,
flags,
colors,
icons,
@ -223,6 +231,7 @@ fn add_header(flags: &Flags, cells: &[Cell], grid: &mut Grid) {
#[allow(clippy::too_many_arguments)]
fn inner_display_tree(
metas: &[Meta],
owner_cache: &OwnerCache,
flags: &Flags,
colors: &Colors,
icons: &Icons,
@ -248,6 +257,7 @@ fn inner_display_tree(
for block in get_output(
meta,
owner_cache,
colors,
icons,
git_theme,
@ -276,6 +286,7 @@ fn inner_display_tree(
cells.extend(inner_display_tree(
content,
owner_cache,
flags,
colors,
icons,
@ -314,6 +325,7 @@ fn display_folder_path(meta: &Meta) -> String {
#[allow(clippy::too_many_arguments)]
fn get_output(
meta: &Meta,
owner_cache: &OwnerCache,
colors: &Colors,
icons: &Icons,
git_theme: &GitTheme,
@ -357,11 +369,11 @@ fn get_output(
]);
}
Block::User => block_vec.push(match &meta.owner {
Some(owner) => owner.render_user(colors, flags),
Some(owner) => owner.render_user(colors, owner_cache, flags),
None => colorize_missing("?"),
}),
Block::Group => block_vec.push(match &meta.owner {
Some(owner) => owner.render_group(colors, flags),
Some(owner) => owner.render_group(colors, owner_cache, flags),
None => colorize_missing("?"),
}),
Block::Context => block_vec.push(match &meta.access_control {

View file

@ -7,7 +7,7 @@ mod inode;
mod links;
mod locale;
pub mod name;
mod owner;
pub mod owner;
mod permissions;
mod permissions_or_attributes;
mod size;
@ -26,7 +26,7 @@ pub use self::indicator::Indicator;
pub use self::inode::INode;
pub use self::links::Links;
pub use self::name::Name;
pub use self::owner::Owner;
pub use self::owner::{Cache as OwnerCache, Owner};
pub use self::permissions::Permissions;
use self::permissions_or_attributes::PermissionsOrAttributes;
pub use self::size::Size;

View file

@ -686,7 +686,7 @@ mod test {
)
);
let file_path = tmp_dir.path().join("\\.txt");
let file_path = tmp_dir.path().join(PathBuf::from("\\.txt"));
File::create(&file_path).expect("failed to create file");
let meta = file_path.metadata().expect("failed to get metas");

View file

@ -2,46 +2,47 @@ use crate::color::{ColoredString, Colors, Elem};
use crate::Flags;
#[cfg(unix)]
use std::fs::Metadata;
#[cfg(unix)]
use users::{Groups, Users, UsersCache};
#[derive(Clone, Debug)]
#[derive(Default)]
pub struct Cache {
#[cfg(unix)]
users: UsersCache,
#[cfg(unix)]
groups: UsersCache,
}
#[cfg(unix)]
#[derive(Clone, Debug, Default)]
pub struct Owner {
user: u32,
group: u32,
}
#[cfg(windows)]
#[derive(Clone, Debug, Default)]
pub struct Owner {
user: String,
group: String,
}
impl Owner {
#[cfg_attr(unix, allow(dead_code))]
#[cfg(windows)]
pub fn new(user: String, group: String) -> Self {
Self { user, group }
}
}
impl Default for Owner {
fn default() -> Owner {
Owner {
user: String::from("-"),
group: String::from("-"),
}
}
}
#[cfg(unix)]
impl From<&Metadata> for Owner {
fn from(meta: &Metadata) -> Self {
use std::os::unix::fs::MetadataExt;
use users::{get_group_by_gid, get_user_by_uid};
let user = match get_user_by_uid(meta.uid()) {
Some(res) => res.name().to_string_lossy().to_string(),
None => meta.uid().to_string(),
};
let group = match get_group_by_gid(meta.gid()) {
Some(res) => res.name().to_string_lossy().to_string(),
None => meta.gid().to_string(),
};
Self { user, group }
Self {
user: meta.uid(),
group: meta.gid(),
}
}
}
@ -62,10 +63,20 @@ fn truncate(input: &str, after: Option<usize>, marker: Option<String>) -> String
}
impl Owner {
pub fn render_user(&self, colors: &Colors, flags: &Flags) -> ColoredString {
// allow unused variables because cache is used in unix, maybe we can cache for windows in the future
#[allow(unused_variables)]
pub fn render_user(&self, colors: &Colors, cache: &Cache, flags: &Flags) -> ColoredString {
#[cfg(unix)]
let user = &match cache.users.get_user_by_uid(self.user) {
Some(user) => user.name().to_string_lossy().to_string(),
None => self.user.to_string(),
};
#[cfg(windows)]
let user = &self.user;
colors.colorize(
truncate(
&self.user,
user,
flags.truncate_owner.after,
flags.truncate_owner.marker.clone(),
),
@ -73,10 +84,20 @@ impl Owner {
)
}
pub fn render_group(&self, colors: &Colors, flags: &Flags) -> ColoredString {
// allow unused variables because cache is used in unix, maybe we can cache for windows in the future
#[allow(unused_variables)]
pub fn render_group(&self, colors: &Colors, cache: &Cache, flags: &Flags) -> ColoredString {
#[cfg(unix)]
let group = &match cache.groups.get_group_by_gid(self.group) {
Some(group) => group.name().to_string_lossy().to_string(),
None => self.group.to_string(),
};
#[cfg(windows)]
let group = &self.group;
colors.colorize(
truncate(
&self.group,
group,
flags.truncate_owner.after,
flags.truncate_owner.marker.clone(),
),