Quick help on version function parameter

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 19 02:19:09 PST 2015


On Thursday, 19 February 2015 at 01:39:19 UTC, Jonathan Marler 
wrote:
> On Wednesday, 18 February 2015 at 23:49:26 UTC, Adam D. Ruppe 
> wrote:
>> I'd write a foo_impl which always takes a parameter. Then do 
>> the versioned foo() functions which just forward to it:
>>
>> void foo_impl(int x) { long function using x here }
>>
>> version(globals) {
>>   int x;
>>   void foo() {
>>      foo_impl(x);
>>   }
>> } else {
>>   void foo(int x) { foo_impl(x); }
>> }
>>
>> Minimal duplication with both interfaces.
>
> That kinda defeats the purpose of why I want this.  It's for 
> performance reasons.  I need one version to have NO arguments 
> and one version to have one ref argument.

If it's a `ref` argument (your original example doesn't have 
one), are you sure there will be a performance problem?

Anyway, how about using a template and an alias, respectively:

     void foo_impl(ref ExpensiveStruct x) { ... }

     version(globals) {
         ExpensiveStruct x;
         void foo()() {
             foo_impl(x);
         }
     } else {
         alias foo = foo_impl;
     }

That way, you'd force `foo` to be instantiated at the call site, 
making it more likely to be inlinable. In the non-global case, 
there will be no overhead at all because of the `alias`.

But, as always for things related to performance: profile first, 
then optimize.


More information about the Digitalmars-d-learn mailing list