🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

11 total messages Started by Michael S Wed, 07 Oct 2026 19:25
Re: Optimization by programmers
#12
Author: Michael S
Date: Wed, 07 Oct 2026 19:25
47 lines
2914 bytes
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




Re: Optimization by programmers
#15
Author: Dan Cross
Date: Wed, 07 Oct 2026 20:05
54 lines
3025 bytes
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.

	- Dan C.
Re: Optimization by programmers
#16
Author: Michael S
Date: Wed, 07 Oct 2026 20:25
65 lines
3758 bytes
On Wed, 7 Oct 2026 20:05:50 -0000 (UTC)
cross@spitfire.i.gajendra.net (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.

That is not different from any other bound-checked language that has
optimizing compiler.

>  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.
> 
> 	- Dan C.
> 

Re: Optimization by programmers
#18
Author: 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"
Re: Optimization by programmers
#19
Author: EricP
Date: Wed, 07 Oct 2026 20:49
11 lines
1579 bytes
On 2026-Oct-07 15:25, Michael S wrote:
> https://doc.rust-lang.org/book/ch03-02-data-types.html?highlight=array#invalid-array-element-access

Does Rust allow zero length arrays, vectors, strings and slices?
Ada calls these a null slice.
In DEC Fortran 77 a zero length slice was a bounds check error,
which was really irritating because in string manipulation they happen a lot.
If so, how does one specify a null slice in Rust?
I couldn't find any definition in that doc.

Re: Optimization by programmers
#20
Author: EricP
Date: Thu, 08 Oct 2026 02:13
54 lines
2857 bytes
On 2026-Oct-07 16:49, EricP wrote:
> On 2026-Oct-07 15:25, Michael S wrote:
>> https://doc.rust-lang.org/book/ch03-02-data-types.html?highlight=array#invalid-array-element-access
> 
> Does Rust allow zero length arrays, vectors, strings and slices?
> Ada calls these a null slice.
> In DEC Fortran 77 a zero length slice was a bounds check error,
> which was really irritating because in string manipulation they happen a lot.
> If so, how does one specify a null slice in Rust?
> I couldn't find any definition in that doc.
> 
> 

I asked the Google AI "does programming language rust support
zero length array slices" and it said:

Yes, Rust fully supports zero-length (empty) array slices.
A slice in Rust ([T]) represents a dynamically-sized view into
a contiguous sequence of elements. Because the length is evaluated
at runtime, a slice can safely have a length of 0.

How to Create an Empty Slice

You can create or obtain a zero-length slice in Rust in several ways:

 From an empty array or vector:

let empty_array: [i32; 0] = [];
let slice: &[i32] = &empty_array; // A zero-length slice

Using a range that yields no elements:

let numbers = [1, 2, 3, 4];
let empty_slice = &numbers[2..2]; // Length is 0

Directly via standard library constants:

let empty_slice: &[i32] = &[];

Memory Layout of a Zero-Length Slice

Under the hood, a slice reference (&[T]) is a fat pointer
consisting of two components:

1. A pointer to the data.
2. The length of the slice (usize).

For a zero-length slice, the length component is 0. To avoid using
a null pointer (which Rust references cannot be), the data pointer
component points to a well-aligned, non-null "dangling" pointer
address (specifically NonNull::dangling()), making it completely
safe and overhead-free.

Re: Optimization by programmers
#22
Author: Thomas Koenig
Date: Thu, 08 Oct 2026 05:46
14 lines
1877 bytes
EricP <ThatWouldBeTelling@thevillage.com> schrieb:
> On 2026-Oct-07 15:25, Michael S wrote:
>> https://doc.rust-lang.org/book/ch03-02-data-types.html?highlight=array#invalid-array-element-access
>
> Does Rust allow zero length arrays, vectors, strings and slices?
> Ada calls these a null slice.
> In DEC Fortran 77 a zero length slice was a bounds check error,

Modern Fortran does allow it, and yes, it is very convenient.

-- 
This USENET posting was made without artificial intelligence,
artificial impertinence, artificial arrogance, artificial stupidity,
artificial flavorings or artificial colorants.
Re: Optimization by programmers
#23
Author: EricP
Date: Thu, 08 Oct 2026 16:05
68 lines
3728 bytes
On 2026-Oct-07 15:25, Michael S 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

The reference is less see-spot-run

https://doc.rust-lang.org/stable/reference/

Looking at the rules for type casting:

https://doc.rust-lang.org/stable/reference/expressions/operator-expr.html#numeric-cast

Some initial thoughts:

There are no value range checks between cast types, just bit-wise reinterpretation
to the new type with possible sign/zero extension or truncation for size change.

They don't define the order in which those two operations, type and size change, occur.
If one casts an i8 into a u16 it doesn't say whether the value is sign extended
to i16 first then type changed to u16, or type changed to u8 first then zero
extended to u16.

Since an array index is defined as type usize, if the index expression is signed
then it will be bit-wise reinterpreted as unsigned, and possibly turn a negative
value into a large positive usize value. If bounds checks are disabled for
production builds then you get exactly the same array overflow as in C.

Re: Optimization by programmers
#27
Author: 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.
Re: Optimization by programmers
#28
Author: Dan Cross
Date: Thu, 08 Oct 2026 22:55
127 lines
5351 bytes
In article <jbPxS.600$MBQb.166@fx08.iad>,
EricP  <ThatWouldBeTelling@thevillage.com> wrote:
>[snip]
>The reference is less see-spot-run
>
>https://doc.rust-lang.org/stable/reference/
>
>Looking at the rules for type casting:
>
>https://doc.rust-lang.org/stable/reference/expressions/operator-expr.html#numeric-cast
>
>Some initial thoughts:
>
>There are no value range checks between cast types, just bit-wise reinterpretation
>to the new type with possible sign/zero extension or truncation for size change.
>
>They don't define the order in which those two operations, type and size change, occur.
>If one casts an i8 into a u16 it doesn't say whether the value is sign extended
>to i16 first then type changed to u16, or type changed to u8 first then zero
>extended to u16.

They do, but it requires looking at _two_ rules from that list.

The first,

|Casting between two integers of the same size (e.g. i32 -> u32)
|is a no-op (Rust uses 2’s complement for negative values of
|fixed integers)

and the third,

|Casting from a smaller integer to a larger integer (e.g. u8 ->
|u32) will
|
| * zero-extend if the source is unsigned
| * sign-extend if the source is signed

Note that the second of these, the third rule on the list,
mentions sign extension if the _source_ type is signed; the
signedness of the destination is not relevant.

Taken together, these imply that the source will be
sign-extended and then the conversion to unsigned will be
treated as a noop: rule #1 does not apply first because it is
specific to conversions between sign/unsigned integers of the
_same width_; so we start with rule #3, sign-extend, and _then_
we may apply rule #1.

For a negative value of a source type that is smaller than an
unsigned destination type, the result will be an unsigned value
in the upper half of the destination type's range.

term% cat cast.rs
fn main() {
    let n: i8 = -2;
    println!("-2 as u16 = {}", n as u16);
}
term% rustc cast.rs
term% ./cast
-2 as u16 = 65534
term%

If you wanted to force specific behavior, you could use multiple
casts (e.g., `n as u8 as u16`).

>Since an array index is defined as type usize, if the index expression
>is signed then it will be bit-wise reinterpreted as unsigned,

No, if the index expression is signed, it will be a compile-time
error since usize is, by definiton, unsigned, and usize is the
only permissible index type.  Rust is statically typed, and
there are no implicit conversions that factor here.

>and possibly turn a negative value into a large positive usize
>value.

In order for this to happen, one would have to use an explicit
cast to make the negative value a `usize` first.

A fair followup question is, "suppose I do a comparison against
a signed type, but don't account for zero, and then convert it
to usize; could the optimizer remove the bounds checks?"  For
example, this:

```rust
#[inline(never)]
pub fn get_a_number(k: i32) -> i32 {
    const ARR: [i32; 10] = [500, 1, 200, 3, 410, 5, 37, 7, 8, -5];
    if k < 10 { ARR[k as usize] } else { 0 }
}
```

When built with optimization enabled and linked with a simple
driver, panic's at runtime trying to use a negative value cast
to a (large) positive value as an index.  On the other hand, if
I modify it to check the lower bound on the array, that is
optimized away.  So this version,

```rust
#[inline(never)]
pub fn get_a_number(k: i32) -> i32 {
    const ARR: [i32; 10] = [500, 1, 200, 3, 410, 5, 37, 7, 8, -5];
    if 0 <= k && k < 10 { ARR[k as usize] } else { 0 }
}
```
yields this object code on x86_64:
```
0000000000014ad0 <rbounds::get_a_number>:
   14ad0:       31 c0                   xor    %eax,%eax
   14ad2:       83 ff 0a                cmp    $0xa,%edi
   14ad5:       73 0c                   jae    14ae3 <rbounds::get_a_number+0x13>
   14ad7:       89 f8                   mov    %edi,%eax
   14ad9:       48 8d 0d 04 19 ff ff    lea    -0xe6fc(%rip),%rcx        # 63e4 <__abi_tag+0x60e8>
   14ae0:       8b 04 81                mov    (%rcx,%rax,4),%eax
   14ae3:       c3                      ret
   14ae4:       cc                      int3
   ...
   14aef:       cc                      int3
```

>If bounds checks are disabled for
>production builds then you get exactly the same array overflow as in C.

See above.

	- Dan C.
Re: Optimization by programmers
#29
Author: 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"
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads