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

Cache TextureMaterial to prevent their proliferation #961

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

aTom3333
Copy link
Member

@aTom3333 aTom3333 commented May 22, 2021

A number of entities need to create TextureMaterial to wrap their Texture when calling primitives. The issue is that a lot of those TextureMaterial are created from the same Texture meaning they are distinct instances that are identical.

I have added a TextureMaterial cache to ensure a single TextureMaterial instance exist for a given Texture, doing this divides the number of TextureMaterial instances by 10, saving memory.
Additionally, by not using the cache for Textures that I know aren't shared (like the SignTexture, cf second commit), the cache is not filled with entries that won't bring any benefits and stays small.

To implement the cache I needed a hash map where the values were weak references (the built-in WeakHashMap has weak keys but strong values) so I added a new dependency on apache common collections. You decide if that is acceptable.

For 1 region of greenfield, I went from 90k TextureMaterial instances to 9k going from 5MB to 500kB.
For 4 regions of greenfield, I went from 160k TextureMaterial instances to 16k, going from 9MB to 900kB.

The additional memory overhead of the cache is was only of 35kB for 1 region of greenfield (didn't measure for 4 regions).

The savings don't seem to scale that much and aren't that big but that's probably still worth it.

Another benefit of reducing the number of instances of Material is for an hypothetical BVH implementation that wants to store the primitives compressed by replacing the material by an index into a palette of materials, having less instances of Material would reduce the size of this palette.

@leMaik
Copy link
Member

leMaik commented May 29, 2021

The weak keys part… The existing TextureCache is buggy then 😱

@@ -14,6 +14,7 @@ configurations {
dependencies {
implementation 'it.unimi.dsi:fastutil:8.4.4'
implementation 'org.apache.commons:commons-math3:3.2'
implementation 'org.apache.commons:commons-collections4:4.4'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder to self: I'll have to add this library to the latest.json and snapshot.json and host it.

leMaik

This comment was marked as outdated.

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.

2 participants