optimizing c structs layouts
Let's consider a simple c struct:
struct foo { const char * str; unsigned char flag; uint64_t len; };
Suppose that this code is being run as a part of a program executing on a 64-bit machine: what would you expect the result of sizeof(struct foo) to be?
Most people that never had to mess with struct sizes and optimizations would guess that it should be 17...
... but it's 24! Why is that?
The reason for this behavior is that compilers optimize struct layouts for speed, and it's the modern norm that aligned memory accesses are the fastest way to access data.
This means that, depending on the type of field and cpu, your data will have some alignment and will be positioned such that that alignment is respected (or, such that field-address % field-alignment == 0).
size, alignment, padding
In the case of the previous example, pointers and 64-bit fields are 8B aligned on 64-bit machines, and this means that, in order to force a layout where everything in the struct is aligned, the compiler will generate some padding between the flag and len fields:
Now let's consider another example, where a struct like this is defined, on the same machine as before:
struct bar { const char * str; short s1; int i1 short s2; int i2; };
How do we go about computing its size?
There are three rules:
- Struct fields want to be aligned with their own natural alignment.
- The overall struct alignment is equal to the alignment of its widest field
- If you had to put two structs of the same type side by side, the second one should be aligned to its alignment -- this means that structs must have trailing padding up to their alignment
Quick recap on basic types alignment and size for 64-bit machines:
type | size | alignment |
---|---|---|
char | 1 | 1 |
short | 2 | 2 |
int | 4 | 4 |
long | 8 | 8 |
float | 4 | 4 |
double | 8 | 8 |
pointers | 8 | 8 |
Also remember that:
- Arrays have the alignment of their value type, and size that is sizeof(type) * elements number.
- Unions have the alignment and size of their widest member.
And you can use the super useful sizeof and _Alignof operators to get this information for your custom types. Note that _Alignof is available from C11 onward, and is called alignof starting with C23. It was always alignof from what I understand in C , since C 11.
For more information, the bible on this topic is The Lost Art of Structure Packing, from which I learned almost everything I know about this, together with a lot of practice and hands-on experience.
optimizing your structs: stropt
This topic here is something that comes up rather frequently at work, where saving bytes here and there is super important when sending huge loads of data continuously in queues and whatnot.
In order to make my life easier, I wrote a tool that produces some statistics on a type you pass as input, with reference to a source file or code snippet, it's called stropt (struct optimizer).
Abathargh/stropt on GitHub
build & install
If you have a local go installation you can go right ahead and build or install the application directly:
struct foo { const char * str; unsigned char flag; uint64_t len; };
Already compiled binaries for a list of os/archs combinations are also provided in the github release page:
stropt binaries
using the tool
You can either use stropt by passing the source to analyze as a string:
struct bar { const char * str; short s1; int i1 short s2; int i2; };
Or you can pass a file in which the definition is contained:
git clone https://github.com/Abathargh/stropt go build // or, if you want to install this directly go install github.com/Abathargh/stropt
The tool can also provide a possible optimization for your types by using the -optimize flag, and it's aware of fields that are structs themselves:
Note that the verbose flag is used to show inner structs (and unions) along with their fields alignment and size.
what's next
This tool was written in go using the great modernc.org/cc C compiler front-end for parsing C code, and lipgloss from charmbracelet for the UI.
I'm writing this for myself but I'm happy to share it publicly; I'd like to make this a webapp for easier use directly in a browser, so probably that's the next thing I'm going to be working on!
The above is the detailed content of optimizing c structs layouts. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys
