Skip to content

Module binary size #376

@ldez

Description

@ldez

不具合の説明と期待する動作

Hello,

I think you know who I am, but I'm the maintainer of lego.

We detected a problem with the size of github.com/sacloud/iaas-api-go dependency, because of:

  1. The dependencies of the packages fake and trace inside /helper/api/caller.go.
  2. The init function inside zz_api_ops.go

The current size of this module inside the lego binary is 3.65 MB (which is huge).

Fake and Trace

fake and trace are options of the API client, but as they are explicitly defined inside the newCaller constructor,
the compiler will add them to the binary.

if opts.TraceAPI {
// note: exact once
trace.AddClientFactoryHooks()
}
if opts.FakeMode {
if opts.FakeStorePath != "" {
fake.DataStore = fake.NewJSONFileStore(opts.FakeStorePath)
}
// note: exact once
fake.SwitchFactoryFuncToFake()
SetupFakeDefaults()
}

fake.PowerOnDuration = time.Millisecond
fake.PowerOffDuration = time.Millisecond
fake.DiskCopyDuration = time.Millisecond

The dependencies on those packages represent more than 1.3 MB inside a binary.

I can bypass this by creating a custom NewCallerWithOptions inside lego.

init function

The init function inside zz_api_ops.go is a problem because it explicitly uses all operation structures (ArchiveOp, AuthStatusOp, etc.).
The operation structures depend on the other generated structures.

In the end, all the structures are inside the binary.

https://github.com/sacloud/iaas-api-go/blob/main/zz_api_ops.go#L28-L413

Also, init function inside a library should be avoided because there is no way for client consumers to avoid calling it.

The dependencies on those packages represent more than 1.45 MB inside a binary.

There is no workaround here because we cannot skip the init function.


I locally use a customized version of NewCallerWithOptions and a modified version of github.com/sacloud/iaas-api-go without the init function (note I added the initialization of the factory for the DNS part inside lego).

The result: 3.65 MB (initial size) -> 2.24 MB (custom NewCallerWithOptions) -> 781.57 KB (drop init function).

781.57 KB is still not extra light, but this is a huge step.

But sadly, this only works because I modified locally zz_api_ops.go inside the github.com/sacloud/iaas-api-go module.


I don't think you can currently do something to change that,
but I wanted to inform you of the situation and maybe discuss the topic a bit with you.

Side note: your API documentation doesn't describe response schemas.

再現手順

  1. Clone lego
  2. run make build
  3. do the following to display the size
go install github.com/Zxilly/go-size-analyzer/cmd/gsa@latest
gas dist/lego
# OR gas --web dist/lego
# OR gas --tui dist/lego

動作環境

- ライブラリのバージョン:
- ツール・言語のバージョン:
- OSのバージョン:

設定やコード

NA

ログ

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementあったらいいかも?

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions