Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Why sourcing a file is not faster than doing a loop with eval, zle -N
- X-seq: zsh-users 22737
- From: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- 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
- Cc: zsh-users@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=P+TpoEPTBZi36NqPPi6nJY8fEQxciJBa0ZPjeCx9BzM=; b=haQP7BVWT5wGJeNAx6XWV/fCPtAh38IIVvZUDUr/5gCgiMFetZKT11WQPmJqPGvtZT EYO0PCwyuQoed6+WnUhZICaYWt60JkZ2JchudfoHzFIxKYNLb9WzFkooRqy1DOPySPPG tuxYBBBalJIG07tZJ/jc4T81rOUh5emnbZ792LE8z99Ol09wclzEVZmwkESHor1fDRVX uOvBEgjvR+ciutv2HcmXI8eRaSdiEQ8NDDG+Qp0gOQf0w/1ar2j8Uv1elg+pBBQYSBDV HngwijocTwdSxg6xZL2POERfxS43eKd+AXmuj+pbMUeifMpCuBlWmdrHbJ1rtpOg1UbV 5Mug==
- In-reply-to: <170619083116.ZM17323__41722.0601499595$1497886320$gmane$org@torch.brasslantern.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mail-followup-to: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, zsh-users@xxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <etPan.594513a8.516100cd.10b2e__10513.1716504276$1497699329$gmane$org@zdharma.org> <20170619122413.GA9294@chaz.gmail.com> <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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author