Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refac: use Lazy to optimize env::current_dir repeated call #261

Merged
merged 2 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Categories Used:
- Use `Cow<'static, str>` in `FinalError` [\#246](https://github.com/ouch-org/ouch/pull/246) ([vrmiguel](https://github.com/vrmiguel))
- Don't allocate when possible in `to_utf`, `nice_directory_display` [\#249](https://github.com/ouch-org/ouch/pull/249) ([vrmiguel](https://github.com/vrmiguel))
- Allow overriding the completions output directory [\#251]](https://github.com/ouch-org/ouch/pull/251) ([jcgruenhage](https://github.com/jcgruenhage))
- Use Lazy to optimize env::current_dir repeated call [\#261]](https://github.com/ouch-org/ouch/pull/261) ([marcospb19](https://github.com/marcospb19))

### Tweaks

Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ pub mod utils;
/// CLI argparsing definitions, using `clap`.
pub mod opts;

use std::{env, path::PathBuf};

use error::{Error, Result};
use once_cell::sync::Lazy;
use opts::{Opts, Subcommand};
use utils::{QuestionAction, QuestionPolicy};

// Used in BufReader and BufWriter to perform less syscalls
const BUFFER_CAPACITY: usize = 1024 * 32;

/// Current directory or empty directory
static CURRENT_DIRECTORY: Lazy<PathBuf> = Lazy::new(|| env::current_dir().unwrap_or_default());

/// The status code returned from `ouch` on error
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;

Expand Down
11 changes: 4 additions & 7 deletions src/utils/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{borrow::Cow, cmp, env, path::Path};
use std::{borrow::Cow, cmp, path::Path};

use crate::CURRENT_DIRECTORY;

/// Converts an OsStr to utf8 with custom formatting.
///
Expand All @@ -17,12 +19,7 @@ pub fn to_utf(os_str: &Path) -> Cow<str> {
/// Removes the current dir from the beginning of a path as it's redundant information,
/// useful for presentation sake.
pub fn strip_cur_dir(source_path: &Path) -> &Path {
let current_dir = env::current_dir();

let current_dir = match &current_dir {
Ok(inner) => inner.as_path(),
Err(_) => Path::new(""),
};
let current_dir = &*CURRENT_DIRECTORY;

source_path.strip_prefix(current_dir).unwrap_or(source_path)
}
Expand Down