Profile-Guided Optimizations 按配置优化

Profile-Guided Optimizations

Profile-guided optimization lets you optimize an output file, where the optimizer uses data from test runs of the .exe or .dll file. The data represents how the program is likely to perform in a production environment.

Profile-guided optimizations are only available for x86 or x64 native targets. Profile-guided optimizations are not available for output files that will run on the common language runtime. Even if you produce an assembly with mixed native and managed code (compile with /clr), you cannot use profile-guided optimization on just the native code. If you attempt to build a project with these options set in the IDE, a build error will result.

仅在X86和X64下支持Profile-guided优化,在CLR下不能使用。

Note

Information that is gathered from profiling test runs will override optimizations that would otherwise be in effect if you specify /Ob, /Os, or /Ot. For more information, see /Ob (Inline Function Expansion) and /Os, /Ot (Favor Small Code, Favor Fast Code).

You can use the automated Profile Guided Optimization for Visual C++ plug-in in the Performance and Diagnostics Hub to simplify and streamline the optimization process within Visual Studio, or you can perform the optimization steps manually in Visual Studio or on the command line. We recommend the plug-in because it is easier to use. For information on how to get the plug-in and use it to optimize your app, see Profile Guided Optimization in the Performance and Diagnostics Hub.

Both the Profile Guided Optimization plug-in and manual profile-guided optimization follow these steps to optimize your app:(无论是使用插件,还是手工进行优化,都应按下面步骤进行。)

  • Compile one or more source code files with /GL.

    Each module built with /GL can be examined during profile-guided optimization test runs to capture run-time behavior. Every module in a profile-guided optimization build does not have to be compiled with /GL. However, only those modules compiled with /GL will be instrumented and later available for profile-guided optimizations.使用/GL选项来编译。

  • Link with /LTCG:PGINSTRUMENT.

    /LTCG:PGINSTRUMENT creates an empty .pgd file. After test-run data is added to the .pgd file, it can be used as input to the next link step (creating the optimized image). When specifying /LTCG:PGINSTRUMENT, you can optionally specify /PGD with a nondefault name or location for the .pgd file.链接时应使用/LTCG:PGINSTRUMENT,发现默认是快速链接时间代码生成。我的理解是在编译时刻生成了.pgd文件,在运行时刻生成的是.pgc文件。当下一次使用/LTCG:PGOPTIMIZE来编译时会自动按照.pdg .pgc的信息来进行优化。

  • Profile the application.

    Each time a profiled EXE session ends or a profiled DLL is unloaded, a appname!#.pgc file is created. A .pgc file contains information about a particular application test run. # is a number starting with 1 that is incremented based on the number of other appname!#.pgc files in the directory. You can delete a .pgc file if the test run does not represent a scenario you want to optimize. 每当profiled exe回话结束或profiled DLL被卸载,都会生成一个appname!#.pgc文件。.PGC文件包含了特定程序的运行测试信息。#是一个从1自增的编号。

    During a test run, you can force closure of the currently open .pgc file and the creation of a new .pgc file with the pgosweep utility (for example, when the end of a test scenario does not coincide with application shutdown).在测试运行中,可以强制关闭打开的pgc文件并创建一个新的pgc文件。

    You can use the PogoSafeMode option when you profile your application. This option enables you to specify whether you want to profile the application in safe mode or fast mode. For more information about these modes, see PogoSafeMode.

  • Link with /LTCG:PGOPTIMIZE.

    /LTCG:PGOPTIMIZE creates the optimized image. This step takes as input the .pgd file. For more information, see /LTCG:PGOPTIMIZE.再用/LTCG:PGOPTIMIZE来创建优化后的镜像。 这一步会将.pgd作为输入文件。

It is even possible to create the optimized output file and later determine that additional profiling would be useful to create a more optimized image. If the instrumented image and its .pgd file are available, you can do additional test runs and rebuilt the optimized image with the newer .pgd file.

The following is a list of the profile-guided optimizations(以下是可以自动进行的优化):

  • Inlining – For example, if there exists a function A that frequently calls function B, and function B is relatively small, then profile-guided optimizations will inline function B in function A. 如果有一个函数A经常调用B,而且B确实也很小,那么按配置优化会在A中将B当作inline编译。

  • Virtual Call Speculation– If a virtual call, or other call through a function pointer, frequently targets a certain function, a profile-guided optimization can insert a conditionally-executed direct call to the frequently-targeted function, and the direct call can be inlined.

  • Register Allocation – Optimizing with profile data results in better register allocation.

  • Basic Block Optimization – Basic block optimization allows commonly executed basic blocks that temporally execute within a given frame to be placed in the same set of pages (locality). This minimizes the number of pages used, thus minimizing memory overhead.

  • Size/Speed Optimization– Functions where the program spends a lot of time can be optimized for speed.

  • Function Layout– Based on the call graph and profiled caller/callee behavior, functions that tend to be along the same execution path are placed in the same section.

  • Conditional Branch Optimization – With the value probes, profile-guided optimizations can find if a given value in a switch statement is used more often than other values. This value can then be pulled out of the switch statement. The same can be done with if/else instructions where the optimizer can order the if/else so that either the if or else block is placed first depending on which block is more frequently true.

  • Dead Code Separation– Code that is not called during profiling is moved to a special section that is appended to the end of the set of sections. This effectively keeps this section out of the often-used pages.

  • EH Code Separation – The EH code, being exceptionally executed, can often be moved to a separate section when profile-guided optimizations can determine that the exceptions occur only on exceptional conditions.

  • Memory Intrinsics – The expansion of intrinsics can be decided better if it can be determined if an intrinsic is called frequently. An intrinsic can also be optimized based on the block size of moves or copies.

For more information on performing manual optimization in the IDE or on the command line, see Walkthrough: Using Profile-Guided Optimizations.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值