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

SimplifyModifier: cache vertex indices to speed faces #22042

Merged
merged 6 commits into from
Jun 25, 2021

Conversation

NNskelly
Copy link
Contributor

Related issue: none

Description

Tell vertices what their index is while we're already touching all of them (O(n)), rather than searching for them 3 times per face (O(m*n)). I concede that bolting extra data onto an established object mid-run is architecturally dubious, but the time saved is on the order of minutes for a 300k vertex mesh.
Aside: I'm also doing some other work which is showing a further ~25% time save by keeping vertices sorted by edge cost (and in the process codifying Vertex.index as a member at construction time) rather than searching for lowest every collapse iteration, but the logical change is considerably deeper, so I'm testing the waters with this hopefully shoo-in change to be sure I have the process right before proposing anything larger.

@mrdoob
Copy link
Owner

mrdoob commented Jun 25, 2021

Adding properties to classes tends to have performance penalties.

However, I see that the Vertex class has an id and seems to be unused?

this.id = id; // old index id

Maybe we can just rename it to index?

@Mugen87
Copy link
Collaborator

Mugen87 commented Jun 25, 2021

BTW: Please modify examples/jsm/modifiers/SimplifyModifier.js . Files in examples/js are getting generated.

@NNskelly
Copy link
Contributor Author

pushed the change to jsm. I was actually wondering about the id field; found it when I was formally adding index as a more actively managed field. It's commented as being the original mesh vertex id, which is different from the order in vertices after a bunch are removed, and very different from the order in vertices in my ongoing work after vertices is sorted by edge cost. If the collective experts are willing to sign off that nobody is ever going to want to link back to the original mesh, I could easily repurpose that member. That said, the performance penalties introduced by making the engine track a dynamically added member are empirically far less than the performance hit of running linear-time searches.

@NNskelly
Copy link
Contributor Author

actually, why even debate this? Changed 'index' to 'newId' for consistency and added it to the constructor. That should satisfy performance concerns, and if whoever is responsible for the old id field wants it gone, that can be decoupled from this optimization.

@Mugen87
Copy link
Collaborator

Mugen87 commented Jun 25, 2021

I suggest to use id as mentioned earlier and not introduce a new field. If id is currently not used in the modifier's code, I think it's better to give it a new purpose.

@NNskelly
Copy link
Contributor Author

If you're confident id is unused, then sure. That said, if this change is taking use/ownership of id , I see no reason to pass it in the constructor when it's not needed until later on and on fewer instances. (init to -1 should be benign for now, and dovetail with the sorting work if that goes through later).

@mrdoob mrdoob added this to the r130 milestone Jun 25, 2021
@mrdoob mrdoob merged commit df13633 into mrdoob:dev Jun 25, 2021
@mrdoob
Copy link
Owner

mrdoob commented Jun 25, 2021

Thanks!

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.

3 participants