🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #27

Re: Optimization by programmers

#27
From: Dan Cross
Date: Thu, 08 Oct 2026 22:05
77 lines
3882 bytes
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:

```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:

```
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.

	- Dan C.
Technical Details
Message-ID: <11a943k$fqq$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> <20261007222539.000033d0@yahoo.com> <11a68mu$m4l$1@reader2.panix.com> <11a6agf$37m70$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: Thu, 8 Oct 2026 22:05:40 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <11a943k$fqq$1@reader2.panix.com>
References: <1177fdi$26ea4$1@dont-email.me> <20261007222539.000033d0@yahoo.com> <11a68mu$m4l$1@reader2.panix.com> <11a6agf$37m70$1@dont-email.me>
Injection-Date: Thu, 8 Oct 2026 22:05:40 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
	logging-data="16218"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)