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

perf: optimize RemoveUnusedMaterialProperties #1326

Merged
merged 7 commits into from
Nov 7, 2024

Conversation

bdunderscore
Copy link
Contributor

This change improves the performance of RemoveUnusedMaterialProperties,
by using native SerializedProperty calls to move items, rather than a
C#-side copy function. This avoids a lot of marshalling and GC.

This provides a performance improvement of 340ms -> 68ms on my main
avatar.

This change improves the performance of RemoveUnusedMaterialProperties,
by using native SerializedProperty calls to move items, rather than a
C#-side copy function. This avoids a lot of marshalling and GC.

This provides a performance improvement of 340ms -> 68ms on my main
avatar.
@bdunderscore bdunderscore marked this pull request as ready for review November 4, 2024 04:07
This pass was obtaining the blendshape buffer for each vertex, then processing those buffers.
However, in practice, most vertices share the same buffer; this pass ignored this, and redid
the same work thousands of times as a result.

This reduces processing time from ~370ms to ~22ms on my main avatar.
@bdunderscore
Copy link
Contributor Author

I will be making further changes; please hold off on merging for now.

@bdunderscore bdunderscore marked this pull request as draft November 4, 2024 05:18
This demonstrates that there's a lack of unit tests for this codepath; I'll leave it in draft until I have a good way to test.
@bdunderscore
Copy link
Contributor Author

This change needs additional unit tests before it should be merged.

// This MoveArrayElement call effectively rotates the range of elements
// between from and to. As such, since we are iterating from the start,
// each prior rotation doesn't affect the "from" index of subsequent elements.
props.MoveArrayElement(from, to);
Copy link
Owner

Choose a reason for hiding this comment

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

I did not know this function. thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's still more costly than ideal, but much better than before...

Comment on lines +26 to +45
- _AUnusedTex0:
m_Texture: {fileID: 2800000, guid: 5ab62c479f88d4aada9d7ae22a8224d5, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _AUnusedTex1:
m_Texture: {fileID: 2800000, guid: 33780cc7041974dcea00d6a3c3de4a3e, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _AUnusedTex2:
m_Texture: {fileID: 2800000, guid: eed4f0bcec3d849eba0543fba412318d, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: 8558587c82bb240ba949e06cc79f1f10, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex2nd:
m_Texture: {fileID: 2800000, guid: 576604d4986f24aaab960da95142db80, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we intersperse these? I don't think this will trigger all codepaths in the new optimized code.

@anatawa12
Copy link
Owner

I have added tests for RemoveUnusedMaterialProperties and I confirmed both moving and removing pass will be checked with the unit test with debugger and coverage report.

image

@bdunderscore
Copy link
Contributor Author

bdunderscore commented Nov 7, 2024

Good to know we have coverage; I still think we should assert on the main purpose of this code, which is to actually remove those properties...
If you don't have time I'll probably get to it on saturday (JST). Thanks for writing the tests!

@anatawa12
Copy link
Owner

I think it's the most important to not break existing avatar so I implemented test for that.

I'm considering how do we implement test for removing property.
GetFloat of Material will return 0 with error log if it's not defined in shader even if material have definition for that so I may have to use SerializedObject in test.

@bdunderscore bdunderscore marked this pull request as ready for review November 7, 2024 13:55
@anatawa12
Copy link
Owner

Thank you for performance improvements and checking tests.

@anatawa12 anatawa12 merged commit 67a5b4f into anatawa12:master Nov 7, 2024
9 checks passed
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