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

Faster _nvm_find_up via string builtin #188

Merged
merged 1 commit into from
Jun 9, 2022
Merged
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
4 changes: 2 additions & 2 deletions functions/nvm.fish
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ end

function _nvm_find_up --argument-names path file
test -e "$path/$file" && echo $path/$file || begin
test "$path" != / || return
_nvm_find_up (command dirname $path) $file
test ! -z "$path" || return
_nvm_find_up (string replace --regex -- '/[^/]*$' "" $path) $file
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a comment here, so one isn’t tempted to refactor to the simpler dirname approach in the future.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thought: Is a while loop faster than recursion? Don’t know what the overhead of calling functions in fish is.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it doesn't seem to make much difference when using the following version:

function _nvm_find_up --argument-names path file
    while true
        test -e "$path/$file" && echo $path/$file && return
        set path (string replace --regex -- '/[^/]*$' "" $path)
        test "$path" = "" && return 1
    end
end

I even got mixed results when I ran it more than once. Sometimes, this version is slower by 2~3 ms.

Keep in mind I'm 40-ish directories deep in the directory tree. When you're in the same directory, even the original version using dirname runs at around 200 microseconds. 🤓

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a comment here, so one isn’t tempted to refactor to the simpler dirname approach in the future.

Ah, comments, yeah, let me come up with a good excuse to avoid having to write one.

Got it!

It's fishier (as in more idiomatic in Fish) to use builtins, as they're usually faster and consistent across OSes, therefore one shouldn't be tempted to switch to an external command (which may not even be available in the host system).

end
end

Expand Down