struct-layout

Optimize C struct layouts for performance, alignment, and ABI stability.

25|2|Updated Oct 23, 2024
One-click install
npx skills add https://github.com/zzxyb/wlframe --skill struct-layout
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: struct-layout
Source: https://github.com/zzxyb/wlframe/tree/main/.github/copilot/skills/struct-layout
Command: npx skills add https://github.com/zzxyb/wlframe --skill struct-layout

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses the complexities of designing and reviewing C struct layouts, ensuring optimal performance through careful field ordering, alignment, and cache locality. It helps prevent common pitfalls like poor memory access patterns and ABI instability.

Core Features & Use Cases

  • Struct Design: Guides the creation of new C structs with considerations for performance and maintainability.
  • Layout Review: Analyzes existing struct layouts to identify and rectify issues related to padding, cache misses, and field grouping.
  • ABI Stability: Provides rules for managing public struct layouts to maintain backward compatibility.
  • Use Case: When adding a new field to a frequently accessed struct, this Skill helps determine the best placement to avoid negatively impacting cache performance or breaking the public interface.

Quick Start

Review the proposed layout for the 'wlf_texture' struct to ensure hot fields are not buried behind cold metadata.

Frequently Asked Questions about struct-layout

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I optimize C struct layout for cache locality and performance?

Optimizing C struct layout for cache locality involves carefully ordering fields so hot-path data is grouped together, minimizing padding through proper alignment, and placing cold metadata separately to prevent cache misses during frequent access.

What is the best way to order fields in a C struct to maintain ABI stability?

To maintain ABI stability when ordering fields in a C struct, keep public layouts backward compatible by placing new fields at the end or using implementation pointers to hide internal state, ensuring existing binaries continue functioning without recompilation.

Why does my C struct have padding and how does it affect memory access patterns?

C struct padding occurs because the compiler inserts empty bytes to satisfy alignment requirements for specific data types, which can fragment memory and cause poor cache locality if frequently accessed hot fields are interspersed with unused padding gaps.

How do I place an intrusive list node or implementation pointer inside a C struct?

Placing intrusive list nodes or implementation pointers inside a C struct requires positioning them based on usage patterns, typically grouping internal linkage and hidden state away from public ABI-sensitive fields to balance maintainability and access speed.

Can I review an existing C struct layout to identify cache miss issues and poor field grouping?

Reviewing an existing C struct layout identifies cache miss issues by analyzing field grouping and alignment, ensuring hot-path data is not buried behind cold metadata, and rectifying padding to improve overall memory access patterns and performance.