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

WebGLUniforms: Assign compareFunction lazily to retain tree-shaking. #28670

Merged
merged 3 commits into from
Jun 17, 2024

Conversation

CodyJasonBennett
Copy link
Contributor

Related issue: #24199

Description

Did a quick test for tree-shaking in core (importing a constant), and found the usual expected culprits (mutations from setting static properties in Texture, Euler, Object3D, and devtools), but also a seemingly low hanging fruit in WebGL source. Everything else is both annoying to fix and expected to be present in user-land code for rendering.

Copy link

github-actions bot commented Jun 16, 2024

📦 Bundle size

Full ESM build, minified and gzipped.

Filesize dev Filesize PR Diff
679.4 kB (168.3 kB) 679.4 kB (168.3 kB) +3 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Filesize dev Filesize PR Diff
457.4 kB (110.4 kB) 457.4 kB (110.4 kB) +10 B
@@ -569,6 +568,13 @@ function setValueT1( gl, v, textures ) {

}

if ( this.type === gl.SAMPLER_2D_SHADOW && emptyShadowTexture === null ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it also fix the tree shaking issue if we do the following?

let emptyTexture2D;

if ( this.type === gl.SAMPLER_2D_SHADOW ) {

    emptyShadowTexture.compareFunction = LessEqualCompare;
    emptyTexture2D = emptyShadowTexture;

} else {

    emptyTexture2D = emptyTexture;

}

I suspect tree-shaking breaks since the code accesses compareFunction right away. If we just move this bit to setValueT1(), we don't need an additional if check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, anything were we don't do a mutation in the top-level scope.

@Mugen87 Mugen87 added this to the r166 milestone Jun 17, 2024
@Mugen87 Mugen87 changed the title WebGLUniforms: lazily create shadow texture for tree-shaking Jun 17, 2024
@Mugen87 Mugen87 merged commit 8a7848a into mrdoob:dev Jun 17, 2024
12 checks passed
@CodyJasonBennett CodyJasonBennett deleted the webgl-treeshaking branch June 17, 2024 20:27
@mrdoob
Copy link
Owner

mrdoob commented Jun 18, 2024

So this PR allows DepthTexture to be tree-shaked?

@CodyJasonBennett
Copy link
Contributor Author

Yes, effectively. I'm seeing unreasonable effectiveness in Vite (importing from three) with the changes which tree-shakes down to REVISION, __THREE_DEVTOOLS__ and __THREE__. Before, it included Object3D etc. which is more expected. Trying to understand this.

@mrdoob
Copy link
Owner

mrdoob commented Jul 1, 2024

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Filesize dev Filesize PR Diff
457.4 kB (110.4 kB) 457.4 kB (110.4 kB) +10 B

How come this PR made the tree shaking number bigger and not smaller? 🤔

@CodyJasonBennett
Copy link
Contributor Author

That doesn't measure tree-shaking actually, but the output of one configuration of one tool. The untreeshaken size increases since we're adding a branch to the source.

It's like saying something affected device performance, but only accounting for one device/driver at one version.

@mrdoob
Copy link
Owner

mrdoob commented Jul 1, 2024

Oh, what should we do to measure tree shaking then...?

@CodyJasonBennett
Copy link
Contributor Author

CodyJasonBennett commented Jul 1, 2024

Either a matrix of tools and configuration, or be okay with measuring the lowest common denominator. That might mean testing tree-shaking in three as a whole rather than the practical case of rendering a scene with a WebGLRenderer, where this has no effect.

I test a bunch here for instance. https://github.com/CodyJasonBennett/treeshaking

@mrdoob
Copy link
Owner

mrdoob commented Jul 1, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants