-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
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
Core: Vector, Matrix, Quaternion and Color instanceof #24342
Core: Vector, Matrix, Quaternion and Color instanceof #24342
Conversation
thanks @pschroen!
Could you expand on "you do need all those classes" a bit more here? For math classes I think I follow, you're probably not going to do anything practical with three.js without math classes. But things like THREE.SkinnedMesh, THREE.Points, or THREE.Lines are only sometimes used — the renderer doesn't need them, but it needs to detect what they are when it gets them as input. These are cases where After this change, what should be tree-shaken that wasn't before? |
6e51f78
to
e0126cd
Compare
Hey @donmccurdy 👋, True if there isn't any performance improvement or a big difference in tree-shaking then I personally do like the This is more of a follow-up PR to @mrdoob's #24199 (comment) At the very least if we don't find For tree-shaking I've tested this PR in both Rollup and webpack just now and there's no difference in either, which is expected with this PR, as the It is worthwhile noting though that adding |
Sorry but I don't see the purpose of this PR, is it just to make the code a little bit cleaner?
This statement is wrong. https://github.com/marcofugaro/treeshake-test/blob/master/src/index-instanceof.js You can test it yourself by running |
@@ -5,7 +5,8 @@ class Vector3 { | |||
|
|||
constructor( x = 0, y = 0, z = 0 ) { | |||
|
|||
Vector3.prototype.isVector3 = true; | |||
// @deprecated | |||
Object.defineProperty( this, 'isVector3', { value: true } ); |
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.
this creates performance issues, see #21284
Hey @marcofugaro, I was tree-shaking just these math classes with Perhaps I should have created this as a Draft PR for discussion. Tree-shaking these math classes with It was also to test performance, but if we already know it's worse for performance we can close this issue then. |
But can we not replace these |
Why? Could you explain the purpose of this? |
Because that was the ask as a possible alternative approach to solving #24167, this is less about tree-shaking really, it only came-up in our tree-shaking conversation because of I will say one last thing here though about all the confusion of Just as in OGL, we would never use To make the renderer fully tree-shakeable we'll need to essentially remove all the type checks and invert the design. In other words, like with making the shaders tree-shakeable, all renderer modules will need to be self-contained with the context passed to them, rather than one giant renderer, it's the opposite approach really, but that's a conversation for another day. 🫠 |
Related issues: #24219 #24199 #24167 #24199 (comment)
Description
Defining
.prototype.is*
inside the constructor is a bit of a hack, really the.prototype.*
properties should be defined outside the classes once like in the rest of the library for consistency. 🙄Given that, I've gone ahead and trying the
instanceof
method here, in my opinion I think usinginstanceof
with tree-shaking is fine, because really you do need all those classes if you have code referencing them. Especially for core classes that are needed anyways, and many classes are already importing the classes used with their respective.is*
properties anyways.The syntax for using
instanceof
is often simpler as well without the need for checking the value first or use of optional chaining.The epic #9310 from @Rich-Harris I would argue is a stopgap solution, it is possible to write a tree-shakeable library with
instanceof
, it's more of a design problem with the renderer, thus the stopgap solution. For example in OGL the renderer there only needs aVec3
.https://github.com/oframe/ogl/blob/master/src/core/Renderer.js
This is a much larger discussion for a different issue, but wanted to mention it here as it's related to the use of
instanceof
. Though for comparison OGL doesn't use anyinstanceof
's either.Note I didn't do
.isTexture
, I feel like I'm opening Pandora's box here, so let's see what everyone thinks of this change first.Also if we were to completely remove the
.is*
properties it would be a breaking change, they are used in the examples, tests, docs and more than likely other people are using them.It's up for debate on whether this actually has any performance improvement or not. 😊
Finally, if
instanceof
doesn't work-out, I would at-least prefer setting the non-enumerable property withObject.defineProperty/ies()
inside the constructor instead, as is done withLOD
andNodeCode
already.three.js/examples/jsm/nodes/core/NodeCode.js
Line 9 in 3a76bc7