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

Dead code elimination in Example #962

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kidandcat
Copy link

Because Go compiler has dead code elimination, if we encapsulate the backend code in this if, as it is based on a constant value, the compiler will remove all code related to what's inside that if when compiling for wasm, leading to considerable size reduction.

(In my playground app it reduces size from 21MB to 17MB uncompressed)

Because Go compiler has dead code elimination, if we encapsulate the backend code in this if, as it is based on a constant value, the compiler will remove all code related to what's inside the if when compiling for wasm, leading to considerable size reduction.

(In my playground app it reduces size from 21MB to 17MB uncompressed)
@oderwat
Copy link
Contributor

oderwat commented Jun 2, 2024

This is very interesting We use build tags for all larger project to separate backend and frontend code, so this has no effect. But I tested it with https://github.com/oderwat/go-guess-the-number-app, and it reduces app.wasm from 16 MB to 5.1 MB, which is very cool.

	if !app.IsServer {
		return // let dead code elimination optimize the frontend code
	}

P.S.: I tried to use the same concept on our much larger apps and found that it does reduce the size also considerably but using build tags reduces the size even more. In one example with a private project (AI frontend) of mine I get 21 MB without any optimization, 8.3 MB using app.IsServer in main.go and 6.8 MB with our code separation using the WASM build flag.

P.P.S.: Using the build tag is demonstrated here: https://github.com/oderwat/go-nats-app which recently was update to V10.

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