From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: zsh-users@zsh.org
Subject: Re: Why sourcing a file is not faster than doing a loop with eval, zle -N
Date: Mon, 19 Jun 2017 17:16:01 +0100 [thread overview]
Message-ID: <20170619161601.GB9294@chaz.gmail.com> (raw)
In-Reply-To: <170619083116.ZM17323__41722.0601499595$1497886320$gmane$org@torch.brasslantern.com>
2017-06-19 08:31:16 -0700, Bart Schaefer:
> On Jun 19, 1:24pm, Stephane Chazelas wrote:
> }
> } There's probably scope for optimisation here, though I can't
> } comment further as I don't know why that signal handling code is
> } there in the first place.
>
> rt_signprocmask should not be significantly more expensive than an
> assignment to an integer.
Still,
$ time zsh -c 'repeat 100 . ./fsh_cache' 2> /dev/null
zsh -c 'repeat 100 . ./fsh_cache' 2> /dev/null 0.73s user 0.78s system 99% cpu 1.522 total
$ time zsh -c 'repeat 100 eval "$(<fsh_cache)"' 2> /dev/null
zsh -c 'repeat 100 eval "$(<fsh_cache)"' 2> /dev/null 0.80s user 0.04s system 99% cpu 0.848 total
See how the system time falls to almost 0 with the eval variant.
I get the same kind of performance gain if I comment out the
line that eventually calls the rt_signprocmask there.
winch_unblock() (so only for SIGWINCH).
> The signal handling code is there because the shell MUST NOT respond
> instantly to arbitrary signals while doing operations such as token
> interpretation or or memory management -- the signal handlers might
> themselves invoke shell commands/functions and many of those layers
> are not safe for re-entrancy -- but it MUST respond to those signals
> whenever it may be blocked for an unknown length of time, such as when
> reading from a file descriptor.
>
> Many years of "I can't interrupt my script when X" or "interrupting
> my script when Y causes a crash" resulted in the current signal
> paradigm. When the shell was first written, processors weren't fast
> enough and process scheduling not well-threaded enough to expose a
> lot of these issues, but the better our computers get the greater
> the likelyhood of hitting an ever-smaller race condition window, so
> those windows have to be aggressively closed.
I suspected it would be something like that, but here note that
it's done for every byte of the data even though the code is
read in full chunks at a time (by stdio's fgetc)
If you look at the strace output, you see something:
open("/etc/zsh/zshenv", O_RDONLY|O_NOCTTY) = 3
fcntl(3, F_DUPFD, 10) = 11
read(11, "# /etc/zsh/zshenv: system-wide ."..., 4096) = 623
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [], 8) = 0
[...]
open("./fsh_cache", O_RDONLY|O_NOCTTY) = 3
fcntl(3, F_DUPFD, 10) = 13
read(13, "zle -N orig-s0.0000060000-r9037-"..., 4096) = 4096
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [CHLD], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [CHLD], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [CHLD], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [CHLD], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [CHLD], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [CHLD], 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [WINCH], [CHLD], 8) = 0
[...]
Most of those rt_sigprocmask are unnecessary.
That defeats a benefit of stdio saving read() systems calls by
reading in chunk if we end up doing one system call per byte
anyway.
--
Stephane
next prev parent reply other threads:[~2017-06-19 16:16 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <etPan.594513a8.516100cd.10b2e__10513.1716504276$1497699329$gmane$org@zdharma.org>
2017-06-19 12:24 ` Stephane Chazelas
2017-06-19 15:31 ` Bart Schaefer
[not found] ` <170619083116.ZM17323__41722.0601499595$1497886320$gmane$org@torch.brasslantern.com>
2017-06-19 16:16 ` Stephane Chazelas [this message]
2017-06-19 19:14 ` Bart Schaefer
2017-06-17 11:34 Sebastian Gniazdowski
[not found] ` <CAH+w=7bVXtubcdwvEBC9isE32683dUipAUS=vrAkgO5pp2bkkw@mail.gmail.com>
[not found] ` <CAH+w=7afTi=1bfLBCmq8-vB-rLWDtEkAtk8gCCna3-mQwZ1-Ow@mail.gmail.com>
2017-06-17 14:05 ` Bart Schaefer
[not found] ` <etPan.594538f9.2ea629d6.10b2e@AirmailxGenerated.am>
2017-06-17 14:56 ` Sebastian Gniazdowski
2017-06-17 15:44 ` Sebastian Gniazdowski
2017-06-17 16:39 ` Bart Schaefer
2017-06-17 17:25 ` Sebastian Gniazdowski
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=20170619161601.GB9294@chaz.gmail.com \
--to=stephane.chazelas@gmail.com \
--cc=schaefer@brasslantern.com \
--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).