Skip to content

Refactor embedded assets and drop unnecessary dependencies #34692

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

Merged
merged 11 commits into from
Jun 12, 2025

Conversation

wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented Jun 11, 2025

Benefits:

  1. smaller binary size (reduces more than 1MB)
  2. better control of the assets details
  3. fewer unmaintained dependencies
  4. faster startup if the assets are not needed
  5. won't hang up editors when open "bindata.go" by accident

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jun 11, 2025
@wxiaoguang wxiaoguang force-pushed the refactor-embed-gzip branch 2 times, most recently from ae8ebc4 to 366982b Compare June 11, 2025 07:32
@wxiaoguang wxiaoguang force-pushed the refactor-embed-gzip branch from 366982b to 2f7e94d Compare June 11, 2025 07:42
@wxiaoguang wxiaoguang changed the title Refactor embeded assets and drop unnecessary dependencies Refactor embedded assets and drop unnecessary dependencies Jun 11, 2025
@wxiaoguang wxiaoguang force-pushed the refactor-embed-gzip branch 3 times, most recently from e40c71e to 2d0e6cb Compare June 11, 2025 08:16
@wxiaoguang wxiaoguang force-pushed the refactor-embed-gzip branch from 2d0e6cb to 1199e94 Compare June 11, 2025 08:54
@silverwind
Copy link
Member

silverwind commented Jun 11, 2025

How does this work on a high level? Put gzipped assets into go:embed?

Where does the size benefit come from?

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Jun 11, 2025

How does this work on a high level? Put gzipped assets into go:embed?

See assetfs.GenerateEmbedBindata

Where does the size benefit come from?

Same as above, now:

  1. we use gzip's "best compression" (just saw that you also submitted the "best compression" PR in upstream)
  2. we use our own bindata format, which is more efficient
  3. no duplicate generated code, all bindata packages share the same "assetfs" code

@silverwind
Copy link
Member

silverwind commented Jun 11, 2025

What's the benefit over using something more established like https://github.com/vearutop/statigz? I guess we would need a webpack gzip plugin, but might still be worth it.

Also, I wonder whether we could eliminate the go:generate step completely somehow.

@wxiaoguang
Copy link
Contributor Author

What's the benefit over using something more established like https://github.com/vearutop/statigz? I guess we would need a webpack gzip plugin, but might still be worth it.

It will bloat the binary about 20MB+

Also, I wonder whether we could eliminate the go:generate step completely somehow.

Not this PR's scope.

@silverwind
Copy link
Member

silverwind commented Jun 11, 2025

It will bloat the binary about 20MB+

If we make webpack write the .gz files and delete the original output files, I guess output size should be pretty similar.

@wxiaoguang
Copy link
Contributor Author

It will bloat the binary about 20MB+

If we make webpack write the .gz files and delete the original assets, I guess output size should be pretty similar.

There is no feasible plan at the moment, see the discussions in #26533

@wxiaoguang
Copy link
Contributor Author

Any other questions?

@lunny lunny added this to the 1.25.0 milestone Jun 11, 2025
@lunny

This comment was marked as outdated.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jun 11, 2025
@wxiaoguang
Copy link
Contributor Author

$ ./gitea embedded extract --dest-dir ./test templates/install.tmpl
Extracting to ./test:
test/templates/install.tmpl

$ cat test/templates/install.tmpl
{{template "base/head" .}}
<div role="main" aria-label="{{.Title}}" class="page-content install">
	<div class="ui grid install-config-container">

@hiifong
Copy link
Member

hiifong commented Jun 12, 2025

Please ignore my comment, the command parameters I executed are incorrect

@wxiaoguang
Copy link
Contributor Author

Hmm, found another problem with **, will fix in a new commit.

@github-actions github-actions bot added the modifies/cli PR changes something on the CLI, i.e. gitea doctor or gitea admin label Jun 12, 2025
@wxiaoguang
Copy link
Contributor Author

Hmm, found another problem with **, will fix in a new commit.

Made more fixes in 9981619

@wxiaoguang wxiaoguang requested a review from hiifong June 12, 2025 02:10
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jun 12, 2025
@wxiaoguang wxiaoguang enabled auto-merge (squash) June 12, 2025 03:05
@wxiaoguang wxiaoguang disabled auto-merge June 12, 2025 03:26
@wxiaoguang wxiaoguang added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jun 12, 2025
@wxiaoguang wxiaoguang enabled auto-merge (squash) June 12, 2025 03:35
@wxiaoguang wxiaoguang merged commit 65986f4 into go-gitea:main Jun 12, 2025
26 checks passed
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jun 12, 2025
@wxiaoguang wxiaoguang deleted the refactor-embed-gzip branch June 12, 2025 06:44
zjjhot added a commit to zjjhot/gitea that referenced this pull request Jun 13, 2025
* giteaofficial/main:
  [skip ci] Updated translations via Crowdin
  Improve the performance when detecting the file editable (go-gitea#34653)
  Fix various problems (go-gitea#34708)
  Refactor embedded assets and drop unnecessary dependencies (go-gitea#34692)
  Bump minimum go version to 1.24.4 (go-gitea#34699)
  Update JS deps (go-gitea#34701)
  Fix markdown wrap (go-gitea#34697)
  [skip ci] Updated translations via Crowdin
  frontport changelog (go-gitea#34689)
  Improve instance wide ssh commit signing (go-gitea#34341)
@silverwind
Copy link
Member

We could consider replacing gzip with zstd for more size savings and better page loading performance. 71% of browsers globally support content-encoding: zstd.

zstd encoding currently needs a third-party module, but it's very likely that it will be added in golang stdlib at some point: golang/go#62513.

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Jun 13, 2025

We could consider replacing gzip with zstd for more size savings and better page loading performance.

Then the key point is not here, but the "gzip middleware"

71% of browsers globally support content-encoding: zstd.

IMO it is still new, the supported browsers were released in recent year

zstd encoding currently needs a third-party module, but it's very likely that it will be added in golang stdlib at some point: golang/go#62513.

TBH I don't have any expectations for Golang's feature, when it is ready, we use it; if it isn't ready, we use others.

@silverwind
Copy link
Member

silverwind commented Jun 13, 2025

Then the key point is not here, but the "gzip middleware"

In any case, now that we have all this compression code in first-party, it should be easy to replace the encoding.

IMO it is still new, the supported browsers were released in recent year

I think the value will be > 80% by the time 1.25 releases.

TBH I don't have any expectations for Golang's feature, when it is ready, we use it; if it isn't ready, we use others.

Me neither, I expect the go feature earliest in a year or two. But it's still good to know that we can eventually eliminate the third-party dependency.

@silverwind
Copy link
Member

I think the value will be > 80% by the time 1.25 releases.

Maybe not, given that Safari lacks support: https://caniuse.com/zstd. But I guess the usage share of Safari users of gitea may be lower then the global average 😆.

@wxiaoguang wxiaoguang mentioned this pull request Jun 15, 2025
wxiaoguang added a commit that referenced this pull request Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/cli PR changes something on the CLI, i.e. gitea doctor or gitea admin modifies/dependencies modifies/go Pull requests that update Go code modifies/internal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants