scottlamb / moonfire-nvr

Moonfire NVR, a security camera network video recorder
Other
1.28k stars 143 forks source link

shrink moonfire-nvr binary #70

Open scottlamb opened 5 years ago

scottlamb commented 5 years ago

There's some significant bloat in the binary right now:

These aren't causing material problems right now; the big binaries are just a bit embarrassing. So no particular size we need to get under or anything.

$ cargo bloat --release --crates
Compiling ...
Analyzing target/release/moonfire-nvr

File  .text     Size Crate
0.8%  21.0%   1.0MiB std
0.4%  12.0% 602.5KiB moonfire_db
0.4%  10.3% 521.0KiB protobuf
0.2%   5.7% 286.6KiB regex
0.2%   5.3% 268.5KiB moonfire_nvr
0.2%   4.8% 242.2KiB cursive
0.2%   4.3% 215.5KiB regex_syntax
0.1%   3.7% 187.3KiB h2
0.1%   3.0% 148.8KiB docopt
0.1%   2.4% 121.9KiB hyper
0.1%   2.3% 116.2KiB futures
...
scottlamb commented 4 years ago

The split debug symbols (aka "fission") would currently break backtraces, unfortunately:

https://github.com/rust-lang/backtrace-rs/issues/287

IronOxidizer commented 4 years ago

There are many (10+) duplicate packages with different versions. This is likely increasing the size to some degree. These can be reduced by using dependencies with matching dependency versions.

Here's a quick list:

ansi_term v0.11.0
ansi_term v0.9.0
error-chain v0.11.0
error-chain v0.12.2
proc-macro2 v0.4.30
proc-macro2 v1.0.10
quote v0.6.13
quote v1.0.3
rand v0.3.23
rand v0.4.6
rand v0.5.6
rand v0.7.3
rand_core v0.3.1
rand_core v0.4.2
rand_core v0.5.1
strsim v0.8.0
strsim v0.9.3
structopt v0.2.18
structopt v0.3.13
structopt-derive v0.2.18
structopt-derive v0.4.6
syn v0.15.44
syn v1.0.17
time v0.1.43
time v0.2.10
unicode-xid v0.1.0
unicode-xid v0.2.0

For the depedency graph, use cargo tree -d

scottlamb commented 4 years ago

Definitely not ideal, although I suspect none of the ones in that list are a major contribution to the binary size given the cargo bloat output.

scottlamb commented 3 years ago

As of 0.7.0, no duplicate packages!

[slamb@slamb-workstation ~/git/moonfire-nvr/server]$ cargo bloat --release --crates
    Finished release [optimized + debuginfo] target(s) in 0.05s
    Analyzing target/release/moonfire-nvr

File  .text     Size Crate
0.6%  18.7% 982.2KiB std
0.6%  16.8% 884.4KiB moonfire_db
0.3%  10.4% 545.1KiB protobuf
0.2%   6.4% 335.4KiB moonfire_nvr
0.2%   6.2% 328.0KiB clap
0.2%   4.7% 248.3KiB cursive_core
0.2%   4.5% 235.2KiB retina
0.1%   4.1% 213.7KiB tokio
0.1%   3.2% 170.8KiB hyper
0.1%   1.8%  93.0KiB backtrace
0.0%   1.1%  60.2KiB http
0.0%   1.1%  60.2KiB cursive
0.0%   1.0%  53.9KiB hashbrown
0.0%   1.0%  52.7KiB serde_json
0.0%   1.0%  51.3KiB rustc_demangle
0.0%   0.9%  47.6KiB url
0.0%   0.8%  42.9KiB miniz_oxide
0.0%   0.8%  40.2KiB addr2line
0.0%   0.7%  36.7KiB idna
0.0%   0.7%  35.3KiB sha2
0.4%  11.2% 589.7KiB And 88 more crates. Use -n N to show more.
3.4% 100.0%   5.1MiB .text section size, the file size is 152.2MiB

Note: numbers above are a result of guesswork. They are not 100% correct and never will be.

Biggest likely-fixable contributors that are still around: debug info (file size minus .text section is 147 MiB, so still the vast majority), protobuf, and cursive:

moonfire-db has grown, I think largely as a result of using JSON stuff in the schema (serde_json code is large). The v6->v7 schema upgrade funcs in particular are pretty large. Maybe eventually I can drop older schema upgrade code.

scottlamb commented 2 years ago

With 0406e09, the binary shrunk from 154 MiB to 70 MiB. I'm still hoping to reduce it further with split debug info. From this comment and this PR it looks like work is actively being done on that.