Remove host's source code from plugin to reduce file size

流过昼夜 提交于 2019-12-24 05:46:09

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/54064390/remove-hosts-source-code-from-plugin-to-reduce-file-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!