Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For what reason would I choose a C# compiler file alignment setting other than 512?

I can see in MS Docs how to change the file alignment for C# compilation (via project settings and the command line).

I have googled and seen articles explaining that a file alignment of 512 Bytes reduces the size of the .dll. I have tested myself with different file alignments and seen that, yes, it does.

My question is:

Why would I ever want to use a different file alignement? There must be scenarios where this is required or there wouldn't be the option?

Also, what does it do exactly? The MSDN pages talk about sections? What are sections?

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/advanced#filealignment

like image 762
J M Avatar asked Mar 05 '11 19:03

J M


People also ask

Why did you choose C-section?

Lower risk of incontinence and sexual dysfunction after the birth of the baby. Lower risk of the baby being deprived of oxygen during delivery. Lower risk of the baby experiencing trauma while passing through the birth canal.

Can you choose to have AC?

Emergencies and complications can lead to a C-section, but you might be surprised to hear that some women choose to have a C-section without a medical reason. This is called an elective C-section.

When is C-section recommended?

You will usually have a planned c-section at 39 weeks of pregnancy. The aim is to do the c-section before you go into labour. Babies born earlier than 39 weeks are more likely to need help with their breathing. Sometimes there's a medical reason for delivering the baby earlier than this.

Can you choose to have ac section for your first child?

Some C-sections are considered elective, meaning they are requested before labor. Someone may choose to have a C-section to plan when to deliver or if they previously had a complicated vaginal delivery. But if someone is eligible for a vaginal delivery, there are not a lot of advantages to having a C-section, said Dr.


1 Answers

That's a rather technical implementation detail. To get started, you first have to understand the structure of a PE32 file, the file format for DLLs and EXEs in Windows. The canonical article for that is Matt Pietrik's "Peering Inside the PE, A Tour of the Win32 Portable Executable File Format". Written 17 years ago but still relevant and available.

The /filealign setting refers to the value of IMAGE_OPTIONAL_HEADER.FileAlignment field. It determines how the raw data in a section is aligned. A section is a chunk of code or data in the file. Almost exclusively data in the case of pure .NET assemblies.

There is a very close relationship between the file format and the disk. An executable image is used as the backing file of a Memory Mapped File in Windows. Executables are loaded by mapping the file into the virtual memory address space. Very efficient, loading a DLL only involves creating that mapping, no actual data is read from the file. That happens in a lazy fashion when the process tries to read a byte from a section. If it isn't loaded into memory yet, that produces a page fault and the operating system reads 4096 bytes from the file into memory. The big advantage is that you don't pay for data or code that you don't use. Also the reason that reading [attributes] is expensive when you read it the first time.

The relevance of the file alignment is how the raw data in the sections line up. Most modern executables that contain machine code use an alignment of 4096 bytes, the size of a virtual memory page. That isn't very relevant for assemblies containing managed code, IL is just data. Which would make it sense to use a smaller alignment, one that wastes less space. 512 bytes (not kilobytes) is a happy number there, it is the smallest value allowed in the PE32 format.

The only possible reason I can think of for adding the option to the UI at all is that the C# compiler has very few compile options compared to other compilers. "Other" being compilers that generate native code. So the option is there because the compiler has the option. A lot of the tweaks are covered by [attributes], nicely making the compiler command line short and snappy. But no attribute for the file alignment, it needs to be known before generating the file, an attribute would be too late.

The opposite example is the C++ compiler and linker, the IDE offers nineteen property pages to set them. But still not covering them all, the really obscure ones have to be set in the "Command Line" option page.

like image 76
Hans Passant Avatar answered Sep 27 '22 20:09

Hans Passant