bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Customize the command resolution in bash?


From: Eric Blake
Subject: Re: Customize the command resolution in bash?
Date: Fri, 11 Nov 2011 15:31:02 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110930 Thunderbird/7.0.1

On 11/11/2011 03:23 PM, Peng Yu wrote:
> Hi,
> 
> bash by default searchs in paths specified in the environment variable
> PATH (separated by ":"). I'm not aware if there is any cache mechanism
> to save the run time (but even so, different terminals still can not
> see the same cache, hence each terminal has the overhead to create the
> cache). When there are many files in PATH, it is going to slow down
> the performance.
> 
> One simple remedy is to instead search in a file where the abspaths of
> all the commands are saved (of course, this database file can be
> generated by using the command 'find' to search for all the
> directories in $PATH, which process can be scheduled to run
> periodically using cron). To make this work, I'm wondering if there is
> an easy way to customize the way that bash resolve a command.

Bash isn't doing the resolution so much as libc (read 'man execvp')
(well, technically, bash may be manually repeating some of the same
resolution code as in execvp for other reasons, but the same principles
apply).

If you want to search fewer directories, then nothing is stopping you from:

mkdir ~/mycache
for every executable you want cached:
  ln -s executable ~/mycache/
PATH=$HOME/mycache:$PATH

so that you now have a single directory with symlinks to all the
executables you want to run; thus, the attempt to stat()/execvp() each
executable will hit in the first directory in $PATH rather than getting
lots of misses as you progress through each directory in $PATH.  But
you'll still have to crawl through every directory (including the new
one you just added) for resolving things such as 'nosuch' not existing
anywhere on $PATH.

But whether this provides a measurable speedup, I don't know.  Benchmark
it yourself if you are interested in trying it.

Meanwhile, per POSIX, bash DOES provide hashing once it learns where an
executable lives, so that future invocations can rely on the hash (the
hash is invalidated when you assign to $PATH).  Read up on 'help hash'.

-- 
Eric Blake   address@hidden    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]