🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #18

Re: Optimization by programmers

#18
From: Terje Mathisen
Date: Wed, 07 Oct 2026 20:36
69 lines
4197 bytes
Dan Cross wrote:
> In article <20261007222539.000033d0@yahoo.com>,
> Michael S  <already5chosen@yahoo.com> wrote:
>> On Mon, 5 Oct 2026 11:36:59 -0000 (UTC)
>> cross@spitfire.i.gajendra.net (Dan Cross) wrote:
>>
>>> In article <86h5j5pky9.fsf@linuxsc.com>,
>>> Tim Rentsch  <tr.17687@z991.linuxsc.com> wrote:
>>>> Terje Mathisen <terje.mathisen@tmsw.no> writes:
>>>>   
>>>>> Tim Rentsch wrote:
>>>>>   
>>>>>> Terje Mathisen <terje.mathisen@tmsw.no> writes:
>>>>>>   
>>>>>>> Tim Rentsch wrote:
>>>> [...]
>>>>>>>> I favor a type equivalent to size_t for use as array index
>>>>>>>> variables.
>>>>>>>
>>>>>>> In Rust, the only acceptable array index is of type "usize",
>>>>>>> which is effectively the same as u64 on most platforms these
>>>>>>> days, but does not need to be so:  On a 32-bit target, it would
>>>>>>> be the same as u32.
>>>>>>>
>>>>>>> So effectively very similar to size_t.
>>>>>>
>>>>>> Maybe that limitation works okay in Rust.  In C sometimes it's
>>>>>> important to allow a signed value for indexing.  Probably 99% of
>>>>>> the time size_t is good, but not always.
>>>>>
>>>>> The fact that array indices are unsigned is one of the more crucial
>>>>> underpinnings of Rust safety.  Even more importantly, all such
>>>>> indices are verified against the actual array upper boundary.
>>>>> [...]
>>>>
>>>> It's nice that array index values are checked against the array
>>>> bounds.
>>>
>>> Terje was actually incorrect here; bounds checks are added in
>>> debug mode, but are (usually) removed in optimized builds.
>>>
>>
>> Are you sure?
>> Methinks The Book states otherwise:
>> https://doc.rust-lang.org/book/ch03-02-data-types.html?highlight=array#invalid-array-element-access
> 
> If the array length and index are known ahead of time, or can be
> statically proven to be valid, then yes, the checks are (likely)
> removed in release mode as part of the ordinary optimization
> passes.  In the case where one has already asserted that
> `p + n < arr.len()`, then I would expect any index `arr[p + k]`
> to omit the bounds check, for k < n.

That's exactly what I expected when I wrote my code the way I did. I 
know that Java specifically handles this situation, lifting any array 
bounds checking out of inner loops if possible.

As that Godbolt link shows, my expectation was wrong.

OTOH, for such a small program having 6 extra branches, none of them 
ever taken, is not likely to add any cycles to the program latency since 
current CPUs have more than enough issue ports to be able to handle 
these branches while processing the real code.

Terje

-- 
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"
Technical Details
Message-ID: <11a6agf$37m70$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <1177fdi$26ea4$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com> <11a024r$f52$1@reader2.panix.com> <20261007222539.000033d0@yahoo.com> <11a68mu$m4l$1@reader2.panix.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Terje Mathisen <terje.mathisen@tmsw.no>
Newsgroups: comp.arch
Subject: Re: Optimization by programmers
Date: Wed, 7 Oct 2026 22:36:30 +0200
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <11a6agf$37m70$1@dont-email.me>
References: <1177fdi$26ea4$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com>
 <11a024r$f52$1@reader2.panix.com> <20261007222539.000033d0@yahoo.com>
 <11a68mu$m4l$1@reader2.panix.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 07 Oct 2026 20:36:34 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3397856"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX186yqgcOUW/zC2JQV5mUA68rGJhBiRvo5wdte+P98pw6g=="; posting-host="c17f588ef9b5adf5026e3d3006504c1f"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101
 Firefox/140.0 SeaMonkey/2.53.24
Cancel-Lock: sha1:UC3i20Aiwyk/pRpPS3R0EJiS/DQ= sha256:gp/gwLBJg0OBtskLyugd9Z4ur/L2WmUQdY6rsN87/js=
	sha1:MlJU1Di80VhAiSJJuthDBI1Ivbs= sha256:XFIaWiH/6jWXc7LuZC+kAOzFjaxHtNTvqOisyPb5qOQ=
In-Reply-To: <11a68mu$m4l$1@reader2.panix.com>