Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: fast subshell
- X-seq: zsh-users 23302
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Ray Andrews <rayandrews@xxxxxxxxxxx>
- Subject: Re: fast subshell
- Date: Sun, 1 Apr 2018 11:39:38 -0700
- Cc: Zsh Users <zsh-users@xxxxxxx>
- 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==
- In-reply-to: <df6bc2d6-48ad-d491-355b-0228a4c6e503@eastlink.ca>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <df6bc2d6-48ad-d491-355b-0228a4c6e503@eastlink.ca>
On Sun, Apr 1, 2018 at 8:59 AM, Ray Andrews <rayandrews@xxxxxxxxxxx> 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 $@ )"} )
Messages sorted by:
Reverse Date,
Date,
Thread,
Author