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

Add alignment support to vec #1110

Closed

Conversation

ZeroErrors
Copy link
Contributor

Fixes: #478

Changes

This change adds memory alignment support to vec which in turn fixes alignment support for components stored in tables.

Changes to vec.

  1. Added ecs_size_t alignment argument alongside ecs_size_t size argument in vec API.
  2. ecs_vec_t stores both void* array and void* mem.
    array is the same as before, it is the pointer to the data. mem is the original pointer that was allocated, this is used so we have the correct address to be able to realloc or free the memory.
  3. Vec allocates a buffer of size * elem_count + alignment this is the void* mem pointer and then shifts the pointer in order to have the correct alignment for void* array.
  4. When reallocating the vector a memmove may be necessary in order to correct for a different alignment of the new pointer.

This increases the memory usage of both the vec struct and the allocated arrays.

Since hashmap uses vec internally it also now has alignment support.

@SanderMertens
Copy link
Owner

Hm, I intentionally replaced the old vector code (which had alignment support) with a new simpler vector which simplified code & improved performance (likely due to the reduced memory footprint of table columns). I'm not sure if I want to add that back into the core.

My plan for supporting components with >16 byte alignment is pluggable storages, where you'll be able to implement your own data structures / allocation schemes.

@ZeroErrors
Copy link
Contributor Author

ah, yeah that makes sense.

@ZeroErrors ZeroErrors closed this Jan 3, 2024
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.

Incorrect memory alignment of components.
2 participants