duanpu1111 2019-01-06 17:59
浏览 77
已采纳

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

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条回答 默认 最新

  • douyan8413 2019-01-06 19:06
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!