Skip to content

[feat]: Add alignment support to TypeErasedComponentArray #379

@coderabbitai

Description

@coderabbitai

Objective

Add proper memory alignment support to the TypeErasedComponentArray class to handle components with over-alignment requirements.

Acceptance Criteria

  • Add alignment parameter to TypeErasedComponentArray constructor
  • Store component alignment alongside component size
  • Update m_componentData to use aligned allocator or custom aligned buffer
  • Modify insert/insertRaw methods to allocate with proper alignment
  • Update shrinkIfNeeded to reallocate with alignment preservation
  • Ensure swapComponents handles aligned storage correctly
  • Update memoryUsage calculation for aligned storage
  • Add tests for components with various alignment requirements

Dependencies

None currently identified, but this enhancement should be considered when:

  • Supporting native C++ components with over-alignment in the future
  • Expanding scripting support beyond .NET
  • Performance optimizations requiring specific memory alignment

Technical Details

Environment: C++ ECS Component System
Components:

  • engine/src/ecs/ComponentArray.hpp (TypeErasedComponentArray class declaration)
  • engine/src/ecs/ComponentArray.cpp (TypeErasedComponentArray implementation)

Resources:

  • std::aligned_alloc or operator new(size, alignment) for memory allocation
  • Custom aligned allocator for std::vectorstd::byte
  • Consider using alignas() specifications for future component types

Current implementation uses std::vectorstd::byte which only guarantees default alignment for std::byte. Components with alignas(64) or greater than alignof(std::max_align_t) may result in misaligned pointers and undefined behavior.

Backlink: Created from PR #352 discussion: #352 (comment)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

Status

Todo

Relationships

None yet

Development

No branches or pull requests

Issue actions