1

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.

14
  • You don't have a different architecture, both CPUs, are x86-64 processors. Why would you need to regenerated your documentation nothing changed.
    – Ramhound
    May 30, 2014 at 0:02
  • @Ramhound Sorry, I mean micro-architecture. May 30, 2014 at 0:03
  • Where did you hear about this so called optimization, I want to contact the author of said information, and give him a virtual slap for giving out bad information.
    – Ramhound
    May 30, 2014 at 0:04
  • 1
    Update your question. Be specific about what CPU you were using and what CPU you upgraded to. Additionally, if your getting errors provide them, we need specifics. I required facts not guess work in order to formulate an answer
    – Ramhound
    May 30, 2014 at 1:14
  • 1
    @Louis I believe the book you were referring to is CLR via C# by Jeffrey Richter.
    – and31415
    Jun 1, 2014 at 14:29

1 Answer 1

4
+250

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

1
  • Wow, you really a fountain of knowledge. Jun 10, 2014 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .