🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #29

Re: Optimization by programmers

#29
From: Terje Mathisen
Date: Fri, 09 Oct 2026 14:36
89 lines
4828 bytes
Dan Cross wrote:
> In article <11a6agf$37m70$1@dont-email.me>,
> Terje Mathisen  <terje.mathisen@tmsw.no> wrote:
>> Dan Cross wrote:
>>> [snip]
>>> 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.
> 
> I thought it was worth a small experiment; here's a silly pair
> of functions:

Thanks for testing!
> 
> ```rust
> #[inline(never)]
> pub fn sum_sub_array(arr: [u32; 4], k: usize) -> u32 {
>      if k + 3 > arr.len() {
>          return 0;
>      }
>      arr[k] + arr[k + 1] + arr[k + 2]
> }
> 
> #[inline(never)]
> pub fn sum_sub_slice(sl: &[u32], k: usize) -> u32 {
>      if k + 3 > sl.len() {
>          return 0;
>      }
>      sl[k] + sl[k + 1] + sl[k + 2]
> }
> ```
> 
> If I run these in such a way that they're not totally optimized
> away and do a release build, they generate the exact same code:

That's actually what I would expect, re my post about a read-only array 
and ditto slice being the same.
> 
> ```
> 0000000000015cb0 <rust_indices::sum_sub_array>:
>     15cb0:       31 c0                   xor    %eax,%eax
>     15cb2:       48 83 fe 01             cmp    $0x1,%rsi
>     15cb6:       77 0b                   ja     15cc3 <rust_indices::sum_sub_array+0x13>
>     15cb8:       8b 44 b7 04             mov    0x4(%rdi,%rsi,4),%eax
>     15cbc:       03 04 b7                add    (%rdi,%rsi,4),%eax
>     15cbf:       03 44 b7 08             add    0x8(%rdi,%rsi,4),%eax
>     15cc3:       c3                      ret
>     15cc4:       cc                      int3
>     ...
>     15ccf:       cc                      int3
> 
> 0000000000015cd0 <rust_indices::sum_sub_slice>:
>     15cd0:       31 c0                   xor    %eax,%eax
>     15cd2:       48 83 fe 01             cmp    $0x1,%rsi
>     15cd6:       77 0b                   ja     15ce3 <rust_indices::sum_sub_slice+0x13>
>     15cd8:       8b 44 b7 04             mov    0x4(%rdi,%rsi,4),%eax
>     15cdc:       03 04 b7                add    (%rdi,%rsi,4),%eax
>     15cdf:       03 44 b7 08             add    0x8(%rdi,%rsi,4),%eax
>     15ce3:       c3                      ret
>     15ce4:       cc                      int3
>     ...
>     15cef:       cc                      int3
> ```
> 
> Perhaps it's something about the complexity of your function
> that exceed some sort of threshold for the optimizer.
> 

Quite possibly so...

Terje

-- 
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"
Technical Details
Message-ID: <11aau61$sbtb$2@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> <20261007222539.000033d0@yahoo.com> <11a68mu$m4l$1@reader2.panix.com> <11a6agf$37m70$1@dont-email.me> <11a943k$fqq$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: Fri, 9 Oct 2026 16:36:48 +0200
Organization: A noiseless patient Spider
Lines: 89
Message-ID: <11aau61$sbtb$2@dont-email.me>
References: <1177fdi$26ea4$1@dont-email.me>
 <20261007222539.000033d0@yahoo.com> <11a68mu$m4l$1@reader2.panix.com>
 <11a6agf$37m70$1@dont-email.me> <11a943k$fqq$1@reader2.panix.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 09 Oct 2026 14:36:50 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="929707"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/ZR/7sN++D2BD4WOrieBySfFhVU3RyB2QOyQaQ9Cq81Q=="; posting-host="49f5b616d83d3bf1e60aecc3180ec584"
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:mJZsr1Yvsdriu/uzAlMoKmFLwyI= sha256:fQfJZLXRm0ogWSzK0OOc/+hpUHU9WRJ/sp+vcTE87eY=
	sha1:qcKZmWuXxIuprIFcpf7bh36i9Fs= sha256:/EjmB4+9NiciwIVmS6h9c0z59YDmtVjZI37LMBsX4Us=
In-Reply-To: <11a943k$fqq$1@reader2.panix.com>