zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Roman Perepelitsa <roman.perepelitsa@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Possible bug: HASH_CMDS has no observable effect
Date: Sat, 12 Sep 2020 13:41:34 -0700	[thread overview]
Message-ID: <CAH+w=7aBwDz_Uo4r2ohv9wLBpeHna2C9g0QGN5FsXmqs8Hh2CA@mail.gmail.com> (raw)
In-Reply-To: <CAN=4vMr0ucdLxfb+GMevBVT-=u1oNRr+Tc9EEVKizkeQC5tL7A@mail.gmail.com>

On Sat, Sep 12, 2020 at 1:49 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> On Sat, Sep 12, 2020 at 10:35 AM Bart Schaefer
> <schaefer@brasslantern.com> wrote:
> > The whole search-and-cache process is
> > short-circuited.
>
> Doesn't this search happen anyway? When I type `rsync`, it gets
> resolved as /usr/bin/rsync and gets executed. This requires searching
> for rsync in all path directories.

The cache is a mapping from command names to locations.  So,  if
"rsync" is in the cache, then the value of that cache entry is
"/usr/bin/rsync" and zsh just executes that without scanning the path
again.  Conversely if HASH_DIRS is set and "rsync" is NOT in the
cache, but everything else from /usr/bin IS in the cache (as a
byproduct of some previous search), zsh reports command not found,
again without re-scanning the path.

HASH_DIRS is done incrementally, e.g., if your path is "/bin:/usr/bin"
and you first execute a command from /bin, zsh only populates the
cache with commands in /bin, and remembers that it has not looked in
/usr/bin yet.  If you then execute "rsync", a cache miss causes
HASH_DIRS to scan /usr/bin, add everything there to the cache, and
execute /usr/bin/rsync.

Eventually every directory in the path will have been scanned, and
cache misses become immediately "not found".  If HASH_DIRS is not set
(but HASH_CMDS is).

> I believe this is done in execute()
> in Src/exec.c. Wouldn't it be better to search for `rsync` in the
> parent shell (before forking) and hash the result?

That's exactly how it works.  The search in execute() is only done if
an absolute file location for the command isn't provided by the
parent, which only occurs if HASH_CMDS is not set.

This does mean that if a command is removed from (or moved within) the
path, execute() will be handed an invalid file location and will fail.
Again, for most users this never happens.


  reply	other threads:[~2020-09-12 20:42 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-11  8:21 Roman Perepelitsa
2020-09-11 14:48 ` Phil Pennock
2020-09-11 15:01   ` Roman Perepelitsa
2020-09-11 16:10     ` Phil Pennock
2020-09-11 16:33       ` Roman Perepelitsa
2020-09-11 21:10         ` Bart Schaefer
2020-09-12  7:02           ` Roman Perepelitsa
2020-09-12  8:35             ` Bart Schaefer
2020-09-12  8:49               ` Roman Perepelitsa
2020-09-12 20:41                 ` Bart Schaefer [this message]
2020-09-12 20:43                   ` Bart Schaefer
2020-09-13  9:31                   ` Roman Perepelitsa
2020-09-13 22:24                     ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7aBwDz_Uo4r2ohv9wLBpeHna2C9g0QGN5FsXmqs8Hh2CA@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=roman.perepelitsa@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).