Skip to content

Reduce padding in _PyCfgBasicblock #160132

Closed

Description

@ashm-dev

Feature or enhancement

On my x86-64 build, _PyCfgBasicblock in Python/flowgraph.c occupies 72 bytes. The layout reported by pahole contains:

  • A 4-byte alignment gap immediately after b_label, at offsets 12–15.
  • Five one-bit flags starting at offset 64.
  • Trailing padding bringing the total size to 72 bytes.

Moving the five bit-fields immediately after b_label should fill the existing gap and reduce the structure to 64 bytes on this platform, without changing the offsets of the remaining fields.

This would save 8 bytes per CFG block before allocator rounding. With the standard 64-bit pymalloc size classes, the allocation would also move from the 80-byte class to the 64-byte class.

The proposed change is limited to the layout of this internal structure. No compiler behavior or public API changes are intended. Compilation performance has not been benchmarked.

❯ pahole -C _PyCfgBasicblock Python/flowgraph.o
struct _PyCfgBasicblock {
	struct _PyCfgBasicblock *  b_list;               /*     0     8 */
	_PyJumpTargetLabel         b_label;              /*     8     4 */

	/* XXX 4 bytes hole, try to pack */

	struct _PyCfgExceptStack * b_exceptstack;        /*    16     8 */
	cfg_instr *                b_instr;              /*    24     8 */
	struct _PyCfgBasicblock *  b_next;               /*    32     8 */
	int                        b_iused;              /*    40     4 */
	int                        b_ialloc;             /*    44     4 */
	uint64_t                   b_unsafe_locals_mask; /*    48     8 */
	int                        b_predecessors;       /*    56     4 */
	int                        b_startdepth;         /*    60     4 */
	/* --- cacheline 1 boundary (64 bytes) --- */
	unsigned int               b_preserve_lasti:1;   /*    64: 0  4 */
	unsigned int               b_visited:1;          /*    64: 1  4 */
	unsigned int               b_except_handler:1;   /*    64: 2  4 */
	unsigned int               b_cold:1;             /*    64: 3  4 */
	unsigned int               b_warm:1;             /*    64: 4  4 */

	/* size: 72, cachelines: 2, members: 15 */
	/* sum members: 60, holes: 1, sum holes: 4 */
	/* sum bitfield members: 5 bits (0 bytes) */
	/* padding: 4 */
	/* bit_padding: 27 bits */
	/* last cacheline: 8 bytes */
};

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added a commit that references this issue on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions