phalcon/cphalcon — pull request #891 by sjinks (2013-07-21 17:54:36+00:00)
Title: [NFR] Reorder struct fields to save some memory

This is mostly useful for x86_64 architecture because pointers are larger than plain integers.

Example:

```c
typedef struct _phannot_scanner_token {
int opcode;
char *value;
int len;
} phannot_scanner_token;
```

pointers are 8 bytes, integers are 4 bytes; pointers must be aligned at 8 bytes boundary, integers at 4 bytes boundary; thus, after `opcode` there will be a padding 4 bytes long. Plus, the structure itself will have to be padded to 8 bytes boundary.

Therefore, the actual size will be (4+8+4)+4+4=24 bytes; 16 bytes are payload, 8 bytes are padding.

If we move `value` to the front:

```c
typedef struct _phannot_scanner_token {
char *value;
int opcode;
int len;
} phannot_scanner_token;
```

`value` will be at 8 bytes boundary and `opcode` and `len` will be at 4 bytes boundary; the size of the structure is 16 bytes (a multiple of 8) and the structure does not have to be padded.

Thus, reordering in this case allowed to reduce the size of the structure in 1.5 times. Not bad.