bug-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] make function local


From: Chet Ramey
Subject: Re: [Help-bash] make function local
Date: Mon, 20 Apr 2015 17:09:00 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

On 4/20/15 4:04 PM, Peng Yu wrote:

>> I disagree that performance overhead in typical use is `significant'.
>> This point is more or less identical to the one I discussed Friday in
>> regards to creating huge numbers of variables.  Who creates 100,000
>> shell functions in a single script?  The overhead, such as it is, of
>> creating and calling even 10,000 functions is negligible.
> 
> Why do you assume that one only calls each of these functions once?
> What if a function is called thousands of time?

Your example script called each function once.  If you want to make a
different argument, use a different example.

Let's deconstruct your argument.  Your assertion is that execution time
will be dominated by the time it takes to look up a function in the shell's
hash table, and that lookup time can be avoided in the case where there
are huge numbers of functions -- on the order of more than 10,000 -- by
adding local functions that are stored separately from the set of functions
at the global scope.  Correct?

> The performance difference is 10x different (0.490s vs 0.040s). To me,
> this is a huge performance problem.

The performance difference is 10x when you define 10x functions.  The
difference is between 10,000, which is highly unlikely, to 100,000,
which is truly outrageous.  Are you seriously proposing that there are
scripts in actual use that define 10,000 or 100,000 functions?  I'd
suggest that any script with that many functions deserves a rewrite,
maybe in a different language.

> Functions are usually called for more times they are defined.
> Therefore, even if the function creation time is acceptable, it
> doesn't mean its call time is acceptable.

That's true, but you haven't demonstrated that call time is unacceptable.

> The main point that Linda and I are making is that we suggest add the
> local function feature. I believe that we've made the case --- it
> makes the code cleaner and run faster.

You've not made any such case.  There's nothing other than your
assertion that real-world benefits will accrue from adding local
functions, and there is no reason to believe that any script in actual
use will benefit from local functions.  The sole advantage is the
namespace issue: you would be able to define a function at a local
scope with the same name as a function at the global scope.  That
would not appear to make code any cleaner, and there's certainly no
evidence that there are performance problems that this would solve.
Increasing the number of buckets in the hash table used to store shell
functions, which I already did, will have a greater performance benefit.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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