donglian1953 2013-12-27 03:49
浏览 41
已采纳

如何在golang中正确使用judy array lib?

In golang, the way calling C library is different from what's used in other mainframe dynamic language like PHP / Python / Java because Golang has a different multitasking mechanism which is not OS thread based, so call c function may result in a context switching or thread switching as I understand. In my project I'm trying to use Judy Array in Golang (as a queue worker) to do some simple but large amount dict-related calculation like "select distinct", so

What's the best practice to involve such c lib (for relatively high density calculation) and minimalise the performance overhead introduced as much as possible?

  • 写回答

2条回答 默认 最新

  • drr7731 2013-12-30 22:10
    关注

    Despite the title, the question here really has two parts: a generic one about golang and C-interfacing for efficiency, and a specific one about performant use of judy arrays.

    This thread seems to summarize the costs: https://groups.google.com/forum/#!topic/golang-nuts/RTtMsgZi88Q , so yeah its expensive compared to straight C, and you should try to minimize the crossover points from Go to C.

    Here's additional, judy array specific advice: I've used judy arrays before in C/C++ code. The library's interface is not intuitive in certain places. And by default it uses a C-macro based API, which makes it tricky to get the interface usage correct because the compiler can't offer as much help as usual.

    What I recommend, therefore, is that you write your tests and benchmarks in C first, so you understand the API and its weird cases. Judy arrays when benchmarked for my application (vs C++ vector of strings) were 3x faster, so it can be worth it. But break the task into three phases. First do what you want to do in C, and make sure it works as expected in your own C code. Then expand the basic C interface to handle batches of what you need done, so as to minimize the number of Go->C switches. Then bind your new C interface from Go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用