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

Set _StaticBufferBuilder capacity via constructor when merging buffers to optimize memory allocations. #166

Merged
merged 10 commits into from Jan 16, 2023

Conversation

MeltyPlayer
Copy link
Contributor

@MeltyPlayer MeltyPlayer commented Jan 13, 2023

I've run into some issues in the past when exporting large GLB/GLTF models in x86 mode, since 32-bit applications only support a max of 2GB of memory. In this PR, I've made some minor optimizations to memory allocations in the GLB/GLTF/OBJ export logic that make it slightly more robust.

Changes:

  • Initialized the _StaticBufferBuilder in MergeBuffers() with the total capacity of all of the individual buffers. When adding to a common List beyond its capacity, it will double its capacity each time, potentially beyond what is actually needed. This change ensures that the List only uses the exact capacity it needs, making it slightly more robust to out of memory errors.
  • Adds a new test that generates and saves an arbitrarily large heightmap (1000x1000) to verify that saving large models works for GLB, GLTF, and OBJ.

@MeltyPlayer MeltyPlayer changed the title Added platforms for x64/x86, and fixed some OutOfMemory exceptions when saving models in x86 mode. Added platforms for x64/x86, and fixed some OutOfMemory exceptions when saving models in 32-bit mode. Jan 13, 2023
@MeltyPlayer MeltyPlayer changed the title Added platforms for x64/x86, and fixed some OutOfMemory exceptions when saving models in 32-bit mode. Added platforms for x64/x86 and fixed some OutOfMemory exceptions when saving models in 32-bit mode. Jan 13, 2023
@MeltyPlayer MeltyPlayer changed the title Added platforms for x64/x86 and fixed some OutOfMemory exceptions when saving models in 32-bit mode. Added platforms for x64/x86 and optimized memory allocations to fix OutOfMemory exceptions in 32-bit mode. Jan 13, 2023
@MeltyPlayer MeltyPlayer changed the title Added platforms for x64/x86 and optimized memory allocations to fix OutOfMemory exceptions in 32-bit mode. Optimized memory allocations to fix OutOfMemory exceptions in 32-bit mode. Jan 13, 2023
@vpenades
Copy link
Owner

Hm.... you're adding multiple features in a single PR, I think it would have been better to propose them in separated PRs.

Some of the optimizations seems good, but I don't like too much tagging the main projects as x86/x64 because it would prevent running the libraries on ARM cpus. If the purpose is testing, the main library projects shoud not be affected in any way.

@MeltyPlayer
Copy link
Contributor Author

Sounds good, I'll go ahead and remove the x86/x64 changes and split out the other changes.

@vpenades
Copy link
Owner

Just keep in mind the less files you change, the easier for me to review, and more chances to accept merging the PR

…that it's more robust.

Revert "Set up platforms for x86 and x64."

This reverts commit 2ab2abd.

Set up platforms for x86 and x64.
…irectly to the output FileStream rather than first to a MemoryStream and then copying.

Optimized some redundant memory allocations in the WavefrontWriter class to avoid OutOfMemory exceptions in 32-bit mode, which fixes some of the existing tests.

Made some more memory optimizations in the WavefrontWriter to limit how many objects are created and stored in memory at once.
…thod so that it's more robust."

This reverts commit ed4450a.
… write directly to the output FileStream rather than first to a MemoryStream and then copying."

This reverts commit 80d5c3a.
@MeltyPlayer MeltyPlayer changed the title Optimized memory allocations to fix OutOfMemory exceptions in 32-bit mode. Set _StaticBufferBuilder capacity via constructor when merging buffers to optimize memory allocations. Jan 15, 2023
@MeltyPlayer
Copy link
Contributor Author

Done--I've changed this to just make the buffer change and add the test, and pulled out the WavefrontWriter changes into a separate PR.

@vpenades
Copy link
Owner

vpenades commented Jan 15, 2023

It almost looks fine.

I feel like the memory calculator is not super precise, and I would increase the allocated memory a bit over what's needed, maybe by 1% with something like this:

reservedMemory = reservedMemory + (reservedMemory / 100); // increase by 1%

The other thing I would request is to move the unit tests from Toolkit to ThirdParty tests. There's already an entry for you.

Third party tests help me remember that some features are actually in use by other developers.

@MeltyPlayer
Copy link
Contributor Author

Sounds good, I've added the reserved memory change and moved the test.

@vpenades vpenades merged commit 29d6f50 into vpenades:master Jan 16, 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.

None yet

2 participants