简体   繁体   English

表示大字节数组的最佳方式

[英]Best Way to Represent Large Byte Array

I'm looking for the most efficient way to store and manage a large byte array in memory.我正在寻找在内存中存储和管理大字节数组的最有效方法。 I will have the need to both insert and delete bytes from any position within the array.我将需要从数组中的任何位置插入和删除字节。

At first, I was thinking that a regular array was best.起初,我认为常规数组是最好的。

byte[] buffer = new byte[ArraySize];

This would allow me to access any byte within the array.这将允许我访问数组中的任何字节。 I can also resize the array.我也可以调整数组的大小。 However, there doesn't appear to be any built-in support for shifting or moving items within the array.但是,似乎没有任何内置支持在数组中移动或移动项目。

One option is to have a loop to move items one by one but that sounds horribly inefficient in C#.一种选择是使用循环来一项一项地移动项目,但这在 C# 中听起来效率极低。 Another option is to create a new array and copy bytes over to the correct position, but that requires copying all data in the array.另一种选择是创建一个新数组并将字节复制到正确的位置,但这需要复制数组中的所有数据。

Is there no better option?没有更好的选择吗?

Actually, I just found the Buffer Class , which appears ideal for what I need.实际上,我刚刚找到了Buffer Class ,它看起来非常适合我的需要。

It looks like the BlockCopy method will block copy a bunch of items and supports copying within the same array, and even correctly handles overlapping items.看起来BlockCopy方法会阻止复制一堆项目并支持在同一数组内复制,甚至可以正确处理重叠项目。

I think the best option in this case is a hybrid between a regular array and a list.我认为在这种情况下最好的选择是常规数组和列表之间的混合。 This would only be necessary with megabyte sized arrays though.不过,这仅对兆字节大小的数组是必要的。

So you could do something like this:所以你可以做这样的事情:

List<byte[]> buffer;

And have each element of the list just a chunk of the data(say 64K or something small and manageable)并且让列表的每个元素只是一个数据块(比如 64K 或一些小而易于管理的东西)

It'd require quite a bit of custom code, but would definitely be the fastest option when having to shift data around in a large array.它需要相当多的自定义代码,但在必须在大型数组中移动数据时绝对是最快的选择。

Also, if you're doing a lot more shifting of bytes than anything else, LinkedList<T> may work better (but it's famously bad for everything but a specific set of cases)此外,如果您进行的字节移位比其他任何事情都多,则LinkedList<T>可能会更好地工作(但众所周知,它对除特定情况外的所有情况都不利)

To clarify why this is more correct than an array, consider inserting 1 byte to the beginning of an array.为了阐明为什么这比数组更正确,请考虑在数组的开头插入 1 个字节。 You must allocate another array (double memory consumption) and then copy every byte to the new array after inserting the new byte, and then free the old array (possible heap corruption depending on size)您必须分配另一个数组(双倍内存消耗),然后在插入新字节后将每个字节复制到新数组,然后释放旧数组(可能堆损坏取决于大小)

Consider now this method with lists.现在考虑这种带有列表的方法。

If you have to insert a lot of bytes, you'll probably want to insert at the beginning of the buffer list.如果您必须插入大量字节,您可能希望在buffer列表的开头插入。 This is an O(n) operation, so your ending efficiency for this operation is O(n/CHUNK_SIZE)这是一个 O(n) 操作,因此此操作的最终效率为 O(n/CHUNK_SIZE)

Or, if you just need to insert a single byte, you can just get the first element of the list and copy the array as normal.或者,如果您只需要插入一个字节,您可以只获取列表的第一个元素并照常复制数组。 Then, the speed is O(CHUNK_SIZE), which isn't horrible, especially if n in comparison is very large (megabytes of data)然后,速度是 O(CHUNK_SIZE),这并不可怕,特别是如果n比较起来非常大(兆字节数据)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM