最优 C 结构定义

发布于 2024-07-28 22:05:30 字数 211 浏览 7 评论 0原文

我只是想知道在 C 结构(C 结构定义)中打包项目(int、float、union 等)时要遵循的注意事项,这将允许编译器进一步优化它。

我想知道是否有任何应该遵循的准则,例如按照允许与单词边界对齐的顺序将项目添加到结构中。 ETC。 ?

详细信息将不胜感激。

问候, -J

该问题还涉及交叉编译此类 C 结构的优化策略。

I was just wondering about the considerations to be followed while packing items (int, float, unions, etc) in a C structure (C struct definition ) that would allow the compiler to further optimize it.

I would like to know whether there are any guidelines that one should follow e.g. adding items to the structure in an order that would allow alignments to the word boundaries. etc. ?

details would be appreciated.

Regards,
-J

The question also entails the optimization strategies towards cross compiling such C structures.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末染殇 2024-08-04 22:05:30

如果您确实想最小化空间,请按从最大对齐到最小对齐的顺序排列。 这应该可以保证你得到最小的填充。

If you really want to minimize space, order things from largest alignment to smallest. That should guarantee that you will get minimal padding.

牵你手 2024-08-04 22:05:30

如果您可以在 gcc 下编译代码,则可以使用 pahole 查找可以改进的结构,并自动重新打包它们。

以下是一些关于 pahole 的文章可能会对您有所帮助:

If you can compile your code under gcc, you might be able to use pahole to find structures that can be improved, and automatically repack them.

Here are a couple of articles about pahole that might help you:

倾城月光淡如水﹏ 2024-08-04 22:05:30

您的计划有什么要求? 你会有多少个元素? 什么样的结构?

例如,如果您有包含多个元素的结构体,并且需要遍历这些元素的数组来搜索某些内容,那么最好不要使用结构体数组,而是使用数组结构体。 这是因为经常使用的成员将并排放置,因此缓存内存将正常工作。

如果您想利用 SIMD(向量)运算,则应按某些边界对齐所有成员。

很难提出正确的建议,因为我不知道您对数据有何期望。

What are the requirements for your program? How many elements would you have? What kind of structures?

For example, if you have struct with several elements, and you need to traverse the array of such elements searching for something, then it is better to do not array of structs, but to do struct of arrays. That's because frequently used members will be located side-by-side, so cache memory will work fine.

If you would like to exploit SIMD (vector) operations, you should align all members by some boundaries.

It's hardly to suggest something correctly, because I don't know what do you expect from your data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文