Performance improvements for D / DMD compiler.

James Dennett jdennett at acm.org
Sat Jan 20 12:26:14 PST 2007


Walter Bright wrote:
> Bill Baxter wrote:
>> The question is how can we write functions in D that take big structs
>> as parameters in such a way that it is:
>> A) efficient and
>> B) guaranteed not to change the caller's value
>>
>> In C++ the answer is const LargeStruct&.
> 
> C++ doesn't *guarantee* it won't change. There are at least two legal
> ways to do it.

This is an oft-repeated point, but it also misses the users'
perspective.  It's important from an implementors perspective
that this use of const as a type modifier doesn't provide the
compiler with a guarantee of immutability.  Given sensible
users, that doesn't generally matter to programmers writing
code.  const documents an interface; it doesn't enforce it,
though

>> In D the answer is...
> 
> There aren't const references in D. But the 3 ways to pass it by pointer
> are:
> 
> 1) inout
> 2) take the address and use a pointer
> 3) make it a class instead

So, in D the answer is that you have to provide a wrapper
which provides no update operations?  Very heavyweight for
a simple, useful thing that C++ can express pretty well
inside the language.  Users like it; they mostly find that
it does what they like.  Java, C#, D and Felix have dropped
this valuable feature from C++, and users miss it.  const
in C++ is far from perfect, but it's much, much more useful
than having no answer other than "make a copy and maybe
the compiler will be able to optimize it away".  Sometimes
we really do want reference-to-const semantics.

Granted, this adds some complexity to the language, and
other languages have flourished without anything like the
C++ notion of const as a qualifier on pointers/references.
But it's important to recognize that this shouldn't be
dismissed by noting that const (apart from top-level
const) doesn't provide much useful guarantee to a compiler
write; it *does* provide something very useful to
programmers using a language.

-- James




More information about the Digitalmars-d mailing list