Passing array as const slows down code?

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 27 11:26:52 PDT 2012


On Fri, 27 Apr 2012 13:23:46 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On Friday, April 27, 2012 11:18:26 Steven Schveighoffer wrote:
>> const should not affect code generation *at all*, except for name  
>> mangling
>> (const MyStruct is a different type from MyStruct), and generating an
>> extra TypeInfo for const MyStruct and const MyStruct[]. Const is purely  
>> a
>> compile-time concept.
>
> Thanks to the fact that const is transitive and that it's illegal to  
> cast it
> away to use mutation, const _can_ affect code optimizations, but I don't  
> know
> exactly under what circumstances it does in the current compiler.

No, it can't.  There can easily be another non-const reference to the same  
data.  Pure functions can make more assumptions, based on the types, but  
it would be a very complex determination in the type system to see if two  
parameters alias the same data.

Real optimization benefits come into play when immutable is there.

-Steve


More information about the Digitalmars-d-learn mailing list