autojump, z, z.lua, fasd 的区别

2019-01-20 18:42:41 +08:00
 Kobayashi

转载自 reddit r/linux。原本是有人用 Lua 写了一个 z,下面有回复者根据 4 者挂载的钩子不同做出了分析。

预先解释一下几种钩子:

I have a question about the hooks. I use zsh. As far I know, z.lua and z use precmd hook, fasd uses preexec hook, autojump uses chpwd hook. So, who is doing the job right?

Update: I got an answer by myself.

autojump is hooked on chpwd in zsh. Because the frecent is based on the times you accessed the directory.

z take the time you spent in the directory into consideration. So z hooks on precmd. The same thing happens with z.lua.

fasd takes a further step, not only because fasd combines z and v together. Besides taking the time you spent in the directory into consideration, it also extracts path or file info from your command line. This explains why fasd uses the preexec hook.

Unlike v, who get files you used from ~/.viminfo, fasd gives you four backends to get the list of files you opened. You may guess it, there's one backend called current, that gets the list based on the files extracted from your command line.

Let's talk about the speed of adding path into history data file.

The 1st thing I want to mention is, fasd is not that slow. Though fasd is slower that z, but it's much faster than autojump.

I benchmarked the four on my machine, the result differs from the one made by the author of z.lua.

time cost: autojump > fasd > _z > _zlua

Tested on my mbp (15-inch, 2016):

❯ time (autojump --add /tmp)
( autojump --add /tmp; )  0.12s user 0.05s system 68% cpu 0.249 total

❯ time (fasd -A /tmp)
( fasd -A /tmp; )  0.02s user 0.01s system 69% cpu 0.042 total

❯ time (_z --add /tmp)
( _z --add /tmp; )  0.01s user 0.01s system 56% cpu 0.019 total

❯ time (_zlua --add /tmp)
( _zlua --add /tmp; )  0.00s user 0.00s system 75% cpu 0.008 total

(因为据 z.lua 作者所说,最耗时的部分就是添加路径到历史数据文件这一过程,所以针对此过程测试。但z.lua 作者的测试应该有问题,autojump 由于 Python 解释器启动耗时问题,按理说肯定是最慢的,但 z.lua 作者的结果中却是 fasd 最慢。我自己的测试情况也是 fasd 比 z 略慢,但远快于 autojump。)

autojump is written in Python. One of the main cause for its slowness is, starting up the Python interpreter takes time.

Both z and fasd are written in shell scripts. Because fasd extracts info from your command line using sed and regex, it's slower than z.

z.lua is the fastest one among the four. It seems z.lua takes the advantage of Lua the language.

You may have your own choice now. Before I finish this, I'd like to give you another advice about how to speed up the startup time of the four.

You should avoid generating the init code time by time. Generate the init codes once and cache them, source the cache next time you spawn a new shell.

prezto/modules/fasd/init.zsh

cache_file="${TMPDIR:-/tmp}/prezto-fasd-cache.$UID.zsh"
if [[ "${commands[fasd]}" -nt "$cache_file" \
    || "${ZDOTDIR:-$HOME}/.zpreztorc" -nt "$cache_file" \
    || ! -s "$cache_file"  ]]; then
    # Set the base init arguments.
    init_args=(zsh-hook)

    # Set fasd completion init arguments, if applicable.
    if zstyle -t ':prezto:module:completion' loaded; then
        init_args+=(zsh-ccomp zsh-ccomp-install zsh-wcomp zsh-wcomp-install)
    fi

    # Cache init code.
    fasd --init "$init_args[@]" >! "$cache_file" 2> /dev/null
fi

Anyway, z.lua is a promising project. If you only need directory jumping, it may be the best choice.

总结一下,此评论给出的结论基本是

原文链接,z.lua - A better method to change directory.

另附各工具项目主页:

当然,现在的主流做法是搭配 fuzzy finder 使用,详见fzf wiki Examples#z

另外这个回复中最有意思的是 Prezto 模块的优化。翻看了一下,Prezto 中 command-not-found 模块也作了类似处理,缓存了命令的位置。python 模块中的 pyenv 初始化使用--no-rehash,减小耗时,node 模块中 nodenv、ruby 模块中的 rbenv 都做了这样的处理。相比之下,Oh-My-ZSH 的插件则缺少对应这种优化。

各位还知不知道其他类似的比较,可以分享一下。

3428 次点击
所在节点    分享发现
1 条回复
hanxiV2EX
2019-01-21 01:20:05 +08:00
z.lua 的作者在这里 @skywind3000

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/528858

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX