draveness / blog-comments

面向信仰编程
https://draveness.me
140 stars 6 forks source link

Go 语言的动态库和插件系统 | Go 语言设计与实现 · /golang-plugin #196

Closed draveness closed 2 years ago

draveness commented 4 years ago

https://draveness.me/golang-plugin

Go 语言的动态库和插件系统

DDAAREN commented 4 years ago

2020-04-12 通读了一遍所有章节,收获很大,非常感谢

draveness commented 4 years ago

2020-04-12 通读了一遍所有章节,收获很大,非常感谢

感谢支持

ppd0705 commented 4 years ago

只写过Go hello world的人居然断断续续和跳跃着看过来了,这本书实在太硬核了,感谢作者带我们领略Go的精彩

Cherrison commented 4 years ago

能不能讲解一下 go sort包啊
为什么超过maxdepth 就要把quicksort变成heapsort啊 maxdepth 为什么样那样算 为什么是 2* ceil(log(n+1)) 看不懂也,网上也找不到 :cry:

draveness commented 4 years ago

能不能讲解一下 go sort包啊 为什么超过maxdepth 就要把quicksort变成heapsort啊 maxdepth 为什么样那样算 为什么是 2* ceil(log(n+1)) 看不懂也,网上也找不到 😢

直接看源代码呗... https://github.com/golang/go/blob/master/src/sort/sort.go#L183

draveness commented 4 years ago

为什么小于12要用希尔插入排序 , 普通on2的不也差不多嘛 才大小12

这个问题非常好,我觉得这个例子用来调试源码非常合适,你可以在 这里 看到很多 Benchmark,把 Shellsort 去掉看看对 Benchmark 有影响么,几个相关的 Commits

第二个 Commits 是最早的代码,你可以看看这两个 Commits 的相关内容,其中有一篇 1993 年的论文 Engineering a Sort Function JON L. BENTLEY & M. DOUGLAS McILROY,我没看过,但是我猜测可能是这么实现的源头。

UPDATES:这个 12 应该只是工程上 Benchmark 的最终决定,论文中使用了两个值,分别是 7 和 40 来对数组的大小进行划分:

Although the program lists only four different values of n, qsort recursively calls many lesser values of n, which should flush out any bugs lurking near the algorithmic breakpoints at n = 7 and n = 40. Function test checks answers against a trusted sort; we found several bugs this way

image

最开始 Go 语言团队应该是直接使用了书中的实例代码,但是后来经过测试发现使用 12 可以跑出更好的 Benchmark。我的理解的是工程上的很多决定就是以 Benchmark 为导向的,设定一个测试用例,然后尽可能地提高效率,当然用例到最后也只是一个参考。

Cherrison commented 4 years ago

@draveness

能不能讲解一下 go sort包啊 为什么超过maxdepth 就要把quicksort变成heapsort啊 maxdepth 为什么样那样算 为什么是 2* ceil(log(n+1)) 看不懂也,网上也找不到 😢

直接看源代码呗... https://github.com/golang/go/blob/master/src/sort/sort.go#L183

看不懂源码只写了是什么和 怎么做 但没说为什么

draveness commented 4 years ago

@draveness

能不能讲解一下 go sort包啊 为什么超过maxdepth 就要把quicksort变成heapsort啊 maxdepth 为什么样那样算 为什么是 2* ceil(log(n+1)) 看不懂也,网上也找不到 😢

直接看源代码呗... https://github.com/golang/go/blob/master/src/sort/sort.go#L183

看不懂源码只写了是什么和 怎么做 但没说为什么

可以看一下我上面的回复,不知道能解答你的问题么

Cherrison commented 4 years ago

在看那个论文。

Draven notifications@github.com 于 2020年4月14日周二 21:49写道:

@draveness https://github.com/draveness

能不能讲解一下 go sort包啊 为什么超过maxdepth 就要把quicksort变成heapsort啊 maxdepth 为什么样那样算 为什么是 2* ceil(log(n+1)) 看不懂也,网上也找不到 😢

直接看源代码呗... https://github.com/golang/go/blob/master/src/sort/sort.go#L183

看不懂源码只写了是什么和 怎么做 但没说为什么

可以看一下我上面的回复,不知道能解答你的问题么

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/draveness/blog-comments/issues/196#issuecomment-613454091, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKHAFSHL4SJYQVASNNKWKT3RMRSWNANCNFSM4MA5UEBA .

cxt90730 commented 4 years ago

使用plugin时,plugin经常要和主程序同时(更确切的说是同一环境下)build才行。如果主程序有改动或者build的路径更换,plugin不同时更新的话,加载plugin时就会报某个package版本错误的问题,导致加载失败。略蛋疼。现在都是靠docker去保持统一环境。

draveness commented 4 years ago

使用plugin时,plugin经常要和主程序同时(更确切的说是同一环境下)build才行。如果主程序有改动或者build的路径更换,plugin不同时更新的话,加载plugin时就会报某个package版本错误的问题,导致加载失败。略蛋疼。现在都是靠docker去保持统一环境。

是的,这个东西非常麻烦...我一般做法是把 API 搞成单独的项目或者直接用 CGO

pikeszfish commented 4 years ago

主程序可以在编译后动态加载共享库实现热插拔的插件系统 目前是不是还不能 https://github.com/golang/go/issues/20461 https://golang.org/pkg/plugin/

@draveness

使用plugin时,plugin经常要和主程序同时(更确切的说是同一环境下)build才行。如果主程序有改动或者build的路径更换,plugin不同时更新的话,加载plugin时就会报某个package版本错误的问题,导致加载失败。略蛋疼。现在都是靠docker去保持统一环境。

是的,这个东西非常麻烦...我一般做法是把 API 搞成单独的项目或者直接用 CGO

有一种方式:在 plugin 里将依赖 vendor 进来,vendor 目录名称修改为随机串,在编译 plugin 前,将 plugin 代码的依赖路径全修改为 source.com/xxx/plugin/{random_str}/source.com/aaa/bbb。和主程序区分开

draveness commented 4 years ago

主程序可以在编译后动态加载共享库实现热插拔的插件系统 目前是不是还不能拔

拔就是换一个 so 呗...

有一种方式:在 plugin 里将依赖 vendor 进来,vendor 目录名称修改为随机串,在编译 plugin 前,将 plugin 代码的依赖路径全修改为 source.com/xxx/plugin/{random_str}/source.com/aaa/bbb。和主程序区分开

这个方法还不如单独搞一个仓库,这也太 hack 了...

Desmond56 commented 4 years ago

编译后的so的大小可以压缩么,感觉so都太大了

draveness commented 4 years ago

编译后的so的大小可以压缩么,感觉so都太大了

可以压缩,但是用的时候还是要解压

DustOak commented 3 years ago

拿cgo做过dll 然后发现没法给vs编译用 因为cgo生成的dll/so是gcc写的 因为我不熟gcc->vs那一套所以是leader做的 但是gcc转到vs可用的dll过程异常痛苦

mudream4869 commented 2 years ago

2022 了,看起來 golang 仍沒有計畫完善 plugin 阿...