Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #299: The data on your hard drive is out of balance.


comp / comp.unix.shell / [ksh93u+m] Performance warning message

SubjectAuthor
* [ksh93u+m] Performance warning messageJanis Papanagnou
`* Re: [ksh93u+m] Performance warning messageJanis Papanagnou
 `* Re: [ksh93u+m] Performance warning messageMartijn Dekker
  `- Re: [ksh93u+m] Performance warning messageJanis Papanagnou

1
Subject: [ksh93u+m] Performance warning message
From: Janis Papanagnou
Newsgroups: comp.unix.shell
Organization: A noiseless patient Spider
Date: Fri, 25 Oct 2024 04:46 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: [ksh93u+m] Performance warning message
Date: Fri, 25 Oct 2024 06:46:05 +0200
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <vff7qe$31ohp$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 25 Oct 2024 06:46:07 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b0051abdce59c21a789bf51cbdb0a4ac";
logging-data="3203641"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ACwe3I2GFfLVaMMikXMNn"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:pzRamMPWNmJCKkvjhmpEHu+W5Ho=
X-Mozilla-News-Host: news://news.eternal-september.org:119
X-Enigmail-Draft-Status: N1110
View all headers

For this command

typeset -i indents=$(( level * indent_factor )) ###

with two integer variables, 'level' and 'indent_factor', declared
I'm getting this nice warning message

warning: line 28: indents=$(( level * indent_factor ))
is slower than ((indents= level * indent_factor ))

I thought (to avoid the warning) I'd have to split the lines like

typeset -i indents
(( indents = level * indent_factor ))

but I noticed that we can also write

(( typeset -i indents = level * indent_factor ))

(I wasn't aware about the 'typeset' command possible in arithmetic
expressions.)

Though I wonder why Ksh doesn't silently optimize the '###' marked
code since Ksh optimizes so many less obvious and much more complex
things.

Janis

Subject: Re: [ksh93u+m] Performance warning message
From: Janis Papanagnou
Newsgroups: comp.unix.shell
Organization: A noiseless patient Spider
Date: Mon, 28 Oct 2024 00:21 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: [ksh93u+m] Performance warning message
Date: Mon, 28 Oct 2024 01:21:17 +0100
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <vfmldv$ll5i$1@dont-email.me>
References: <vff7qe$31ohp$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 28 Oct 2024 01:21:19 +0100 (CET)
Injection-Info: dont-email.me; posting-host="f95b327adceab14f91e2f2628e068817";
logging-data="709810"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/IaWDzdOXDUgXmYKd9tAKl"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:5tQwAOaximFHkg0UpY01lm15F6Q=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <vff7qe$31ohp$1@dont-email.me>
View all headers

On 25.10.2024 06:46, Janis Papanagnou wrote:
> For this command
>
> typeset -i indents=$(( level * indent_factor )) ###
>
> with two integer variables, 'level' and 'indent_factor', declared
> I'm getting this nice warning message
>
> warning: line 28: indents=$(( level * indent_factor ))
> is slower than ((indents= level * indent_factor ))
>
> I thought (to avoid the warning) I'd have to split the lines like
>
> typeset -i indents
> (( indents = level * indent_factor ))
>
> but I noticed that we can also write
>
> (( typeset -i indents = level * indent_factor ))
>
> (I wasn't aware about the 'typeset' command possible in arithmetic
> expressions.)

Oops! - That's wrong. - I had just tested that only with 'ksh -n'.

ksh -n had produced the above performance warning message, but it
did not produce an error message for the (( typeset ... )) syntax,
which I saw just now when invoking the shell regularly on the code.

This is really odd (shell-)behavior! (Reporting a warning but not
an error.) - Looks like a bug to me.[*]

So we have to either use the "non-performant" syntax marked '###'
or separate the statements on two lines.

>
> Though I wonder why Ksh doesn't silently optimize the '###' marked
> code since Ksh optimizes so many less obvious and much more complex
> things.
>
> Janis

[*] The man page says for option '-n':
"Read commands and check them for syntax errors, [...]"
So the warning message is just an undocumented feature for free?
And the 'typeset' inside the arithmetic command is syntactically
okay and deserves no diagnostic message?

Subject: Re: [ksh93u+m] Performance warning message
From: Martijn Dekker
Newsgroups: comp.unix.shell
Date: Sun, 3 Nov 2024 17:29 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder2.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: martijn@inlv.demon.nl (Martijn Dekker)
Newsgroups: comp.unix.shell
Subject: Re: [ksh93u+m] Performance warning message
Date: Sun, 3 Nov 2024 17:29:02 +0000
Lines: 40
Message-ID: <lopq6uF6mbkU1@mid.individual.net>
References: <vff7qe$31ohp$1@dont-email.me> <vfmldv$ll5i$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net kF3Wm/ACUhAs65JM6YEl7wUJsXVmdLfrrihZf0sQjDfc3OCFw=
Cancel-Lock: sha1:+UEMdbYNFDaAF9k7hHNWkAz7NSc= sha256:KqUEBJbYMwvRhIWt01qSScUKBUZkIeL9sHjNU2qqnzo=
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <vfmldv$ll5i$1@dont-email.me>
View all headers

Op 28-10-2024 om 00:21 schreef Janis Papanagnou:
> On 25.10.2024 06:46, Janis Papanagnou wrote:
[...]
>> (( typeset -i indents = level * indent_factor ))
>>
>> (I wasn't aware about the 'typeset' command possible in arithmetic
>> expressions.)
>
> Oops! - That's wrong. - I had just tested that only with 'ksh -n'.
>
> ksh -n had produced the above performance warning message, but it
> did not produce an error message for the (( typeset ... )) syntax,
> which I saw just now when invoking the shell regularly on the code.
>
> This is really odd (shell-)behavior! (Reporting a warning but not
> an error.) - Looks like a bug to me.[*]

C-style shell arithmetic is treated as mostly separate from the shell language
proper. As far as the shell language is concerned, an arithmetic expansion or
command is just a glorified quoted string. So, they are parsed separately from
the shell language, and both parsing and executing happens at runtime. This
means shell arithmetic syntax errors can only be detected while executing the
script, i.e., not when using noexec. All the POSIX-based shells are the same
in that, because they all copied ksh's design for shell arithmetic.

> [*] The man page says for option '-n':
> "Read commands and check them for syntax errors, [...]"
> So the warning message is just an undocumented feature for free?

Yes. The -n (noexec) option activates a (very limited) linter mode that
produces certain warnings. The arithmetic-related linter warnings don't look
at the arithmetic expression itself at all, but only at the shell grammar
context in which an arithmetic expression is used.

--
|| modernish -- harness the shell
|| https://github.com/modernish/modernish
||
|| KornShell lives!
|| https://github.com/ksh93/ksh

Subject: Re: [ksh93u+m] Performance warning message
From: Janis Papanagnou
Newsgroups: comp.unix.shell
Organization: A noiseless patient Spider
Date: Sun, 3 Nov 2024 18:59 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: [ksh93u+m] Performance warning message
Date: Sun, 3 Nov 2024 19:59:04 +0100
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <vg8h5p$g55c$1@dont-email.me>
References: <vff7qe$31ohp$1@dont-email.me> <vfmldv$ll5i$1@dont-email.me>
<lopq6uF6mbkU1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 03 Nov 2024 19:59:05 +0100 (CET)
Injection-Info: dont-email.me; posting-host="de6bcf366341b149f37f8eb7d0dc5bfc";
logging-data="529580"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+tDDbyF1bqxjp6+hrb486"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:0vSDpIs9AGa7YPGN6wWRl6VfqOM=
In-Reply-To: <lopq6uF6mbkU1@mid.individual.net>
X-Enigmail-Draft-Status: N1110
View all headers

On 03.11.2024 18:29, Martijn Dekker wrote:
> Op 28-10-2024 om 00:21 schreef Janis Papanagnou:
>> On 25.10.2024 06:46, Janis Papanagnou wrote:
>> [...]
>
> C-style shell arithmetic is treated as mostly separate from the shell
> language proper. As far as the shell language is concerned, an
> arithmetic expansion or command is just a glorified quoted string. So,
> they are parsed separately from the shell language, and both parsing and
> executing happens at runtime. This means shell arithmetic syntax errors
> can only be detected while executing the script, i.e., not when using
> noexec. All the POSIX-based shells are the same in that, because they
> all copied ksh's design for shell arithmetic.
>
>> [*] The man page says for option '-n':
>> "Read commands and check them for syntax errors, [...]"
>> So the warning message is just an undocumented feature for free?
>
> Yes. The -n (noexec) option activates a (very limited) linter mode that
> produces certain warnings. The arithmetic-related linter warnings don't
> look at the arithmetic expression itself at all, but only at the shell
> grammar context in which an arithmetic expression is used.
>

Thanks for the background information. - I still find it strange
that structural syntactic errors are not reported but semantical
optimization warnings are. (And, as said, strange that it's not
just optimized away if the shell is even able to detect it.)

I would not have noticed if I hadn't used ksh's option -n that I
anyway use only rarely, so this discrepancy won't have an effect
on my work in practice. - Just thought it might be of interest. -
So never mind.

Concerning the original expression[*] - given that the processing
time is dominated by called processes and I/O and not by built-in
primitive math - I had kept the construct in its "non-performant"
form due to its best readability as an initialized definition.

But it bites in my eyes seeing that 'typeset -i' is declaring an
integer. So pondering about that, and remembering that Kornshell
allows integer variable use in integer contexts without using the
'$' string-value dereferencing, I just tried

typeset -i indents=level*indent_factor

and, what shall I say, it works! And it doesn't trigger a warning
if the shell is called with option '-n'.

Janis

[*] typeset -i indents=$(( level * indent_factor ))

1

rocksolid light 0.9.8
clearnet tor