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

zflecs: fix wrong alignment issue #303

Merged
merged 6 commits into from
May 31, 2023

Conversation

LukasKastern
Copy link
Contributor

@LukasKastern LukasKastern commented May 27, 2023

This is the flecs issue for reference.
Compiling flecs using -DFLECS_USE_OS_ALLOC already works around the issue.

But I've also implemented the allocation related os calls using a gpa to track memory leaks and guarantee correct alignment.
I haven't benchmarked the performance impact of using it. So it might make sense to make it optional 🤷.

This change should fix #284.

@LukasKastern LukasKastern changed the title zflecs: fix wrong alignment issue #284 zflecs: fix wrong alignment issue May 27, 2023
@michal-z
Copy link
Collaborator

Thanks for the PR!

So, this adds 128 bytes to each allocation, right? This seems expensive to me.

In other libs I use a hash map to track size and alignment for each allocation. Like here:
https://github.com/michal-z/zig-gamedev/blob/main/libs/zphysics/src/zphysics.zig#L625

So my approach adds 8 bytes per allocation + some hash map lookups.

I think this PR is good idea but I would prefer to use approach with the hash map. What do you think? Would you like to modify this PR?

@LukasKastern
Copy link
Contributor Author

So, this adds 128 bytes to each allocation, right? This seems expensive to me.

Oh sorry. I mixed bits/bytes up in this pull request. The max alignment should be 16 bytes not 128 lol.
I'll fix it up later today 😅.

Do you think 16 bytes of extra space for the allocation header is fine or should we use a hashmap anyways?

@michal-z
Copy link
Collaborator

Maybe we could use 8 bytes for a header and store there size (u48) and alignment (u16) that would be the best solution.

@LukasKastern
Copy link
Contributor Author

LukasKastern commented May 30, 2023

Maybe we could use 8 bytes for a header and store there size (u48) and alignment (u16) that would be the best solution.

That would also mean that the maximum alignment that flecs would be able to request is 8 bytes.
I'm gonna try that out and see if it works.

Glibc malloc seems to align to 16 bytes on 64bit platforms by default.

I guess we can expose the alignment as a build_option in case you have a component that requires a greater alignment.
At that point it might be simpler to just use a hashmap like you suggested tho'.

@michal-z
Copy link
Collaborator

michal-z commented May 30, 2023

I thought that flecs passes alignment for each allocation.

Let's go with 16 bytes headers.

@LukasKastern
Copy link
Contributor Author

Should be good now. Let me know if you would like me to change something.

# Conflicts:
#	libs/zflecs/src/zflecs.zig
@michal-z michal-z merged commit 94f8636 into zig-gamedev:main May 31, 2023
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.

zflecs: Components cannot contain zmath.F32x4
2 participants