From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29772 invoked by alias); 2 Oct 2015 21:38:50 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 36752 Received: (qmail 9976 invoked from network); 2 Oct 2015 21:38:49 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Jmb5fLwTEHJjF7rjVrrXIMp7cyudisYR311kWHIjvIg=; b=biyMHh+fmtnVADIP4DsL3isCRtUjtqgNMMWrXcKycV3AaNKqQqvnMxRKx/A4wzYtSy exvf5jAsd94fCoMRUWcsOV8dlhMfOqtVCaP/6GqDYUjMXLnkM8At/b7zX/EflBdwoiCI +QbF1/8GPFapyUKmcwnr7ebGZBcBvZAk+U0WC+Ax4AGgNAg8e2YudzwZUhqG587wE2yl pOf88oSrYqWm5kfoamyZ2EvkxHw88gT1zoNe8NOOtwrMYOTbFl+uk0VLOX28QDptn8Zm pLhoiAaBmVBMIG4Rm00RSzHiPgzwuW0XcfJM6k0JCZxZkiaRRn4tFzwYM4qCAtP1erLo FMeQ== MIME-Version: 1.0 X-Received: by 10.140.100.182 with SMTP id s51mr21853304qge.25.1443821928228; Fri, 02 Oct 2015 14:38:48 -0700 (PDT) In-Reply-To: <560EF1CD.6060009@tthamilton.com> References: <560EF1CD.6060009@tthamilton.com> Date: Fri, 2 Oct 2015 23:38:48 +0200 Message-ID: Subject: Re: PATCH: minor variable allocation change in add-zsh-hook From: Mikael Magnusson To: Matthew Hamilton Cc: zsh workers Content-Type: text/plain; charset=UTF-8 On Fri, Oct 2, 2015 at 11:06 PM, Matthew Hamilton wrote: > The 'local usage' variable is allocated in main, outside of the if loop > that determines if it is going to print the usage information. This > means time is spent allocating that variable, when in most cases, it > will never be printed. It would be better to set it within the if loop, > or alternatively, not using a variable and simply output the usage text > as as literal string (as many other functions do). > > A trace of the time being needlessly being spent allocating the variable > can be seen here: https://gist.github.com/Eriner/3192c9eb98fabdd70607 > > It's not that much time, but it adds up and is inefficient/unnecessary. > > diff --git a/Functions/Misc/add-zsh-hook b/Functions/Misc/add-zsh-hook > index ee37d67..bccd115 100644 > --- a/Functions/Misc/add-zsh-hook > +++ b/Functions/Misc/add-zsh-hook > @@ -19,7 +19,6 @@ hooktypes=( > chpwd precmd preexec periodic zshaddhistory zshexit > zsh_directory_name > ) > -local usage="Usage: $0 hook function\nValid hooks are:\n $hooktypes" > > local opt > local -a autoopts > @@ -58,6 +57,7 @@ if (( list )); then > typeset -mp "(${1:-${(@j:|:)hooktypes}})_functions" > return $? > elif (( help || $# != 2 || ${hooktypes[(I)$1]} == 0 )); then > + local usage="Usage: $0 hook function\nValid hooks are:\n $hooktypes" > print -u$(( 2 - help )) $usage > return $(( 1 - help )) > fi You would need to add tens of thousands of hooks before this would make any difference, and then you're already far past the point of sanity. I like the way the current code separates the content and logic. There is also no such thing as an "if loop". -- Mikael Magnusson