首页 新闻 赞助 找找看

如果去除一段 C# 代码中的 unsafe fixed

0
悬赏园豆:100 [已解决问题] 解决于 2018-09-16 20:51

下面是 EnyimMemcachedCore 中的一段代码,由于进行了指针操作,所以用到了 unsafe fixed ,请问有没有办法可以去掉 unsafe fixed

public unsafe IList<ArraySegment<byte>> CreateBuffer(IList<ArraySegment<byte>> appendTo)
{
    // key size 
    byte[] keyData = BinaryConverter.EncodeKey(this.Key);
    int keyLength = keyData == null ? 0 : keyData.Length;

    if (keyLength > 0xffff) throw new InvalidOperationException("KeyTooLong");

    // extra size
    ArraySegment<byte> extras = this.Extra;
    int extraLength = extras.Array == null ? 0 : extras.Count;
    if (extraLength > 0xff) throw new InvalidOperationException("ExtraTooLong");

    // body size
    ArraySegment<byte> body = this.Data;
    int bodyLength = body.Array == null ? 0 : body.Count;

    // total payload size
    int totalLength = extraLength + keyLength + bodyLength;

    //build the header
    byte[] header = new byte[24];

    fixed (byte* buffer = header)
    {
        buffer[0x00] = 0x80; // magic
        buffer[0x01] = this.Operation;

        // key length
        buffer[0x02] = (byte)(keyLength >> 8);
        buffer[0x03] = (byte)(keyLength & 255);

        // extra length
        buffer[0x04] = (byte)(extraLength);

        // 5 -- data type, 0 (RAW)
        // 6,7 -- reserved, always 0

        buffer[0x06] = (byte)(this.Reserved >> 8);
        buffer[0x07] = (byte)(this.Reserved & 255);

        // body length
        buffer[0x08] = (byte)(totalLength >> 24);
        buffer[0x09] = (byte)(totalLength >> 16);
        buffer[0x0a] = (byte)(totalLength >> 8);
        buffer[0x0b] = (byte)(totalLength & 255);

        buffer[0x0c] = (byte)(this.CorrelationId >> 24);
        buffer[0x0d] = (byte)(this.CorrelationId >> 16);
        buffer[0x0e] = (byte)(this.CorrelationId >> 8);
        buffer[0x0f] = (byte)(this.CorrelationId & 255);

        ulong cas = this.Cas;
        // CAS
        if (cas > 0)
        {
            // skip this if no cas is specfied
            buffer[0x10] = (byte)(cas >> 56);
            buffer[0x11] = (byte)(cas >> 48);
            buffer[0x12] = (byte)(cas >> 40);
            buffer[0x13] = (byte)(cas >> 32);
            buffer[0x14] = (byte)(cas >> 24);
            buffer[0x15] = (byte)(cas >> 16);
            buffer[0x16] = (byte)(cas >> 8);
            buffer[0x17] = (byte)(cas & 255);
        }
    }

    var retval = appendTo ?? new List<ArraySegment<byte>>(4);

    retval.Add(new ArraySegment<byte>(header));

    if (extraLength > 0) retval.Add(extras);

    // NOTE key must be already encoded and should not contain any invalid characters which are not allowed by the protocol
    if (keyLength > 0) retval.Add(new ArraySegment<byte>(keyData));
    if (bodyLength > 0) retval.Add(body);

    return retval;
}
C#
dudu的主页 dudu | 高人七级 | 园豆:31094
提问于:2018-09-13 21:18
< >
分享
最佳答案
0

后来想到,这不正是 Span<T>stackalloc 的用武之地吗,于是进行了这样的修改:

1)去掉 unsafe
2)将

fixed (byte* buffer = header)

改为

Span<byte> header = stackalloc byte[24];

3)将 buffer 变量名都改为 header
4)将

retval.Add(new ArraySegment<byte>(header));

改为

`retval.Add(new ArraySegment<byte>(header.ToArray()));

这样修改后,持续集成通过

dudu | 高人七级 |园豆:31094 | 2018-09-13 23:09

关于 stackalloc 的用途,stackoverflow 上的这个回答讲的很清楚

dudu | 园豆:31094 (高人七级) | 2018-09-14 09:44

@dudu: 那么除非这个类内部处理同样使用指针,否则要么更废功夫要么也就差不多(如果看性能的话还多一次函数调用,多一次堆开销...)。

花飘水流兮 | 园豆:13540 (专家六级) | 2018-09-14 11:22

@花飘水流兮: stackalloc 是在栈上分配内存

dudu | 园豆:31094 (高人七级) | 2018-09-14 11:45
其他回答(2)
0

也没其他操作,直接去掉就行了。

收获园豆:100
花飘水流兮 | 园豆:13540 (专家六级) | 2018-09-13 21:33

有指针操作 byte* buffer = header

支持(0) 反对(0) dudu | 园豆:31094 (高人七级) | 2018-09-13 21:37

@dudu: 去掉 buffer,直接给header赋值操作就行了。无非慢点点而已。

支持(0) 反对(0) 花飘水流兮 | 园豆:13540 (专家六级) | 2018-09-13 21:39

@花飘水流兮: 这地方性能要求很高

支持(0) 反对(0) dudu | 园豆:31094 (高人七级) | 2018-09-13 22:12

@dudu: 你又想去掉又想高性能,鱼和熊掌...反正这个操作也不很小,我干过几次操作image的事,也还wonderful。

支持(0) 反对(0) 花飘水流兮 | 园豆:13540 (专家六级) | 2018-09-13 22:18

@花飘水流兮: 这个提问本来就是为了解决如何兼得鱼和熊掌

支持(0) 反对(0) dudu | 园豆:31094 (高人七级) | 2018-09-13 22:37
0

为什么要去掉呢。必要的时候 指针操作的高性能是普通的操作难以企及的。

SoarNo1 | 园豆:195 (初学一级) | 2018-09-17 01:58

去掉的只是是 unsafe fixed ,没有去掉指针

支持(0) 反对(0) dudu | 园豆:31094 (高人七级) | 2018-09-17 07:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册