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

冷暖自知 提交于 2019-12-02 20:02:19

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!