🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #15

Re: Optimization by programmers

#15
From: 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.
Technical Details
Message-ID: <11a68mu$m4l$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> <20261007222539.000033d0@yahoo.com>

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 20:05:50 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <11a68mu$m4l$1@reader2.panix.com>
References: <1177fdi$26ea4$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com> <11a024r$f52$1@reader2.panix.com> <20261007222539.000033d0@yahoo.com>
Injection-Date: Wed, 7 Oct 2026 20:05:50 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
	logging-data="22677"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)