From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20300 invoked by alias); 1 Apr 2018 18:39:56 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 23302 Received: (qmail 12736 invoked by uid 1010); 1 Apr 2018 18:39:56 -0000 X-Qmail-Scanner-Diagnostics: from mail-lf0-f54.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(209.85.215.54):SA:0(-1.9/5.0):. Processed in 13.979587 secs); 01 Apr 2018 18:39:56 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_PASS,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: schaefer@brasslantern.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=lfOJaEnrHHxSMVYUCqP2lPbN4AtpbPYHZ+lhtY2jFI8=; b=EnOGyLa+enrDZhpGsq4gKNAXXXI01tKL3Cx1mJH16p5Jv+q5HFOm+EkVLSQeoFJbEy CyCJvuJYe9H3BKQ1YcsFAISYFE5JaoFuHVUUDrvpqMmONtily2PPQoEUaZhmrYKamcdD gjbDc03csec3/rNK/iGC3Nu71M3+uA/PXdIR7gZijFNNrllA4YOJHjEyqryjCvfIm8ta zClsenQNa+xJ8tsc4LTwd0SBfSjNXF2B6+MR0ycadZxlOjdb5kliHGlZIlrRg7se9mkk 9IgUt/quYqOnFfPpq6iUIqHNjunn2CBViYQGP2YEPwMWpj5Ji+sE2MZMxDqJ6MtQzKI8 c+zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=lfOJaEnrHHxSMVYUCqP2lPbN4AtpbPYHZ+lhtY2jFI8=; b=Rdc23vcaU1o1jHQw/jam3jQWOitjKidbg8Lc807PKxxstp1kBB5qNw0VMgA6vNm0Yd AknFPs5LNJ0dcvuObNNNpmJj0jXaiN20QVH21MnEriurbS6O+oRqVpGPEXxeTiUKZfEW sJGeDR/ClzmDBdqL1zZS0ElIZUKL6CFJ56t+3UjzohpC/a8jS8jb4NbeLMWS4YdsvWmn rHQLrphgNkrmA2oNIwImA9HJIOaeJNyrWcor+ZwynLHDOLMoiZjvl+IiTlhBJvIRQqy+ ZU2fLpd0t4DtZrTWrBG6XA6BltTSmgKwscxEShlvv4Fm8Scaz2S9P4bYper4VakDTmmc 1VYA== X-Gm-Message-State: ALQs6tBlVhRF4BppyENORhQX5bmVRvYvk86MOM/ncTvPpWRbmSmuc6xf MnXUCEFdeOjQmeEk3SEb55KJ7J5rcj6ObA7fzH0cYA== X-Google-Smtp-Source: AIpwx4+5FPjMZlaBj9f2FhSF0nWpLcZD9669yU84Wdoa9I0M92ZVEdVtkXkF5Tlb1vZaDVUEMfoISUwlizKEagPWk+4= X-Received: by 10.46.131.80 with SMTP id l16mr4138968ljh.133.1522607978843; Sun, 01 Apr 2018 11:39:38 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Bart Schaefer Date: Sun, 1 Apr 2018 11:39:38 -0700 Message-ID: Subject: Re: fast subshell To: Ray Andrews Cc: Zsh Users Content-Type: text/plain; charset="UTF-8" On Sun, Apr 1, 2018 at 8:59 AM, Ray Andrews wrote: > > local IFS=$'\n' # Must split on newlines only. > all_matches=( $( whence -mavS $@ ) ) > IFS=$OLD_IFS > > ( > IFS=$'\n' # Must split on newlines only. > all_matches=( $( whence -mavS $@ ) ) > ) > > ... and the interesting thing is that the former executes a stress test in > ca. 290 mS, but the latter in ca. 250 mS. The difference isn't significant > in the real world, but it is hard to understand, it seems strange that the > subshell would be faster. Is this possible? $(...) is always going to create one subshell. How expensive a subshell is, may depend on several things: How the operating system implements process forking; how much memory your current shell is using at the time; how busy the system otherwise is; what kind of multithreading your processor supports; and so on. It's quite possible that doing the memory management for the assignment to all_matches in a newly forked process is faster than in the original shell (perhaps because the subshell never needs to free it again?). There is also some overhead involved in saving/restoring IFS with "local" which you are avoiding. Also there's really no reason to do both "local IFS=..." and "IFS=$OLD_IFS", the whole point of the "local" is to allow zsh to do the save/restore of IFS for you. If you only need the change of $IFS for "whence" then you can do it with an anonymous function: (){ local IFS=$'\n'; all_matches=( $( whence -mavS $@ ) ) } Or you can do it without changing IFS at all, like this: all_matches=( ${(f)"$( whence -mavS $@ )"} )