1

在下面的代码中,我通过 File 对象的所有 Block 对象并执行基于 BitConverted 的序列化。在某些情况下,我有 OutOfMemory 异常。有什么办法可以优化吗?

File.Serialze();

public byte[] Serialize()
{
    byte[] bytes = new byte[Blocks.Count * Block.Size];

    for (int i = 0; i < Blocks.Count; i++)
    {
        Block block = Blocks[i];
        Buffer.BlockCopy(block.Serialize(), 0, bytes, i * Block.Size, Block.Size);
    }
    return bytes;
}

块。序列化()

public byte[] Serialize()
{
    byte[] bytes = new byte[Size];

    Buffer.BlockCopy(BitConverter.GetBytes(fid), 0, bytes, 0, sizeof(long));
    Buffer.BlockCopy(BitConverter.GetBytes(bid), 0, bytes, sizeof(long), sizeof(long));
    Buffer.BlockCopy(BitConverter.GetBytes(oid), 0, bytes, sizeof(long) * 2, sizeof(long));
    Buffer.BlockCopy(BitConverter.GetBytes(iid), 0, bytes, sizeof(long) * 3, sizeof(long));
    Buffer.BlockCopy(BitConverter.GetBytes(did), 0, bytes, sizeof(long) * 4, sizeof(long));

    return bytes;
}

MemoryStream 代替 byte[] 和 shift 代替 BitConverter.GetBytes() 方法:

文件.序列化()

public MemoryStream Serialize()
{
    MemoryStream fileMemoryStream = new MemoryStream(Blocks.Count * Block.Size);
    foreach (Block block in Blocks)
    {
        using (MemoryStream blockMemoryStream = block.Serialize())
        {
            blockMemoryStream.WriteTo(fileMemoryStream);
        }
    }

    return fileMemoryStream;
}

块。序列化()

public MemoryStream Serialize()
{
    MemoryStream memoryStream = new MemoryStream(Size);

    memoryStream.Write(ConvertLongToByteArray(fid), 0, sizeof(long));
    memoryStream.Write(ConvertLongToByteArray(bid), 0, sizeof(long));
    memoryStream.Write(ConvertLongToByteArray(oid), 0, sizeof(long));
    memoryStream.Write(ConvertLongToByteArray(iid), 0, sizeof(long));
    memoryStream.Write(ConvertLongToByteArray(did), 0, sizeof(long));

    return memoryStream;
}

    private byte[] ConvertLongToByteArray(long number)
    {
        byte[] bytes = new byte[8];
        bytes[7] = (byte)((number >> 56) & 0xFF);
        bytes[6] = (byte)((number >> 48) & 0xFF);
        bytes[5] = (byte)((number >> 40) & 0XFF);
        bytes[4] = (byte)((number >> 32) & 0XFF);
        bytes[3] = (byte)((number >> 24) & 0xFF);
        bytes[2] = (byte)((number >> 16) & 0xFF);
        bytes[1] = (byte)((number >> 8) & 0XFF);
        bytes[0] = (byte)((number & 0XFF));

        return bytes;
    }
4

1 回答 1

1

我的第一个问题是:什么是 Count 和 Size ?如果这些(相乘时)很大,那么是的,它会咀嚼记忆。当然,序列化到一个大缓冲区总是会导致问题。最好查看序列化为 Stream 的技术,这将允许使用单个中等大小的缓冲区。在您的情况下,也许每个“块”可以单独序列化并刷新到流中,然后重用相同的中等大小的缓冲区。我个人尽量避免引入不必要的“块”——另一种技术是序列化到缓冲流,然后让它决定何时刷新到底层流。

最后,BitConverter 想要创建 byte[] 总是让我失望。编写该 API 的人需要进行严厉的交谈。适当的技术应该是有一个 API,它接受一个缓冲区和偏移量,并写入现有的缓冲区。少得多的分配。我建议寻找在没有所有这些(诚然是短暂的)分配的情况下编写的方法。这对于 int / long 等(您只需使用移位操作)很容易 - 但对于 double 等,您将需要不安全的代码或联合结构。

于 2013-05-12T13:57:58.943 回答