如何在装配抗冲击性能类的数量?数量、性能、抗冲击、如何在

2023-09-02 10:40:36 作者:再贱"板砖伺候

我的工作将产生code的大量类的项目 - 数百到数千预期。 目前还不知道在生成时多少这些类将实际被访问。

The project I'm working on will generate code for a large number of classes - hundreds to thousands is expected. It is not known at generation time how many of these classes will actually be accessed.

生成的类可以(1)每个人都生活在一个单一的组件(也可能是几个组件),当脚尖耗时的过程开始它会被加载。

The generated classes could (1) all live in a single assembly (or possibly a handful of assemblies), which would be loaded when toe consuming process starts.

...或者(2)我可以生成的每个的类,就像Java的编译每类单个 *。类二进制文件,然后拿出一个机制,按需加载的程序集。

...or (2) I could generate a single assembly for each class, much like Java compiles every class to a single *.class binary file, and then come up with a mechanism to load the assemblies on demand.

这样的问题:这种情况下,会产生更好的(内存和时间)的表现?

The question: which case would yield better (memory and time) performance?

我的直觉是,情况(1)负载时间和使用的内存成正比类组成单一的整体装配的数量。 OTOH,案例(2)自带的并发症。

My gut feeling is that for case (1) the load time and used memory are directly proportional to the number of classes that make up the single monolithic assembly. OTOH, case (2) comes with its complications.

如果你知道的,尤其是什么code被调用有关的装载组件的内部的所有资源(如果有的话!?),什么内存分配(簿记为新加载的程序集),请分享。

If you know any resources pertaining to the internals of loading assemblies, especially what code gets called (if any!?) and what memory is allocated (book-keeping for the newly loaded assembly), please share them.

推荐答案

正在试图解决一个不存在的问题,组件加载是非常高度优化的.NET。

You are trying to solve a non-existing problem, assembly loading is very heavily optimized in .NET.

分手大型装配成许多较小的,毫无疑问,你可以做的最糟糕的事情。到目前为止,在加载程序集最大的开销是找到该文件。这是一个的冷启动的问题,CLR的装载机陷入了缓慢的磁盘,目录条目需要进行检索和搜索定位包含该文件内容的磁盘扇区。在热启动这个问题会消失的时候,装配数据可以从文件系统缓存中检索。请注意,Java没有做这种方式或者说,它封装.class文件成的​​.jar。 .jar中是大致相当于一个组件。

Breaking up a large assembly into many smaller ones is without a doubt the very worst thing you can do. By far the biggest expense in loading an assembly is finding the file. This is a cold start problem, the CLR loader is bogged down by the slow disk, directory entries need to be retrieved and searched to locate the disk sectors that contain the file content. This problem disappears on a warm start when the assembly data can be retrieved from the file system cache. Note that Java doesn't do it it this way either, it packages .class files into a .jar. A .jar is the rough equivalent of an assembly.

在该文件的位置,.NET使用操作系统设施,使实际加载装配数据很便宜。它采用的是存储器映射的文件。其中涉及仅仅保留的虚拟内存文件,但的不是的从文件中读取数据。

Once the file is located, .NET uses an operating system facility to making actually loading the assembly data very cheap. It uses a memory mapped file. Which involves merely reserving the virtual memory for the file but not reading from the file.

阅读不会发生,直到后来与被做了的缺页的。任何需求分页虚拟内存的操作系统的功能。访问虚拟存储器产生页面故障,操作系统加载从文件中的数据和虚拟内存页映射到RAM中。程序继续在此之后,从来没有意识到它得到了由操作系统中断。这将是产生这些页面错误的抖动,在访问中找到白细胞介素的方法组装的元数据表。从它然后生成可执行的机器code。

Reading doesn't happen until later and is done by a page fault. A feature of any demand-paged virtual memory operating system. Accessing the virtual memory produces a page fault, the operating system loads the data from the file and maps the virtual memory page into RAM. After which the program continues, never being aware that it got interrupted by the OS. It will be the jitter that produces these page faults, it accesses the metadata tables in the assembly to locate the IL for a method. From which it then generates executable machine code.

这是该方案的自动好处是,你从来没有付出code,它是一个程序集,但没有被使用。抖动根本没有理由看文件的部分包含IL所以它从来没有真正被读取。

An automatic benefit from this scheme is that you never pay for code that is in an assembly but is not being used. The jitter simply has no reason to look at the section of the file that contains the IL so it never actually gets read.

和注意该方案的缺点,使用类首次就涉及PERF因读盘打击。这需要支付或那种方式,在.NET债务是由于在最后一刻。这就是为什么属性有正在缓慢的声誉。

And note the disadvantage of this scheme, using a class for the first time does involve a perf hit due to the disk read. This needs to be paid for one way or another, in .NET the debt is due at the last possible moment. Which is why attributes have a reputation for being slow.

更大的组件总是比许多较小的组件更好。

Bigger assemblies are always better than many smaller assemblies.