在BCL隐藏拳击?

在BCL隐藏拳击?

问题描述:

最近我才知道,有在BCL一些地方仍然使用一些传统$ C $这可能是书面前,仿制药在该框架的2.0版中介绍了C。 显然,这传统code部位,可能导致CLR的执行众多的装箱/拆箱操作。

Recently I became aware that there are some parts in the BCL that still use some "legacy" code that was probably written before generics were introduced in v2.0 of the framework. Apparently, parts of that "legacy" code may cause the CLR to perform numerous boxing/unboxing operations.

由于拳击过量使用是不是一个好东西,我想知道是否有在BCL一些其他的,关键的地方,你已经注意到,拳击发生的呢? 谢谢

Since excessive usage of boxing is never a good thing, I wanted to know whether there are some other, critical places in the BCL that you've noticed that boxing occur? Thanks

需要注意的是,对于所提到的具体的例子:

Note that, for the specific example mentioned:

  • DateTime.Now调用具有相当高的性价比是一个int的拳击(即使考虑到与之相关联的增加gen0收集频率)的系统功能。
  • Dateime.Now的precision极低Windows平台(10-15毫秒,在大多数情况下)
    • 在这样调用这个函数很多不是非常有用的,无论如何,如果你正在做它那么很可能你正在做别的错误...
    • DateTime.Now calls a system function with considerably higher cost that the boxing of an int (even taking into account the increased gen0 collection frequency associated with it).
    • The precision of Dateime.Now is extremely low on windows platforms (10 -15 ms in most cases)
      • Thus calling this function a lot is not terribly useful anyway, if you are doing it then it is likely you're doing something else wrong...

      因此​​,你应该只担心这个内部,如果你的分析表明这是一个问题。
      由于MS从未费心来解决它这似乎不太可能,这曾经冒出作为任何客户的一个问题。

      As such you should only worry about the internals of this if your profiling indicates it is a problem.
      Since MS never bothered to fix it it would seem unlikely that this has ever cropped up as a problem for any customers.

      在隐静脉的分配更关心你的是更可能是:

      Of more concern to you in the hidden allocation vein is more likely to be:

      • Use of enumerations as keys in a dictionary (boxing occurs on every access).
      • Classes whose GetHashCode causes an allocation
      • Enumerator instances when using foreach on an IList<T> rather than List<T>

      但同样,这一切(除了也许这需要相当大的努力来解决枚举在字典中键)只能处理如果您需要

      But again, all these (apart perhaps for the enumeration as key in dictionary which requires considerable effort to work around) should only be dealt with if you need to