Optimizing DLLs: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
m (Removed "Function inlining" section. This setting is now enabled by default on the release configuration, so it's useless information.)
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
The Source SDK Visual Studios Projects properties and options are acceptable for many mods, but it can be made better and faster with some small changes to utilise Microsoft's optimising C++ compiler. These changes produce code better tuned for newer processors without the need to re-write anything.
{{Dead end|date=January 2024}}
 
The Source SDK Visual Studios Projects properties and options are acceptable for many mods, but it can be made better and faster with some small changes to utilize Microsoft's optimizing C++ compiler. These changes produce code better tuned for newer processors without the need to re-write anything.


These changes are implemented by added extra switches and options to the property pages of your client and server projects. You can find this by selecting ''Project'' then ''Properties'' from the menu in the Visual C++ 2003 IDE.
These changes are implemented by added extra switches and options to the property pages of your client and server projects. You can find this by selecting ''Project'' then ''Properties'' from the menu in the Visual C++ 2003 IDE.
{{note|Don't forget to add these options in both the '''client''' and '''hl''' properties}}
{{note|Don't forget to add these options for both the '''client''' and '''hl''' projects.}}


== Options ==
== Options ==


In the project properties page of your client and server, change these settings to optimise your DLL files.
In the project properties page of your client and server, change these settings to optimize your DLL files.
 
=== Disable debugging ===
Set the compiler to use the release configuration instead of the debug configuration. This will apply several stock options. Most notably, it will define the NDEBUG macro instead of the DEBUG macro, allowing the preprocessor to remove various debugging aids, such as range checks and assertions, that consumes time.


=== General ===
Another thing this does is that it switches the runtime from the debugging version to the optimized release version.


*''Whole Program Optimization'' - set to '''Yes'''.<br>Specifies that the program will be optimised across .obj boundaries.{{note|Memory requirements can be very high when compiling with ''Whole Program Optimization'' on}}
=== Target modern CPUs ===
''Code Generation'' -> ''Enable Enhanced Instruction Set'' - set to '''Advanced Vector Extensions 2 (/arch:AVX2)'''<br>The [http://msdn2.microsoft.com/en-us/library/7t5yh4fd.aspx /arch] compiler flag enables the SSE and AVX instruction set extensions of Intel and AMD processors. This determines your mod's '''''REQUIRED''''' CPU specs.<br>SSE (Streaming SIMD (Single Instruction, Multiple Data) Extensions) and AVX (Advanced Vector Extensions) allow the CPU to pack and modify large amounts of data at once, similarly to a GPU.{{note|This is a very safe optimization, as all Intel Core CPUs released since 2014 support AVX2, as well as all AMD Ryzen CPUs. This includes AMD Athlon CPUs on the Zen architecture.<br>'''''HOWEVER''''', Intel Celeron and Pentium CPUs have only supported AVX1 and AVX2 from 2021 onwards.<br>If you suspect your mod will be commonly played on extremely weak hardware such as this, consider '''Streaming SIMD Extensions 2 (/arch:SSE2)'''.}}


=== C/C++ ===
=== Linker ===
*''Optimization'' -> ''Optimize for Processor'' - set to '''Pentium 4 and Above(/G7)'''<br>The [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/optimization.asp G7] flag produces code which is optimised for Pentium 4 processors and above which, in some cases can lead to a 10% improvement in execution speed. The code ''will'' still run on older processors, just not as fast.
*''Optimization'' -> ''Enabled COMDAT Folding'' - set to '''Remove Redundant COMDATs (/OPT:ICF)'''<br>[http://msdn2.microsoft.com/en-us/library/bxwfs976(VS.80).aspx /OPT:ICF] removes redundant COMDAT symbols from the linker output.
*''Optimization'' -> ''Optimize for Windows98'' - set to '''No (/OPT:NOWIN98)'''<br>[http://msdn2.microsoft.com/en-us/library/bxwfs976(VS.80).aspx /OPT:NOWIN98] controls the section alignment in the final image. You should use this switch when building DLL's only for Windows NT, Windows 2000, Windows XP or later.


*''Code Generation'' -> ''Enable Enhanced Instruction Set'' - set to '''Streaming SIMD Extensions (/arch:SSE)'''<br>The [http://msdn2.microsoft.com/en-us/library/7t5yh4fd.aspx arch] flag enables the use of instructions found on processors that support enhanced instruction sets e.g., the SSE and SSE2 extensions of Intel 32-bit processors.{{note|Be careful with this setting as it will prevent the code running on processors which don't support these extensions}}
=== Whole Program Optimization / Link time code generation ===


*''Command Line'' -> ''Additional Options'' - add the switch '''/GL'''.<br>The [http://msdn2.microsoft.com/en-us/library/0zza0de8(VS.80).aspx GL] flag enables whole program optimization.
*''Whole Program Optimization'' - set to '''Yes'''.<br>Specifies that the program will be optimized across .obj boundaries.


=== Linker ===
{{note|Memory requirements can be very high when compiling with ''Whole Program Optimization'' on}}
*''Optimization'' -> ''Enabled COMDAT Folding'' - set to '''Remove Redundant COMDATs (/OPT:ICF)'''<br>[http://msdn2.microsoft.com/en-us/library/bxwfs976(VS.80).aspx /OPT:ICF] removes redundant COMDAT symbols from the linker output.
{{note|You should only need to use Whole Program Optimization if you built the vgui_controls.lib with the fixes.}}


*''Command Line'' -> ''Additional Options'' - add the switch '''/LTCG'''.<br>The [http://msdn2.microsoft.com/en-us/library/xbf3tbeh(VS.80).aspx /LTCG] option tells the linker to call the compiler and perform whole program optimization.{{note|You should only need to use /LTCG if you built the vgui_controls.lib with the fixes.}}
Using whole program optimization means that each build will take longer, since the compiler effectively runs on all the files at the same time each build, but the compiler will on the other hand be able to optimize across compilation units, resulting in better performing output.


== Notes ==
=== Profile guided optimization (PGO)===
It will take longer for it to link the code, but it will run faster than with the default settings.
The compiler normally has to guess a lot about how the code will run as it compiles. By using PGO you run the program and capture actual usage statistics that you can feed back to the compiler, allowing it to know reality instead of having to guess. This can allow the compiler to drop hints for the CPU on what is likely and what is unlikely, letting the CPU use it's own optimizations more efficiently.


You can enable SSE (Streaming SIMD Extensions) optimizations due to the minium requirements of HL2 being a 1.2 GHz CPU, that defaults to a Pentium 4 or AMD Athlon processor that supports this technology.
This is difficult to automatize, since it involves actually playing the game and ensuring that various parts of the code actually gets executed.


== External Links ==
== External links ==
*[http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/optimization.asp Visual C++ Optimization Overview]
*[http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/optimization.asp Visual C++ Optimization Overview]
*[http://msdn2.microsoft.com/en-us/library/9s7c9wdw(VS.80).aspx Compiler Options]
*[http://msdn2.microsoft.com/en-us/library/9s7c9wdw(VS.80).aspx Compiler Options]
*[http://msdn2.microsoft.com/en-us/library/y0zzbyt4(VS.80).aspx Linker Options]
*[http://msdn2.microsoft.com/en-us/library/y0zzbyt4(VS.80).aspx Linker Options]


 
[[Category:Programming]]
[[category:programming]][[category:tutorials]]
[[Category:Tutorials]]

Latest revision as of 00:14, 22 November 2024

Dead End - Icon.png
This article has no Wikipedia icon links to other VDC articles. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

The Source SDK Visual Studios Projects properties and options are acceptable for many mods, but it can be made better and faster with some small changes to utilize Microsoft's optimizing C++ compiler. These changes produce code better tuned for newer processors without the need to re-write anything.

These changes are implemented by added extra switches and options to the property pages of your client and server projects. You can find this by selecting Project then Properties from the menu in the Visual C++ 2003 IDE.

Note.pngNote:Don't forget to add these options for both the client and hl projects.

Options

In the project properties page of your client and server, change these settings to optimize your DLL files.

Disable debugging

Set the compiler to use the release configuration instead of the debug configuration. This will apply several stock options. Most notably, it will define the NDEBUG macro instead of the DEBUG macro, allowing the preprocessor to remove various debugging aids, such as range checks and assertions, that consumes time.

Another thing this does is that it switches the runtime from the debugging version to the optimized release version.

Target modern CPUs

Code Generation -> Enable Enhanced Instruction Set - set to Advanced Vector Extensions 2 (/arch:AVX2)
The /arch compiler flag enables the SSE and AVX instruction set extensions of Intel and AMD processors. This determines your mod's REQUIRED CPU specs.
SSE (Streaming SIMD (Single Instruction, Multiple Data) Extensions) and AVX (Advanced Vector Extensions) allow the CPU to pack and modify large amounts of data at once, similarly to a GPU.

Note.pngNote:This is a very safe optimization, as all Intel Core CPUs released since 2014 support AVX2, as well as all AMD Ryzen CPUs. This includes AMD Athlon CPUs on the Zen architecture.
HOWEVER, Intel Celeron and Pentium CPUs have only supported AVX1 and AVX2 from 2021 onwards.
If you suspect your mod will be commonly played on extremely weak hardware such as this, consider Streaming SIMD Extensions 2 (/arch:SSE2).

Linker

  • Optimization -> Enabled COMDAT Folding - set to Remove Redundant COMDATs (/OPT:ICF)
    /OPT:ICF removes redundant COMDAT symbols from the linker output.
  • Optimization -> Optimize for Windows98 - set to No (/OPT:NOWIN98)
    /OPT:NOWIN98 controls the section alignment in the final image. You should use this switch when building DLL's only for Windows NT, Windows 2000, Windows XP or later.

Whole Program Optimization / Link time code generation

  • Whole Program Optimization - set to Yes.
    Specifies that the program will be optimized across .obj boundaries.
Note.pngNote:Memory requirements can be very high when compiling with Whole Program Optimization on
Note.pngNote:You should only need to use Whole Program Optimization if you built the vgui_controls.lib with the fixes.

Using whole program optimization means that each build will take longer, since the compiler effectively runs on all the files at the same time each build, but the compiler will on the other hand be able to optimize across compilation units, resulting in better performing output.

Profile guided optimization (PGO)

The compiler normally has to guess a lot about how the code will run as it compiles. By using PGO you run the program and capture actual usage statistics that you can feed back to the compiler, allowing it to know reality instead of having to guess. This can allow the compiler to drop hints for the CPU on what is likely and what is unlikely, letting the CPU use it's own optimizations more efficiently.

This is difficult to automatize, since it involves actually playing the game and ensuring that various parts of the code actually gets executed.

External links