🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #9

Re: Optimization by programmers

#9
From: Terje Mathisen
Date: Wed, 07 Oct 2026 14:43
77 lines
4336 bytes
Dan Cross wrote:
> 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.

Not unrolling, the code is here:

https://godbolt.org/z/YnY5G1K49

The interesting part starts at line 28/29 .LBB0_02 in the asm window 
where you can see (starting at line 37) a bunch of cmp r8,rsi / jae 
<panic> sequences.


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

I guess I can try this, I'm guessing chunks(7) will error out if less 
than 7 elements are available?

The code is from Advent of Code 2024 day 3,
https://github.com/TerjeWiigMathisen/aoc/tree/master/2024/day03

Terje


-- 
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"
Technical Details
Message-ID: <11a5lqo$2v5bq$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> <11a3ofn$2ak3s$1@dont-email.me> <11a5hd5$4qb$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 16:43:34 +0200
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <11a5lqo$2v5bq$1@dont-email.me>
References: <1177fdi$26ea4$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com>
 <11a024r$f52$1@reader2.panix.com> <11a3ofn$2ak3s$1@dont-email.me>
 <11a5hd5$4qb$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 14:43:37 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3118458"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18JtLrCchUMGtEqn19WIsU2j0YQRU/75ggoKOkjBK/fgQ=="; 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:xnFN617biRPkIxAaO9SDwHRirZE= sha256:beYLHke9y5B0JymTmAKKPAiriThbN1BW3w1jgYoPH0Q=
	sha1:sFR/kuOJEUuXuZF0cys7B4ru0lE= sha256:d0+MXVsSy9iW8Fw48DzAhR6VugbKyUlQ+7Xr1x8i98s=
In-Reply-To: <11a5hd5$4qb$1@reader2.panix.com>