Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR] Reorder struct fields to save some memory #891

Merged
merged 1 commit into from Jul 22, 2013
Merged

[NFR] Reorder struct fields to save some memory #891

merged 1 commit into from Jul 22, 2013

Conversation

ghost
Copy link

@ghost ghost commented Jul 21, 2013

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

Example:

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:

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.

phalcon pushed a commit that referenced this pull request Jul 22, 2013
[NFR] Reorder struct fields to save some memory
@phalcon phalcon merged commit c0378c0 into phalcon:1.2.1 Jul 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants