Improve binary size #3310

Open
opened 2026-03-04 03:23:54 -05:00 by deekerman · 2 comments
Owner

Originally created by @ainar-g on GitHub (Dec 27, 2021).

Originally assigned to: @ainar-g on GitHub.

Discussed in https://github.com/AdguardTeam/AdGuardHome/discussions/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.

Originally created by @ainar-g on GitHub (Dec 27, 2021). Originally assigned to: @ainar-g on GitHub. ### Discussed in https://github.com/AdguardTeam/AdGuardHome/discussions/4024 <div type='discussions-op-text'> <sup>Originally posted by **jamesmacwhite** December 27, 2021</sup> 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!</div> ### 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.
Author
Owner

@siavashs commented on GitHub (Dec 21, 2022):

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.

@siavashs commented on GitHub (Dec 21, 2022): I was able to compress the binary using [`upx`](https://upx.github.io/), 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.
Author
Owner

@tsl0922 commented on GitHub (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)
 }
@tsl0922 commented on GitHub (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). ```bash zip -r embed.zip build ``` ```diff --- 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 in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/AdGuardHome#3310
No description provided.