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

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

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录