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

Improve binary size #4025

Open
ainar-g opened this issue Dec 27, 2021 Discussed in #4024 · 2 comments
Open

Improve binary size #4025

ainar-g opened this issue Dec 27, 2021 Discussed in #4024 · 2 comments
Assignees
Labels
infrastructure CI, CD, releases, etc. P3: Medium research Looking up ways to improve the product in the future.
Milestone

Comments

@ainar-g
Copy link
Contributor

ainar-g commented Dec 27, 2021

Discussed in #4024

Originally posted by jamesmacwhite December 27, 2021
First of all, I just want to say AdGuard Home is great and it's really exciting to see all the constant development and features constantly being rolled out.

Once thing I've noticed is the binary size seems to be growing a fair bit. Comparing the last two latest stable releases 0.106.3 to 0.107.0 for ARMv7.

  • 0.106.3 - 18.4 MB
  • 0.107.0 - 32.1 MB

Roughly a 57% increase. Now for most contexts and platforms, this isn't really an issue. An increase of 13.7 MB in the context of today's world with data centres with literally petabytes of storage, it's nothing! However, for more embedded platforms such as routers which will have limited flash space, this is quite significant. There are of course ways to extend flash storage space with additional storage partitions and such, but it did make me think about the binary size itself.

With the great development cycle and constant releases and improvements is the binary size just going to continue to grow? I'm not really that familiar with Go so please forgive my ignorance. It is my understanding that because the binary is statically linked and includes the Go runtime, it will be much heavier, but it does raise the question whether at some point the binary size will make running on embedded products like routers very difficult.

Either way, just thought it was an interesting topic, thanks for anyone's contributions!

Thoughts

The two main suspects are the packed front-end and module github.com/google/gopacket, which has some fairly large tables of some information. But perhaps there are others.

@ainar-g ainar-g added P3: Medium infrastructure CI, CD, releases, etc. research Looking up ways to improve the product in the future. labels Dec 27, 2021
@ainar-g ainar-g self-assigned this Dec 27, 2021
@ainar-g ainar-g added this to the v0.108.0 milestone Dec 27, 2021
@siavashs
Copy link

I was able to compress the binary using upx, in this case the mipsel binary was compressed on amd64:

> upx AdGuardHome
                       Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2022
UPX git-fdec47  Markus Oberhumer, Laszlo Molnar & John Reiser   Nov 16th 2022

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
  38207488 ->  11077244   28.99%  linux/mipsel   AdGuardHome                   

Packed 1 file.

WARNING: this is an unstable beta version - use for testing only! Really.

The binary was shrunk from 36M to 11M!

I've tested the binary and it seems to be working fine so far, I recommend to compress the beta releases using this method first and later do the same for stable releases after proper testing.

I'll report if I notice any issues with my setup using the compressed binary.

@tsl0922
Copy link

tsl0922 commented Jun 27, 2023

This is my patch to embed a zipped version of the static files to the binary.

The compiled binary size reduced from 28.25M to 20.69M (-26.76%, mipsle).

zip -r embed.zip build
--- a/main.go
+++ b/main.go
@@ -1,7 +1,9 @@
 package main
 
 import (
-	"embed"
+	"archive/zip"
+	"bytes"
+	_ "embed"
 
 	"github.com/AdguardTeam/AdGuardHome/internal/home"
 )
@@ -10,9 +12,14 @@ import (
 // internal directory and the embed package is unable to embed files located
 // outside of the same or underlying directory.
 
-//go:embed build
-var clientBuildFS embed.FS
+//go:embed embed.zip
+var b []byte
 
 func main() {
-	home.Main(clientBuildFS)
+	f, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
+	if err != nil {
+		panic(err)
+	}
+
+	home.Main(f)
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
infrastructure CI, CD, releases, etc. P3: Medium research Looking up ways to improve the product in the future.
Projects
None yet
Development

No branches or pull requests

3 participants