TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Tiny JITs for a Faster FFI

316 点作者 hahahacorn3 个月前

14 条评论

cchianel3 个月前
I had to deal with a lot of FFI to enable a Java Constraint Solver (Timefold) to call functions defined in CPython. In my experience, most of the performance problems from FFI come from using proxies to communicate between the host and foreign language.<p>A direct FFI call using JNI or the new foreign interface is fast, and has roughly the same speed as calling a Java method directly. Alas, the CPython and Java garbage collectors do not play nice, and require black magic in order to keep them in sync.<p>On the other hand, using proxies (such as in JPype or GraalPy) cause a significant performance overhead, since the parameters and return values need to be converted, and might cause additional FFI calls (in the other direction). The fun thing is if you pass a CPython object to Java, Java has a proxy to the CPython object. And if you pass that proxy back to CPython, a proxy to that proxy is created instead of unwrapping it. The result: JPype proxies are 1402% slower than calling CPython directly using FFI, and GraalPy proxies are 453% slower than calling CPython directly using FFI.<p>What I ultimately end up doing is translating CPython bytecode into Java bytecode, and generating Java data structures corresponding to the CPython classes used. As a result, I got a 100x speedup compared to using proxies. (Side note: if you are thinking about translating&#x2F;reading CPython bytecode, don&#x27;t; it is highly unstable, poorly documented, and its VM has several quirks that make it hard to map directly to other bytecodes).<p>For more details, you can see my blog post on the subject: <a href="https:&#x2F;&#x2F;timefold.ai&#x2F;blog&#x2F;java-vs-python-speed" rel="nofollow">https:&#x2F;&#x2F;timefold.ai&#x2F;blog&#x2F;java-vs-python-speed</a>
评论 #43033979 未加载
评论 #43036091 未加载
评论 #43035472 未加载
chris123213 个月前
Between Rails At Scale and byroot&#x27;s blogs, it&#x27;s currently a fantastic time to be interested in in-depth discussions around Ruby internals and performance! And with all the recent improvements in Ruby and Rails, it&#x27;s a great time to be a Rubyist in general!
评论 #43031238 未加载
haberman3 个月前
&gt; Rather than calling out to a 3rd party library, could we just JIT the code required to call the external function?<p>I am pretty sure this is the basis of the LuaJIT FFI: <a href="https:&#x2F;&#x2F;luajit.org&#x2F;ext_ffi.html" rel="nofollow">https:&#x2F;&#x2F;luajit.org&#x2F;ext_ffi.html</a><p>I think LuaJIT&#x27;s FFI is very fast for this reason.
internetter3 个月前
&quot;write as much Ruby as possible, especially because YJIT can optimize Ruby code but not C code&quot;<p>I feel like I&#x27;m not getting something. Isn&#x27;t ruby a pretty slow language? If I was dipping into native I&#x27;d want to do as much in native as possible.
评论 #43030900 未加载
评论 #43030782 未加载
评论 #43030723 未加载
评论 #43030839 未加载
评论 #43030843 未加载
评论 #43031935 未加载
评论 #43030828 未加载
eay_dev3 个月前
I&#x27;ve been using Ruby more than 10 years, and seeing its development in these days is very exciting. I hope
pestatije3 个月前
FFI - Foreign Function Interface, or how to call C from Ruby
评论 #43031410 未加载
aidenn03 个月前
Why does this need to be JIT compiled? If it could be written in C, then it certainly could just be compiled at load time, no?
评论 #43034031 未加载
brigandish3 个月前
It&#x27;s an aside, but<p>&gt; Now, usually I steer clear of FFI, and to be honest the reason is simply that it doesn’t provide the same performance as a native extension.<p>I usually avoid it, or in particular, gems that use it, because compilation can be such a pain. I&#x27;ve found it easier to build it myself and cut out the middleman of Rubygems&#x2F;bundler.
evacchi3 个月前
somewhat related, this library uses the JVMCI (JVM Compiler Interface) to generate arm64&#x2F;amd64 code on the fly to call native libraries without JNI <a href="https:&#x2F;&#x2F;github.com&#x2F;apangin&#x2F;nalim">https:&#x2F;&#x2F;github.com&#x2F;apangin&#x2F;nalim</a>
nialv73 个月前
isn&#x27;t this exactly what libffi does?
评论 #43031969 未加载
评论 #43031109 未加载
评论 #43031707 未加载
评论 #43031331 未加载
poisonta3 个月前
I can sense why it didn’t go to tenderlovemaking.com
评论 #43030953 未加载
IshKebab3 个月前
&gt; Even in those cases, I encourage people to write as much Ruby as possible, especially because YJIT can optimize Ruby code but not C code.<p>But the C code is still going to be waaay faster than the Ruby code even with YJIT. That seems like an odd reason to avoid C. (I think there are other good reasons though.)
评论 #43034484 未加载
cpursley3 个月前
Can anyone still make a case to start a new project in Rails in 2025 when there is Elixir LiveView?<p>I enjoy Ruby but activerecord is a mess and the language is slow and lacks real time functionality.
评论 #43036491 未加载
评论 #43041449 未加载
shortrounddev23 个月前
Does ruby have its equivalent to typescript, with type annotations? The language sounds interesting but I tend not to give dynamically typed languages the time of day
评论 #43030975 未加载
评论 #43030735 未加载
评论 #43038202 未加载
评论 #43033067 未加载
评论 #43034524 未加载
评论 #43031262 未加载
评论 #43031829 未加载