隐藏拳击在BCL? [英] Hidden Boxing in the BCL?

查看:132
本文介绍了隐藏拳击在BCL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我意识到BCL中有一些部分仍然使用一些遗留代码,这些代码可能是在框架v2.0中引入泛型之前编写的。
显然,这些遗产代码的一部分可能导致CLR执行大量的拳击/拆箱操作。



由于过度使用拳击从来不是一件好事我想知道BCL中还有其他一些关键的地方你注意到拳击发生了吗?
谢谢

解决方案

请注意,对于上述具体示例:




  • DateTime.Now调用一个具有相当高成本的系统函数,即int的inting(甚至考虑到与之相关联的增加的gen0收集频率)。

  • 在Windows平台上,Dateime.Now的精度非常低(大多数情况下为10 -15 ms)


    • 因此很多调用这个函数不是很有用,反正如果你这样做,那么很可能你在做别的事情...




因此,如果您的分析表明这是一个问题,您应该只需担心这些内容。

由于MS从来没有打扰过修复它,所以似乎不太可能对于任何客户来说,这是一个问题。



在隐藏的分配静脉中更关心的是更有可能是:




  • 使用e数字作为字典中的键(每次访问都会发生拳击)。

  • 其中 GetHashCode导致分配

  • 使用时的枚举器实例在 IList< T> 而不是列表< T>
  • $ b $但是,再次,所有这些(除了可能的枚举作为关键的字典,需要相当多的努力来解决)只应该处理如果你需要到


    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.

    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 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...

    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:

    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

    这篇关于隐藏拳击在BCL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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