memcpy 性能因循环期间目标指针是否更改而异
因此,我正在使用 FPGA 卡来捕获来自网络的数据,但在将数据足够快地复制到内存时遇到问题。我使用 mempcy 将字节从一个位置复制到另一个位置,但是当我将其配置为通过可用空间递增时,我收到一条错误消息,指出我没有足够快地将数据从源中复制出来,并且它被传入的数据覆盖数据。
但是,如果我将目标设置为静态值(绝对数或不变的变量),则不会收到“复制速度不够快”错误。这是没有用的,因为我基本上一直在覆盖目标中的数据,但 memcpy 工作得足够快。
代码示例(来自供应商抽象的元素):
unsigned char *destinationBuffer = new unsigned char[500000000];
unsigned int destBuffer_current_size = 0;
while (!RingBuffer->IsEmpty()){
//abstraction - get pointer to next unit to be copied from source buffer
bytes = (char*)unitToBeCopied
memcpy((void*)&destinationBuffer[destBuffer_current_size], (void*)bytes, 4096);
destBuffer_current_size += 4096;
允许 memcpy 足够高效地工作但不是可行选项的东西:
将指针设置为destinationBuffer[0]
memcpy((void*)&destinationBuffer[0], (void*)bytes, 4096);
将指针设置为destinationBuffer[490000000] - 意味着访问数组的部分距离头部很远不是问题
memcpy((void*)&destinationBuffer[490000000], (void*)bytes, 4096);
使用 int 作为目标指针,但不增加它 - 意味着计算变量不是瓶颈
memcpy((void*)&destinationBuffer[destBuffer_current_size], (void*)bytes, 4096);
destBuffer_current_size += 0; //ie destBuffer_current_size does not increment
使用不为目标指针增加的 int,但添加另一个int 确实递增到循环 - 意味着递增器不是瓶颈
memcpy((void*)&destinationBuffer[destBuffer_current_size], (void*)bytes, 4096);
second_destBuffer_current_size += 4096; //ie destBuffer_current_size does not increment, but a second unsigned int does, to eliminate the incrementing as the root cause
尚未起作用的事情:
- 更改缓冲区大小
- 将 (void*)&var[] 更改为 &var[]
为什么不递增目标指针在 memcpy 中使其工作更有效,有没有办法让它在递增的同时工作?
注意:是的,如果我可以交换指针并完成它会更快。源数据位于内存的一部分,如果复制得不够快,它将被覆盖。
So I am working with an FPGA card to capture data coming off the network, and I am having problems copying the data to memory quickly enough. I use mempcy to copy the bytes from one location to another, but when I have it configured to increment through the available space I get an error saying that I'm not copying data out of the source quickly enough and it is getting overwritten by incoming data.
However, if I set the destination to be a static value (either an absolute number or a variable that does not change), I do not get the 'not copying fast enough' error. This is useless since I've been basically overwriting the data in the destination the entire time, but memcpy works fast enough.
Code sample (elements from vendor abstracted):
unsigned char *destinationBuffer = new unsigned char[500000000];
unsigned int destBuffer_current_size = 0;
while (!RingBuffer->IsEmpty()){
//abstraction - get pointer to next unit to be copied from source buffer
bytes = (char*)unitToBeCopied
memcpy((void*)&destinationBuffer[destBuffer_current_size], (void*)bytes, 4096);
destBuffer_current_size += 4096;
Things that have allowed memcpy to work efficiently enough but are not viable options:
Set the pointer to destinationBuffer[0]
memcpy((void*)&destinationBuffer[0], (void*)bytes, 4096);
Set the pointer to destinationBuffer[490000000] - means that accessing portions of the array that are significant distances from the head is not the problem
memcpy((void*)&destinationBuffer[490000000], (void*)bytes, 4096);
Use an int for the destination pointer, but do not increment it - means that evaluating a variable is not the bottleneck
memcpy((void*)&destinationBuffer[destBuffer_current_size], (void*)bytes, 4096);
destBuffer_current_size += 0; //ie destBuffer_current_size does not increment
Use an int that does not increment for the destination pointer, but add another int that does increment to the loop - means that the incrementor is not the bottleneck
memcpy((void*)&destinationBuffer[destBuffer_current_size], (void*)bytes, 4096);
second_destBuffer_current_size += 4096; //ie destBuffer_current_size does not increment, but a second unsigned int does, to eliminate the incrementing as the root cause
Things that have not worked:
- Changing the buffer size
- Changing (void*)&var[] to just &var[]
Why would not incrementing the destination pointer in memcpy make it work more efficiently, and is there a way to make it work while incrementing it?
Note: Yes, it would be faster if I could just swap pointers and be done with it. The source data is in a section of memory where it will get overwritten if not copied quickly enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论