Performance improvements for D / DMD compiler.
Bill Baxter
dnewsgroup at billbaxter.com
Sat Jan 20 06:19:03 PST 2007
Chris Nicholson-Sauls wrote:
> Bill Baxter wrote:
>> 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.
>>
>> Very true. What I'm after is really a concise way to both
>> a) document to readers of the code that it won't change and
>> b) get the compiler to zap me if I accidentally try to modify it.
>>
>>>> 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
>>
>> And none of those accomplishes a or b.
>>
>> --bb
>
> Just a thought: B) can be accomplished with an Interface that only
> exposes gettors.
> Granted, though, that 1) it means defining everything you want to be
> read-only as a
> property, but these would be optimized away by inlining anyhow, 2) you
> have to write an
> Interface (which isn't a big deal, IMHO, but I know some people can be
> annoyed with them).
> Also, you would want to make the implementor class and all methods
> 'final' to avoid
> potential virtuality overhead.
>
> Something akin to:
> <code>
> interface IFoo {
> int alpha () ;
> int beta () ;
> }
>
> final class Foo : IFoo {
> private {
> int m_alpha ;
> int m_beta ;
> }
>
> final int alpha () { return m_alpha ; }
> final int beta () { return m_beta ; }
>
> final int alpha (int x) { return m_alpha = x ; }
> final int beta (int x) { return m_beta = x ; }
> }
>
> void somefunc (IFoo obj) {
> // do stuff with obj.alpha and obj.beta
> }
>
> auto myfoo = new Foo;
> somefunc(myfoo);
> </code>
>
> -- Chris Nicholson-Sauls
That's pretty clever, but it's an awful lot superfluous cruft for
something that should just be as simple as
struct Foo
{
int m_alpha, m_beta;
}
Reminds me of a quote I heard once about knitting needles and eyeballs.
--bb
More information about the Digitalmars-d
mailing list