自动化领域的重新排序的C结构,以避免填充 [英] Automated field re-ordering in C structs to avoid padding

查看:137
本文介绍了自动化领域的重新排序的C结构,以避免填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花几分钟手动重新排序在一个结构域,以减少填料的效果[1],其感觉就像几分钟太多。我的直觉说,我的时间也许可以更好地用撰写一个Perl脚本或诸如此类的东西做这种优化的我。

I've spent a few minutes manually re-ordering fields in a struct in order to reduce padding effects[1], which feels like a few minutes too much. My gut feeling says that my time could probably be better spent writing up a Perl script or whatnot to do this kind of optimization for me.

我的问题是,这是否太是多余的;在那里已经有一些工具,我不知道的,或者一些编译器的功能,我应该能够打开[2]收拾结构?

My question is whether this too is redundant; is there already some tool that I'm not aware of, or some compiler feature that I should be able to turn on[2] to pack structs?

的问题更是由这需要跨越几个不同的结构一致地优化的事实复杂化,所以无论工具用于需要能够考虑到不同结构的比对和指针的大小以及

The issue is even more complicated by the fact that this needs to be consistently optimized across a few different architectures, so whatever tool used needs to be able to account for different struct alignments and pointer sizes as well.

编辑:快速澄清 - 我想要做的是重新排序源$ C ​​$ C场,以避免填充,而不是包的结构为无填充正在编制

A quick clarification -- what I want to do is re-order the field in the source code in order to avoid padding, not "pack" the struct as is compiling without padding.

编辑#2:另外一个问题:根据配置,某些数据类型的大小也可以改变。显而易见的是指针和指针的diff为不同的体系结构,也浮点类型(16,32或根据精确的64位),校验和(取决于速度8或16位)和一些其他非显而易见的东西。

EDIT #2: Another complication: depending on the configuration, sizes of some data types may also change. The obvious ones are pointers and pointer-diffs for different architectures, but also floating-point types (16, 32 or 64-bit depending on the 'exactness'), checksums (8 or 16-bit depending on 'speed') and some other non-obvious stuff.

[1]有问题的结构被实例化数千次的嵌入式设备上,因此该结构的每4个字节的减少可能意味着之间的区别的前往的和的不走的这个项目。

[1] The struct in question is instantiated thousands of times on an embedded device, so each 4-byte reduction of the struct could mean the difference between a go and no-go for this project.

[2]提供的编译器GCC是* 3和4 *,Visual Studio中,TCC,ARM ADS 1.2,RVCT 3. *和其他一些比较模糊的。

[2] Available compilers are GCC 3.* and 4.* , Visual Studio, TCC, ARM ADS 1.2, RVCT 3.* and a few others more obscure.

推荐答案

如果每一个字,你可以挤出存储是至关重要的,那么我建议优化用手结构。一个工具可以最优化安排成员你,但它不知道,例如,在这里这个值,你以16位存储实际上从不1024以上,所以你可以去偷高6位的的在这个的值的此处 ...

If every single word you can squeeze out of the storage is critical, then I have to recommend optimizing the struct by hand. A tool could arrange the members optimally for you, but it doesn't know, for example, that this value here that you're storing in 16 bits actually never goes above 1024, so you could steal the upper 6 bits for this value over here...

所以,人类几乎肯定会击败这项工作的机器人。

So a human will almost certainly beat a robot on this job.

但好像你真的不想手工优化结构的每个架构。也许你真的有一个伟大的很多架构来支持?

But it seems like you really don't want to hand-optimize your structs for each architecture. Maybe you really have a great many architectures to support?

我觉得这个问题是不适合一般的解决方案,但你也许能够为en code你的领域知识到一个自定义的Perl / Python的/什么脚本生成的结构定义每个架构。

I do think this problem isn't amenable to a general solution, but you might be able to encode your domain knowledge into a custom Perl/Python/something script that generates the struct definition for each architecture.

另外,如果你的会员有是两种力量的大小,那么你会得到最佳的包装只需按大小排序的成员(最大的第一个)。在这种情况下,你可以只使用老式的宏观基础的结构路技术 - 是这样的:

Also, if all your members have sizes that are powers of two, then you will get optimal packing simply by sorting members by size (largest first.) In that case, you can just use good old-fashioned macro-based struct-building - something like this:

#define MYSTRUCT_POINTERS      \
    Something*  m_pSomeThing;  \
    OtherThing* m_pOtherThing; 

#define MYSTRUCT_FLOATS        \
    FLOAT m_aFloat;            \
    FLOAT m_bFloat;

#if 64_BIT_POINTERS && 64_BIT_FLOATS
    #define MYSTRUCT_64_BIT_MEMBERS MYSTRUCT_POINTERS MYSTRUCT_FLOATS
#else if 64_BIT_POINTERS
    #define MYSTRUCT_64_BIT_MEMBERS MYSTRUCT_POINTERS
#else if 64_BIT_FLOATS
    #define MYSTRUCT_64_BIT_MEMBERS MYSTRUCT_FLOATS
#else
    #define MYSTRUCT_64_BIT_MEMBERS
#endif

// blah blah blah

struct MyStruct
{
    MYSTRUCT_64_BIT_MEMBERS
    MYSTRUCT_32_BIT_MEMBERS
    MYSTRUCT_16_BIT_MEMBERS
    MYSTRUCT_8_BIT_MEMBERS
};

这篇关于自动化领域的重新排序的C结构,以避免填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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