可变长度阵列性能的影响(C / C ++) [英] Variable Length Array Performance Implications (C/C++)

查看:83
本文介绍了可变长度阵列性能的影响(C / C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写了一个文件描述符发出的数组一个相当简单的功能。然而,为了发送数据,我需要附加一字节的头

I'm writing a fairly straightforward function that sends an array over to a file descriptor. However, in order to send the data, I need to append a one byte header.

下面是我在做什么的简化版本,它似乎工作:

Here is a simplified version of what I'm doing and it seems to work:

void SendData(uint8_t* buffer, size_t length) {
  uint8_t buffer_to_send[length + 1];
  buffer_to_send[0] = MY_SPECIAL_BYTE;
  memcpy(buffer_to_send + 1, buffer, length);
  // more code to send the buffer_to_send goes here...
}

就像我说的,code似乎很好地工作,但是,我最近陷入使用谷歌C ++风格指南,因为我现在的项目没有为其设置风格指南的习惯(实际上,我在我的项目,我唯一的软件工程师想用东西是工业中使用)。我跑了谷歌的cpplint.py,它抓住了我在哪里创建buffer_to_send行,扔关于不使用变长数组的一些评论。具体来说,以下是谷歌的C ++风格指南对此有什么看法变长数组...

Like I said, the code seems to work fine, however, I've recently gotten into the habit of using the Google C++ style guide since my current project has no set style guide for it (I'm actually the only software engineer on my project and I wanted to use something that's used in industry). I ran Google's cpplint.py and it caught the line where I am creating buffer_to_send and threw some comment about not using variable length arrays. Specifically, here's what Google's C++ style guide has to say about variable length arrays...

<一个href=\"http://google-styleguide.google$c$c.com/svn/trunk/cppguide.xml#Variable-Length_Arrays_and_alloca__\" rel=\"nofollow\">http://google-styleguide.google$c$c.com/svn/trunk/cppguide.xml#Variable-Length_Arrays_and_alloca__

根据他们的意见,看来我的可能的发现看似随机崩溃的根本原因,在我的code(发生的非常频繁,但仍然讨厌)。不过,我有点撕裂为如何解决它。

Based on their comments, it appears I may have found the root cause of seemingly random crashes in my code (which occur very infrequently, but are nonetheless annoying). However, I'm a bit torn as to how to fix it.

下面是我提出的解决方案:

Here are my proposed solutions:


  1. 请buffer_to_send基本上恒定长度的固定长度的数组。我能想到的这里的问题是,我必须使缓冲区一样大,我想送理论上最大的缓冲区。在一般情况下,缓冲区是小很多,而且我在浪费约0.5KB这样每个函数被调用的时间。请注意,程序必须在嵌入式系统上运行,而我并不指望每个字节,我想用尽可能少的内存越好。

  1. Make buffer_to_send essentially a fixed length array of a constant length. The problem that I can think of here is that I have to make the buffer as big as the theoretically largest buffer I'd want to send. In the average case, the buffers are much smaller, and I'd be wasting about 0.5KB doing so each time the function is called. Note that the program must run on an embedded system, and while I'm not necessarily counting each byte, I'd like to use as little memory as possible.

使用新的和删除或的malloc /免费动态分配的缓冲区。这里的问题是该函数频繁调用并且也就会不断要求对内存的操作系统,然后松开方面一些开销。

Use new and delete or malloc/free to dynamically allocate the buffer. The issue here is that the function is called frequently and there would be some overhead in terms of constantly asking the OS for memory and then releasing it.

使用两个连续调用write()方法,以便将数据传递给文件描述符。即,第一写将通过仅一个字节,而下一个将发送缓冲器的其余部分。虽然看似简单,我需要研究的codeA的详细位(注意,我得到这个code从previous工程师,谁后来离开我工作的公司传世),以保证两个连续写入原子发生。另外,如果这需要锁定,那么它本质上变得更加复杂,并具有比情况#2更高的性能的影响。

Use two successive calls to write() in order to pass the data to the file descriptor. That is, the first write would pass only the one byte, and the next would send the rest of the buffer. While seemingly straightforward, I would need to research the code a bit more (note that I got this code handed down from a previous engineer who has since left the company I work for) in order to guarantee that the two successive writes occur atomically. Also, if this requires locking, then it essentially becomes more complex and has more performance impact than case #2.

请注意,我不能让buffer_to_send功能之外的成员变量或范围的它,因为有(可能)从不同的线程任何给定时间多次调用该函数。

Note that I cannot make the buffer_to_send a member variable or scope it outside the function since there are (potentially) multiple calls to the function at any given time from various threads.

请让我知道你的意见和我的preferred的做法应该是什么。感谢您的时间。

Please let me know your opinion and what my preferred approach should be. Thanks for your time.

推荐答案

您可以使用writev就折了两个连续通话写()在你的选择3成一个单一的()调用。

You can fold the two successive calls to write() in your option 3 into a single call using writev().

http://pubs.opengroup.org/onlinepubs/009696799/functions/ writev.html

这篇关于可变长度阵列性能的影响(C / C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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