Erm...

case "$1" in
        *.sh)
                # Source shell script for speed.
                (
                        trap - INT QUIT TSTP
                        scriptname=$1
                        shift
                        . $scriptname
                )
                ;;
        *)
                "$@"
                ;;
  esac

OPTIMISATION FAIL.

NP: Roseland NYC Live, Portishead

18:00 Monday, 23 Jun 2008 [#] [computers] (5 comments)

Posted by joey hess at Mon Jun 23 18:38:15 2008:
Posting FAIL to blogs w/o analysis is always asking for trouble or a waste of time :-)

In this case, while a subshell is still forked, the shell does avoid the overhead of restarting the ssh interpreter. Not likely to save a vast amount of time for a few dozen init scripts, but not a total FAIL AFAICS.
Posted by Anonymous at Mon Jun 23 19:38:05 2008:
I've benchmarked this, actually.  Sourcing rather than exec'ing saves significant amounts of time.

$ cat target.sh
#!/bin/bash
$ cat run.sh
#!/bin/bash
./target.sh
$ cat exec.sh
#!/bin/bash
exec ./target.sh
$ cat source.sh
#!/bin/bash
. target.sh
$ time for i in $(seq 1 10000) ; do ./run.sh ; done

real 0m36.046s
user 0m17.517s
sys 0m17.833s
$ time for i in $(seq 1 10000) ; do ./exec.sh ; done

real 0m32.856s
user 0m21.301s
sys 0m9.937s
$ time for i in $(seq 1 10000) ; do ./source.sh ; done

real 0m19.116s
user 0m6.332s
sys 0m7.924s
Posted by daniels at Mon Jun 23 19:52:39 2008:
@Anonymous: You missed the point, which was the () subshell invocation.
Posted by Anonymous at Mon Jun 23 19:57:25 2008:
No, just PASTE FAIL and lack of preview. :)

$ cat subshell.sh
\#!/bin/bash
( . ./target.sh )
$ time for i in $(seq 1 10000) ; do ./subshell.sh ; done

real 0m23.406s
user 0m8.237s
sys 0m13.869s

Still a big win compared to exec.
Posted by Ross at Mon Jun 23 20:19:36 2008:
I was wondering if shells can implement () a lot faster than exec.  My target shell is the busybox shell (derived from ash) so any benchmarks will have to be against that and not bash (which is known to be slow) so I'll have to benchmark it and see what the performance metrics are.

Name:


E-mail:


URL:


Add 3 and 3 (required):


Comment: