What LDC flags should be used to get the fastest executable on Windows?

Preetpal preetpal.sohal at gmail.com
Sat Mar 6 10:51:35 UTC 2021


On Saturday, 6 March 2021 at 09:07:05 UTC, Imperatorn wrote:
> On Saturday, 6 March 2021 at 06:29:11 UTC, Preetpal wrote:
>> I was wondering how to get the fastest binary using the LDC 
>> compiler.
>>
>> I am currently using the following command to compile my 
>> program:
>> ldc2 -O3 -release -mcpu=native -flto=full 
>> -defaultlib=phobos2-ldc-lto,druntime-ldc-lto -m64 -betterC 
>> -static main.d
>>
>> Is the -defaultlib command might be redundant if the betterC 
>> flag is being used?
>>
>> This is probably pretty silly but the program I am trying to 
>> optimize is this one 
>> (https://gist.github.com/preetpalS/d2482d6ec91eb8147e6cff43ab197ed5). The program runs on each keystroke so I would like to optimize it.
>
> There's not much going on in the code there. Where are you 
> experiencing problems? Have you profiled it?

There is an issue with the code where the running program very 
infrequently misses when the SHIFT key is released. As this 
program keeps track of what modifier keys are currently pressed 
(as it is supposed keep track of all key presses and key 
releases), this can cause the wrong keyboard shortcut to be 
triggered.

This could be a performance related problem as the hook that this 
program installs in Windows (WH_KEYBOARD_LL) supposedly has a 
default timeout of 300 milliseconds 
(https://www.autohotkey.com/docs/commands/_IfTimeout.htm).

As it is small program, I re-implemented it in C 
(https://gist.github.com/preetpalS/81405cd78ade738034cfa6d49e2a4202) to see if it could reduce the problem I was seeing. Based on my observations it did reduce the problem but it did not eliminate it. This led me to believe that the issue I was seeing in the D version was performance-related.

I added additional compiler flags to LDC2 and recompiled the 
program and I do think there has been an improvement (instances 
of shift key releases not being registered have been reduced 
based on my observations).
- Before I was compiling with: ldc2 -O3 -release -m64 -betterC 
-static main.d
- Currently I am compiling with: ldc2 -O3 -release -mcpu=native 
-flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto -m64 
-betterC -static main.d

I have not profiled the code yet as I am unsure of how to do so. 
I was thinking of adding a global counter (int or something) in 
the program in that would be incremented whenever the hook was 
fired and adding an error count that is incremented whenever a 
key press or release event for a modifier key does not change the 
tracked state but if the program was actually having performance 
issues this additional code would probably change the performance 
characteristics of the code anyways.


More information about the digitalmars-d-ldc mailing list