由于什么原因,我会选择除512以外的C#编译器文件对齐设置? [英] For what reason would I choose a C# compiler file alignment setting other than 512?

查看:75
本文介绍了由于什么原因,我会选择除512以外的C#编译器文件对齐设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在MSDN中看到如何更改C#编译的文件对齐方式(通过项目设置和命令行)。

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

我在Google上搜索并看到有文章说明512字节的文件对齐减小了.dll的大小。我用不同的文件对齐方式测试了自己,发现是的。

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.

我的问题是:

为什么我要使用其他文件对齐方式?

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?

此外,它到底有什么作用?有关MSDN页面的部分?什么是节?

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

http://msdn.microsoft.com/zh-cn/library/0s4tzdf2.aspx

推荐答案

相当技术性的实现细节。首先,您首先必须了解PE32文件的结构,即Windows中DLL和EXE的文件格式。关于这一点的经典文章是Matt Pietrik的在PE内窥视,Win32便携式可执行文件格式之旅。撰写于17年前,但仍与并可用相关。

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.

/ filealign设置引用IMAGE_OPTIONAL_HEADER.FileAlignment字段的值。它确定节中原始数据的对齐方式。节是文件中的代码或数据块。在纯.NET程序集的情况下,几乎是专有数据。

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.

文件格式与磁盘之间存在非常密切的关系。可执行映像用作Windows中内存映射文件的备份文件。通过将文件映射到虚拟内存地址空间来加载可执行文件。非常有效,加载DLL仅涉及创建该映射,而不会从文件中读取实际数据。当进程尝试从节中读取一个字节时,这会以一种惰性的方式发生。如果尚未将其加载到内存中,则会产生页面错误,并且操作系统将从文件中读取4096个字节到内存中。最大的优势是您无需为不使用的数据或代码付费。也是您第一次阅读[属性]时花费昂贵的原因。

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.

文件对齐方式的相关性在于各节中的原始数据如何对齐。包含机器代码的大多数现代可执行文件都使用4096字节的对齐方式,即虚拟内存页面的大小。这与包含托管代码的程序集不太相关,IL只是数据。这将使使用较小的对齐方式(浪费更少的空间)变得有意义。 512字节(不是千字节)是一个快乐数字,它是PE32格式允许的最小值。

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.

我可以想到的唯一将选项添加到UI的唯一可能原因是,与其他编译器相比,C#编译器具有很少的编译选项。 其他是生成本机代码的编译器。因此存在该选项,因为编译器具有该选项。 [attributes]涵盖了许多调整,这使编译器命令行简短而生动。但是没有文件对齐的属性,需要在生成文件之前就知道它,属性会为时已晚。

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.

相反的例子是C ++编译器和链接器, IDE提供了 19 个属性页来进行设置。但是仍然不能完全涵盖所有内容,必须在命令行选项页面中设置真正晦涩的内容。

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.

这篇关于由于什么原因,我会选择除512以外的C#编译器文件对齐设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆