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

Faster _nvm_find_up via string builtin #188

merged 1 commit into from
Jun 9, 2022

Conversation

jorgebucaran
Copy link
Owner

We could improve our .nvmrc find-up recursive function by switching to the string builtin instead of using dirname.

名称未設定

@jorgebucaran jorgebucaran added the enhancement New feature or fix label Jun 8, 2022
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).

@jorgebucaran jorgebucaran merged commit ae1e745 into main Jun 9, 2022
@jorgebucaran jorgebucaran deleted the fast-find-up branch June 9, 2022 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants