I could use a couple of trained eyes for this ruby-ffi-cgo question :)

xuanbao · · 651 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So, </p> <p>as you know (?), I decided to bring some golang into our 7-year-old rails project to break the ice and demonstrate the advantages of go and the easiest way to do this is to attach well-performing goroutines to ruby classes that really represent a bottleneck. In the future, we&#39;ll start to replace portions of our API with legit go. This is to say that I am aware that cgo is not go. :)</p> <p>So my thinking is to create a word-parser &#34;main&#34; go file that imports two other files - just for organizing code - that will own the logic of parsing a word-cloud and a word-tree respectively. </p> <p>Word-parser will provide ruby-attachable methods that delegate to the word-cloud and word-tree files. </p> <p>My first attempt reminds me of ruby and I am pretty sure there ways to improve it. Things work until I try to concatenate strings in the deepest routines. I can guess the combination of pointers, go-strings and c-strings, but I would rather understand! Any suggestion? </p> <p><a href="http://i.imgur.com/bXGD2VE.png">http://i.imgur.com/bXGD2VE.png</a></p> <p>source <a href="https://github.com/etozzato/packaged">https://github.com/etozzato/packaged</a></p> <p>EDIT: *the real-life code actually works. *</p> <hr/>**评论:**<br/><br/>mc_hammerd: <pre><p>maybe an expert can answer you</p> <p>i would suggest trying <code>&#34;foo&#34; + C.GoString(bar) + &#34;baz&#34;</code></p> <p>from: <a href="https://utcc.utoronto.ca/%7Ecks/space/blog/programming/GoCGoStringFunctions" rel="nofollow">https://utcc.utoronto.ca/~cks/space/blog/programming/GoCGoStringFunctions</a></p></pre>mekdigital: <pre><p>that was easy! </p></pre>mekdigital: <pre><p>and this is the &#39;real life repo&#39;</p> <p><a href="https://github.com/etozzato/word-parser-go" rel="nofollow">https://github.com/etozzato/word-parser-go</a></p></pre>robvdl: <pre><p>I wonder if restructuring the code so that it doesn&#39;t need to re-run the regexp.MustCompile() statement each time in the matchSentences() function has any effect on performance, which is called in a loop from what I can see. It could also be that Go maintains a compiled regex cache in which case it might be an unnecessary optimisation, I don&#39;t know really.</p></pre>mekdigital: <pre><p>that is a very good observation. I can&#39;t even think that far :__)</p></pre>mortonpe: <pre><p>Hey Mek - I just sent you a pull request on the library: <a href="https://github.com/etozzato/packaged/pull/1" rel="nofollow">https://github.com/etozzato/packaged/pull/1</a>. </p> <p>Ruby, C and GO have different representations of most data types. Your data flow is Ruby &lt;-&gt; C &lt;-&gt; Go. This means that your ruby library and your go library need to convert to and from the c data types in order to properly talk.</p> <p>In this case, the ruby FFI gem converts ruby strings to C string before passing them to the shared object and converts the result to a ruby string before sending it back. (Magic) </p> <p>On the GO side, your go wrapper function is expecting a go string when in fact you are sending it a C string. You need to accept and return *C.char and use C.CString and C.GoString to convert between go and c string.</p></pre>mekdigital: <pre><p>That is pretty awesome, I didn&#39;t think of &#34;return ftm&#34; :) </p> <p>if you look at the &#39;real life example&#39; I got pretty close to the C.CString/C.GoString handling! <a href="https://github.com/etozzato/word-parser-go" rel="nofollow">https://github.com/etozzato/word-parser-go</a></p> <p>Thanks!</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

651 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传