Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Is large `if' or `case' statement a potential cause of a slow-down?



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@xxxxxxxxx> 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



Messages sorted by: Reverse Date, Date, Thread, Author