zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: fast subshell
Date: Sun, 1 Apr 2018 11:39:38 -0700	[thread overview]
Message-ID: <CAH+w=7Z=9t0hK2CGibSNfN=WWhZzNLq9MHP_N-+s05DgGhq5vQ@mail.gmail.com> (raw)
In-Reply-To: <df6bc2d6-48ad-d491-355b-0228a4c6e503@eastlink.ca>

On Sun, Apr 1, 2018 at 8:59 AM, Ray Andrews <rayandrews@eastlink.ca> 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 $@ )"} )


  reply	other threads:[~2018-04-01 18:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-01 15:59 Ray Andrews
2018-04-01 18:39 ` Bart Schaefer [this message]
2018-04-01 20:45   ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7Z=9t0hK2CGibSNfN=WWhZzNLq9MHP_N-+s05DgGhq5vQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).