Intel® C++ Compiler Classic Developer Guide and Reference

ID 767249
Date 12/16/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

GAP Message (Diagnostic ID 30755)

Message

Reordering the fields of the structure '%s' will improve data locality. Suggested field order: '%s'.

Advice

You should reorder the fields of the class or structure type in the specified order. This should improve performance by better utilizing the processor cache.

You must verify that the application code does not rely on the structure fields to be laid out in a specific order. For example, if the application code uses the address of a field to access other fields, it may stop working once the field reordering is applied. Note also that such code is not considered valid.

Example

Consider the following:

//field_reord.c 
struct str {
    int a1, b1, carr[100], c1, d1, e1; 
};
 
extern struct str sp[];
 
int hot_func1() {
    int i, ret = 0;
 
    for (i = 0; i < 1000000; i++) {
        ret += sp[i].a1;
        ret += sp[i].c1;
    }
    return ret; 
}
 
int hot_func2() {
    int ret = 0, i;
    for (i = 0; i < 100000; i++) {
        ret += sp[i].a1;
        ret -= sp[i].e1;
    }
    return ret; 
}
 
int hot_func3() {
    int ret = 0, i;
    for (i = 0; i < 1000000; i++) {
        ret += sp[i].carr[10];
    }
    return ret + sp[0].b1 + sp[0].d1; 
}

In this case, when the program is compiled, the compiler generates a message saying that reordering the fields of the structure 'str' will improve data locality and that the suggested field order is 'a1, c1, e1, carr, b1, d1'.

For the above example, the only changes in field_reord.c to reorder fields of the structure 'str' as advised are the following:

//field_reord.c 
struct str {
    int a1, c1, e1, carr[100], b1, d1; 
}; 
...

Verify

The suggestion is based on the field references in the current compilation. Please make sure that the restructured code satisfies the original program semantics.