-
Notifications
You must be signed in to change notification settings - Fork 77
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
aTom3333
wants to merge
5
commits into
chunky-dev:master
Choose a base branch
from
aTom3333:texture-material-cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
leMaik
reviewed
May 22, 2021
aTom3333
force-pushed
the
texture-material-cache
branch
from
May 27, 2021 21:16
e467828
to
5b18d67
Compare
The weak keys part… The existing |
aTom3333
force-pushed
the
texture-material-cache
branch
from
August 7, 2021 13:37
5b18d67
to
0ca4d3b
Compare
…Material based on the same Texture
leMaik
force-pushed
the
texture-material-cache
branch
from
October 27, 2023 21:58
0ca4d3b
to
c1cd78d
Compare
leMaik
reviewed
Oct 27, 2023
@@ -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' |
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.
Reminder to self: I'll have to add this library to the latest.json
and snapshot.json
and host it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A number of entities need to create
TextureMaterial
to wrap theirTexture
when callingprimitives
. The issue is that a lot of thoseTextureMaterial
are created from the sameTexture
meaning they are distinct instances that are identical.I have added a
TextureMaterial
cache to ensure a singleTextureMaterial
instance exist for a givenTexture
, doing this divides the number ofTextureMaterial
instances by 10, saving memory.Additionally, by not using the cache for
Texture
s that I know aren't shared (like theSignTexture
, 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 ofMaterial
would reduce the size of this palette.