🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #7

Re: Optimization by programmers

#7
From: Dan Cross
Date: Wed, 07 Oct 2026 13:28
57 lines
3184 bytes
In article <11a3ofn$2ak3s$1@dont-email.me>,
Terje Mathisen  <terje.mathisen@tmsw.no> wrote:
>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.
>
>That's what I assumed, except I check my assumptions by godbolt'ing my 
>code, using -O -C "target_cpu=native" to get a bleeding edge optimized 
>build, and that's where I saw a loop which started with
>
>   while pos+7 < input.len() {
>     ... here a bunch of accesses to input[p], input[p+1]...input[p+6]
>   }
>
>Every one of those secodnary accesses were very obviously tested against 
>the input length (stored in a register for the duration)
>
>I considered reporting this as a very clear performance bug!

I wonder if what you saw was actually loop unrolling.  I just
tried something similar, albeit with a very simple sum in the
loop body, and don't see a bunch of extraneous bounds checks.

I do wonder, though, whether you'd get better object code using
`input.iter().chunks(n)` or `.windows(n)`.

	- Dan C.
Technical Details
Message-ID: <11a5hd5$4qb$1@reader2.panix.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
References: <1177fdi$26ea4$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com> <11a024r$f52$1@reader2.panix.com> <11a3ofn$2ak3s$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
From: cross@spitfire.i.gajendra.net (Dan Cross)
Newsgroups: comp.arch
Subject: Re: Optimization by programmers
Date: Wed, 7 Oct 2026 13:28:05 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <11a5hd5$4qb$1@reader2.panix.com>
References: <1177fdi$26ea4$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com> <11a024r$f52$1@reader2.panix.com> <11a3ofn$2ak3s$1@dont-email.me>
Injection-Date: Wed, 7 Oct 2026 13:28:05 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
	logging-data="4939"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)