为什么好,这结构是可变的?如果是可变的结构可以接受的? [英] Why is it okay that this struct is mutable? When are mutable structs acceptable?

查看:117
本文介绍了为什么好,这结构是可变的?如果是可变的结构可以接受的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

埃里克利珀告诉我,我应该尽量总是值类型不变,所以我想我应该尝试总是值类型不变。



不过,我刚刚发现了这个内部结构可变, System.Web.Util.SimpleBitVector32 的System.Web 组装,这让我觉得必须有具有可变结构的好理由。我猜测的原因,他们没有这样说,是因为它表现在测试更好,他们便将其内部,以阻止其滥用。然而,这猜测。



我有C&放大器; P'd这个结构的来源。它是什么,证明了设计决定使用一个可变的结构?一般情况下,什么样的好处是可以通过这种方法来获得,当这些优点足以显著来证明潜在的利弊?



  [序列化,StructLayout(LayoutKind.Sequential)] 
内部结构SimpleBitVector32
{
私人int数据;
内部SimpleBitVector32(int数据)
{
this.data =数据;
}

内部INT IntegerValue
{
{返回this.data; }
集合{this.data =价值; }
}

内部布尔本[int位]
{
获得{
回报率((this.data&安培;位)==位) ;
}
集合{
int数据= this.data;如果
(值)this.data =数据|位;
,否则this.data =数据&安培; 〜位;
}
}

内部INT此[INT面具,诠释抵消]
{
{返回((this.data&安培;面罩)> ;>抵消); }
集合{this.data =(this.data&安培;〜面罩)| (价值<<抵消); }
}

内部空隙集(int位)
{
this.data | =位;
}

内部空隙清除(int位)
{
this.data&安培; =〜位;
}
}


解决方案

其实如果你搜索包含位向量在.NET框架中的所有类,你会发现一堆这些野兽: - )




  • 系统.Collections.Specialized.BitVector32(唯一的公共的那个......)

  • System.Web.Util.SafeBitVector32(线程安全)

  • 系统。 Web.Util.SimpleBitVector32

  • System.Runtime.Caching.SafeBitVector32(线程安全)

  • System.Configuration.SafeBitVector32(线程安全)

  • System.Configuration.SimpleBitVector32



如果你看的此处是驻留在SSCLI(微软共享源代码CLI,又名转子)系统的源代码。 Configuration.SimpleBitVector32,你会发现此评论:

  // 
//这是一个降低副本System.Collections.Specialized.BitVector32。在
//的原因,这是在这里,是因为它被使用,而通过集中控制和
// WebControl的。其结果是,能够内联在
//衡量的性能增益这种操作导致,一些维修的费用。
//
[序列化()]
内部结构SimpleBitVector32

我认为这说明了一切。我认为System.Web.Util一个是更复杂的,但建立在同样的理由。


Eric Lippert told me I should "try to always make value types immutable", so I figured I should try to always make value types immutable.

But, I just found this internal mutable struct, System.Web.Util.SimpleBitVector32, in the System.Web assembly, which makes me think that there must be a good reason for having a mutable struct. I'm guessing the reason that they did it this way is because it performed better under testing, and they kept it internal to discourage its misuse. However, that's speculation.

I've C&P'd the source of this struct. What is it that justifies the design decision to use a mutable struct? In general, what sort of benefits can be gained by the approach and when are these benefits significant enough to justify the potential detriments?

[Serializable, StructLayout(LayoutKind.Sequential)]
internal struct SimpleBitVector32
{
    private int data;
    internal SimpleBitVector32(int data)
    {
        this.data = data;
    }

    internal int IntegerValue
    {
        get { return this.data; }
        set { this.data = value; }
    }

    internal bool this[int bit]
    {
        get { 
            return ((this.data & bit) == bit); 
        }
        set {
            int data = this.data;
            if (value) this.data = data | bit;
            else this.data = data & ~bit;
        }
    }

    internal int this[int mask, int offset]
    {
        get { return ((this.data & mask) >> offset); }
        set { this.data = (this.data & ~mask) | (value << offset); }
    }

    internal void Set(int bit)
    {
        this.data |= bit;
    }

    internal void Clear(int bit)
    {
        this.data &= ~bit;
    }
}

解决方案

Actually, if you search for all classes containing BitVector in the .NET framework, you'll find a bunch of these beasts :-)

  • System.Collections.Specialized.BitVector32 (the sole public one...)
  • System.Web.Util.SafeBitVector32 (thread safe)
  • System.Web.Util.SimpleBitVector32
  • System.Runtime.Caching.SafeBitVector32 (thread safe)
  • System.Configuration.SafeBitVector32 (thread safe)
  • System.Configuration.SimpleBitVector32

And if you look here were resides the SSCLI (Microsoft Shared Source CLI, aka ROTOR) source of System.Configuration.SimpleBitVector32, you'll find this comment:

//
// This is a cut down copy of System.Collections.Specialized.BitVector32. The
// reason this is here is because it is used rather intensively by Control and
// WebControl. As a result, being able to inline this operations results in a
// measurable performance gain, at the expense of some maintainability.
//
[Serializable()]
internal struct SimpleBitVector32

I believe this says it all. I think the System.Web.Util one is more elaborate but built on the same grounds.

这篇关于为什么好,这结构是可变的?如果是可变的结构可以接受的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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