windows - Does the .NET Framework need to be reoptimized after upgrading to a new CPU microarchitecture?

07
2014-07
  • Louis

    I believe that the .NET Framework will optimize certain binaries targeting features specific to the machine it's installed on.

    After changing the CPU from an Intel Nehalem to a Haswell chip, should the optimization be run again manually? If so, what is the process for that?

    Between generations here are some notable additions:

    So my, albeit naive, thought process was that the optimizations could take advantage of these in general cases. For example, perhaps calls to the Random library could utilize the hardware-RNG on Ivy Bridge and later models.

  • Answers
  • and31415

    Short answer

    It doesn't need to, as of now.

    Long answer

    Code that targets the .NET Framework Common Language Runtime (CLR) is known as managed code, and code that doesn't is known as unmanaged.

    When compiling to managed code, the compiler translates your source code into Microsoft intermediate language (MSIL), which is a CPU-independent set of instructions that can be efficiently converted to native code.

    Before you can run Microsoft intermediate language (MSIL), it must be compiled against the common language runtime to native code for the target machine architecture. The .NET Framework provides two ways to perform this conversion:

    Source: Managed Execution Process

    JIT compilation

    JIT compilation takes into account the possibility that some code might never be called during execution. Instead of using time and memory to convert all the MSIL in a PE file to native code, it converts the MSIL as needed during execution and stores the resulting native code in memory so that it is accessible for subsequent calls in the context of that process.

    Source: Managed Execution Process

    While in theory the JIT compiler could take advantage of CPU-specific instructions like AES or AVX, such optimizations haven't been implemented yet. Full AVX support will probably come later with a new release of the .NET runtime that includes RyuJIT, the next-gen JIT compiler which is being developed.

    Native images

    [The Native Image Generator (Ngen.exe) is a tool that performs an] ahead-of-time compilation [of] MSIL assemblies to native code much like the JIT compiler does. However, the operation of Ngen.exe differs from that of the JIT compiler in three ways:

    • It performs the conversion from MSIL to native code before running the application instead of while the application is running.

    • It compiles an entire assembly at a time, instead of one method at a time.

    • It persists the generated code in the Native Image Cache as a file on disk.

    Source: Managed Execution Process

    When you use ngen.exe to create native images, the output depends on multiple factors such as the identity of the assemblies, and the framework version. Until version 1.1 of the .NET Framework, the output was also CPU-dependent:

    If you upgrade a computer's processor to a new processor family, all native images stored in the native image cache become invalid.

    Source: Native Image Generator (Ngen.exe) Legacy Syntax

    Starting with version 2.0, some causes of image invalidation (including the CPU type) have been removed. Additionally, a new /update switch was added to re-create invalid images. Quoting a blog entry written by David Notario (emphasis mine):

    In previous versions of the CLR (1.0 and 1.1), we treated NGEN compilation the same way as we treated JIT compilation, ie , given that we are compiling in the target machine, we should take advantage of it. However, in Whidbey [Visual Studio 2005], this is not the case. We assume a PPro instruction set and generate code as if it was targeting a P4. Why is this? There was a number of reasons:

    • Increase predictability of .NET redist assemblies (makes our support life easier).

    • OEMs and other big customers wanted a single image per platform (for servicing, building and managing reasons).

    We could have had a command line option to generate platform specific code in ngen, but given that ngen is mainly to provide better working set behavior and that this extra option would complicate some other scenarios, we decided not to go for it.

    Source: Does the JIT take advantage of my CPU?

    In Windows 8 and later, the .NET Framework is able to automatically generate and maintain native images based on usage. The Native Image Task will do all the work in the background whenever needed, and there's no need for manual intervention.

    Further reading


  • Related Question

    windows - Quickest way to find the latest installed version of the .NET framework?
  • Mal Ross

    I always forget what version(s) of the .NET framework (and their service packs) I've got installed. Whenever I need to find out, I end up trawling through a long list of updates performed by Windows Update, or, failing that, looking in Add/Remove Programs. There must be a quicker way...

    Thanks.


  • Related Answers
  • Joey

    Scott Hanselman created a nice webpage which tells you which .NET Framework is installed and which one to download if it's not the current one.

  • hanleyp

    From Microsoft:

    How to determine which versions of the .NET Framework are installed To determine which versions of the .NET Framework are installed, locate the %systemroot%\Microsoft.NET\Framework folder. To open this folder, you can paste this address into a Windows Explorer address bar. The following folders contain the released versions of the .NET Framework:

    • v3.5
    • v3.0
    • v2.0.50727
    • v1.1.4322
    • v1.0.3705

    ...

    To determine which versions of the .NET Framework are installed on a computer, follow these steps:

    • Open any one of the folders in the previous list.
    • Right-click the Mscorlib.dll file, and then click Properties.
    • Click the Version tab, and then note the file version.
    • Use the previous list to determine which version of the .NET Framework is installed on the computer, and then click OK.
    • Repeat these steps for each version of the .NET Framework on the computer.
  • 8088

    GotDotNet 2.00 – Detect .NET Version

    alt text

  • bouvierr

    My favorite tool for detecting installed .NET versions is ASoft .NET Version Detector. It tells you all versions currently installed, including service packs and the "Client Profile" version. Nothing to install, just an executable.

    enter image description here