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
Small allocation fix in fbx loader #4494
Small allocation fix in fbx loader #4494
Conversation
First, |
Thanks Turo, indeed the current version leaks memory - I will have to re-iterate |
proper deallocation handled in new commit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stack-allocator looks great. I would recommend not to include a cpp-file. If you need this for linkage just define this in an inline file and include this in the header.
Hi @kimkulling, the compilation issues above do not make too much sense, it looks like it's compiling with a cached old .h file. |
#define new_LazyObject new (allocator.Allocate(sizeof(LazyObject))) LazyObject | ||
#define new_Connection new (allocator.Allocate(sizeof(Connection))) Connection | ||
#define delete_LazyObject(_p) (_p)->~LazyObject() | ||
#define delete_Connection(_p) (_p)->~Connection() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object construction and destruction should be delegated to the function related to the allocator. Generally it would be better not to use macross like that overall.
// #include <new>
public:
template<class T, class... Args>
T* ConstructObject(Args&&... args)
{
return ::new(this->Allocate(sizeof(T))) T(std::forward<Args>(args)...);
}
template<class T>
void DestroyObject(T* obj) noexcept
{
obj->~T();
}
FreeAll(); | ||
} | ||
|
||
inline void *StackAllocator::Allocate(size_t byteSize) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function returns unaligned memory. It should take in additional argument called alignment and align memory internaly.
StackAllocator sa;
char* c = new (sa.Allocate(sizeof(char))) char(1);
std::string* s = new (sa.Allocate(sizeof(std::string))) std::string("str"); // This is UB?
std::cout << reinterpret_cast<size_t>(s) % alignof(std::string) << '\n';
Will get fixed by #5096 |
This PR addresses a problem with loading very large binary fbx files. We had a case where the parsing code performed one billion(!) single small allocation which eventually crashed on any system.
This PR introduces class StackAllocator which can be used to allocate many small objects in a deque pattern. After parsing, the single objects are not de-allocated individually but instead the whole allocator is discarded.
This fix not only allows for loading much larger fbx files (in our case ~16GB with 3 million individual parts) but also improves parsing speed significantly on all other medium to large fbx files.