从插件中删除主机的源代码以减小文件大小

问题描述:

I'm currently experimenting with golang's plugin system. A problem which I experienced in my testings is that the file size of the plugins is relativly big.

The application loading the plugin will be referenced as "host".

The host application itself is ~50MiB big since it is a web application and should be extended with plugin functionality.

I've implemented a small plugin loader to start the plugins up.

The plugins may use the already existing APIs in the application for example to access the database.

I've prepared a example plugin for this question. The plugin .so file size is ~39MiB. This gives me the reasonable suspicion that the plugin also contains source code from the host application.

Command used to create main.so:

go build -ldflags="-s -w" --buildmode=plugin main.go

Is it possible to "remove" the duplicated source code from the application to reduce file size since it is already loaded on runtime when the plugin gets loaded?

Plugin loader: https://github.com/jonasfranz/gitea/blob/feature/plugin/modules/plugins/loader.go

Example plugin: https://git.jonasfranz.software/JonasFranzDEV/giteaplugin

我目前正在尝试使用golang的插件系统。 我在测试中遇到的一个问题是插件的文件大小相对较大。 p>

加载插件的应用程序将被称为“主机”。 p>

主机应用程序本身约为50MiB,因为它是一个Web应用程序,应使用插件功能进行扩展。 p>

我已经实现了一个小型插件加载器来启动插件。 p>

插件可以使用应用程序中已经存在的API,例如 访问数据库。 p>

我已经为这个问题准备了一个示例插件。 插件.so文件大小为〜39MiB。 这使我有一个合理的怀疑,即该插件还包含来自主机应用程序的源代码。 p>

用于创建 main.so code>: p> 的命令

  go build -ldflags =“-s -w” --buildmode = plugin main.go 
  code>  pre> 
 
 

是否可以“删除” “从应用程序复制的源代码以减小文件大小,因为插件加载时已在运行时加载了该文件? p>

插件加载器: https://github.com/jonasfranz/gitea/blob/feature/plugin/modules/plugins/loader .go p>

示例插件: https:// git.jonasfranz.software/JonasFranzDEV/giteaplugin p> div>

Source code is not included in plugins. But what is included in them is their dependencies, recursively. This is so because there is no guarantee that the main app that loads the plugin also contains the dependencies, so to ensure the viability of the plugin, its dependencies must be self-contained.

This does not cause problems if the main app also include the same dependencies (with the same version), they will only be "instantiated" once in the go runtime, for details, see How do Go plugin dependencies work?

What to do in order to reduce plugins' sizes? Besides removing the debug information (what you did), you should minimize the dependencies.

This may require redesign and major changes both in the plugin or in the app you wish to create the plugin for. For example, plugins should not refer to "implementation" packages, plugins should only refer to "interface" packages. If interfaces and implementations are not separated, this may not be possible (hence may it be required to change the main app too).

You may also try utilities that try to compress binaries, for details see: Shrink your Go binaries with this one weird trick