zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Is large `if' or `case' statement a potential cause of a slow-down?
Date: Wed, 13 Jun 2018 16:57:41 +0200	[thread overview]
Message-ID: <CAKc7PVCatykwJ6LMeEPX3ossi29Qm-rzBErruiXDCJx9MQfQ+g@mail.gmail.com> (raw)
In-Reply-To: <CAKc7PVBLD7M-aFqy_jPHsmscst3n6XP+VnNZi_MBdLaLyUpMFw@mail.gmail.com>

Guys is this silence to not openly state that something is not nice ;)
Or was my post e.g. too long. Because when I replaced significant if
(10 conditions if, elif, ..., fi ) with what I call "hue architecture"
(in FSH everything is about colors), described at the end, I've got
performance gain from 300 ms to 47 ms. This is great, not only fast,
but also implements "modularization" that many people like and that
will help fast-syntax-highlighting.

Hue description: suppose there is:

if [[ b = c ]]; then
...
elif [[ a = "b" ]]; then
...
elif [[ b = c ]]; then
...
elif (( 1 )); then
...
fi

It is then possible to put all conditions into array, just by-hand
copy-paste and wrapping with ', and then run:

local cond;
integer idx=1
for cond in "${HUE_ROUTING__SOME}"; do
    cond="$cond && return $idx; "
    HUE_ROUTING__SOME[idx]="$cond"
    idx+=1
done

To obtain array with:
HUE_ROUTING__SOME=(
    '[[ b = c ]] && return 1'
    '[[ a = "b" ]] && return 2'
    '[[ b = c ]] && return 3'
    '(( 1 )) && return 4'
)

And then do:

() { eval "${HUE_ROUTING__SOME[@]}"; }; index=$?

to obtain index into second table that maps 1...N integers on names of
functions to run.

All that may seem long, but almost everything is stored once and
active is only this part (example from upcoming FSH):

() { eval "${FAST_ROUTING__NONE[@]}"; }
${(0)FAST_DISPATCH__NONE[$?]}

So my dispatch table (the second one) holds code to be run like this:
local$'\0'a=b.

So 2 lines instead of big if monolith. So happy :)


On 12 June 2018 at 19:27, Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> Hello,
> a project (f-sy-h) suffers from what I call a "big-loop". I will
> rewrite it with a trick soon to check, but I though, why guess that a
> big loop, `if' or `case' are not easy for Zsh engine, while I can ask.
> Does someone know a potential marauder script constructs, more
> difficult than less difficult for Zsh execution? Is big-loop/if/case
> among them? I recall Bart mentioned that long assignment statements in
> zcompdump are pretty much slow, IIRC.
>
> On this occasion, I would ask when are functions compiled? Before
> first use, or when defining, or maybe different from these both?
> Functions in .zwc digest are of course compiled and do not need
> compilation at all (?).
>
> To bump up the stake: when sourcing a compiled script that defines
> functions, will there be a wordcode for those function definitions and
> will Zsh use it avoiding compilation?
> --
> Best regards,
> Sebastian Gniazdowski


  reply	other threads:[~2018-06-13 14:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-12 17:27 Sebastian Gniazdowski
2018-06-13 14:57 ` Sebastian Gniazdowski [this message]
2018-06-13 15:20   ` Bart Schaefer
2018-06-13 16:43     ` Sebastian Gniazdowski
2018-06-13 19:48   ` 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=CAKc7PVCatykwJ6LMeEPX3ossi29Qm-rzBErruiXDCJx9MQfQ+g@mail.gmail.com \
    --to=sgniazdowski@gmail.com \
    --cc=zsh-workers@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).