automake-patches
[Top][All Lists]
Advanced

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

Re: avoiding forks in shell scripts (was: [PATCH] Modernize, improve and


From: Stefano Lattarini
Subject: Re: avoiding forks in shell scripts (was: [PATCH] Modernize, improve and/or extend tests `colon*.test.)
Date: Sun, 8 Aug 2010 19:45:28 +0200
User-agent: KMail/1.13.3 (Linux/2.6.30-2-686; KDE/4.4.4; i686; ; )

At Sunday 08 August 2010, Ralf Wildenhues wrote:
> * Stefano Lattarini wrote on Sun, Aug 08, 2010 at 07:09:46PM CEST:
> > At Sunday 08 August 2010, Ralf Wildenhues wrote:
> > > * Stefano Lattarini wrote on Sun, Aug 08, 2010 at 06:22:00PM 
CEST:
> > > > Exactly! So:
> > > >   foo() { foo_result=`sed ...`; }
> > > >   foo; bar=$foo_result;
> > > > 
> > > > instead of:
> > > >   foo() { sed ...; }
> > > >   bar=`foo`
> > > > 
> > > > Where is the significant improvement?
> > > 
> > > Two forks vs. one: the latter still forks right before exec'ing
> > > sed.
> > 
> > And the first doesn't?
> 
> For some shells, that is correct.
> 
> Your questioning prompted me to try it out, to make sure I'm not
> telling anything wrong here.  So, with bash 3.2.39 on GNU/Linux,
> the first of the scripts below calls clone once, the second twice,
> as strace -fF tells me.
Ah-ah!  An optimization done under-the-hood by the shell...  Now,
this makes sense.
 
> Cheers,
> Ralf
> 
> --- script 1 ---
> #! /bin/sh
> foo () { result=`sed -n 1p $0`; }
> foo
> echo $result
> 
> 
> --- script 2 ---
> #! /bin/sh
> foo () { sed -n 1p $0; }
> result=`foo`
> echo $result

Thanks
  Stefano



reply via email to

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