Requesting Help with Optimizing Code

Max Haughton maxhaton at gmail.com
Thu Apr 8 18:26:24 UTC 2021


On Thursday, 8 April 2021 at 17:00:31 UTC, Kyle Ingraham wrote:
> On Thursday, 8 April 2021 at 03:27:12 UTC, Max Haughton wrote:
>> Are you making the compiler aware of your machine? Although 
>> the obvious point here is vector width (you have AVX-512 from 
>> what I can see, however I'm not sure if this is actually a win 
>> or not on Skylake W), the compiler is also forced to use a 
>> completely generic scheduling model which may or may not yield 
>> good code for your procesor. For LDC, you'll want 
>> `-mcpu=native`. Also use cross module inlining if you aren't 
>> already.
>
> I haven't tried this yet but will give it a go. Ideally I'd 
> want performance independent of compilers but could sprinkle 
> this into the build config for when LDC is used.
>
>> I also notice in your hot code, you are using function 
>> contracts: When contracts are being checked, LDC actually does 
>> use them to optimize the code, however in release builds when 
>> they are turned off this is not the case. If you are happy to 
>> assume that they are true in release builds (I believe you 
>> should at least), you can give the compiler this additional 
>> information via the use of an intrinsic or similar (with LDC I 
>> just use inline IR, on GDC you have __builtin_unreachable()). 
>> LDC *will* assume asserts in release builds as far as I have 
>> tested, however this still invokes a branch (just not to a 
>> proper assert handler)...
>
> Thanks for shining light on a level of optimization that I did 
> not know existed. I've got a lot of learning to do in this 
> area. Do you have any resources you'd recommend?

GDC has equivalent options - dmd does not have any equivalent 
however.

As for resources in this particular area there isn't really an 
exact science to it beyond not assuming that the compiler can 
read your mind. These optimizations can be quite ad-hoc i.e. the 
propagation of this information through the function is not 
always intuitive (or possible).

One trick is to declare a method as inline true, then assume 
something in that method - the compiler will then assume (say) 
that the return value is aligned or whatever. But proceed with 
caution.


More information about the Digitalmars-d mailing list