TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Tiny JITs for a Faster FFI

316 pointsby hahahacorn3 months ago

14 comments

cchianel3 months ago
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 months ago
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 months ago
&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 months ago
&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 months ago
I&#x27;ve been using Ruby more than 10 years, and seeing its development in these days is very exciting. I hope
pestatije3 months ago
FFI - Foreign Function Interface, or how to call C from Ruby
评论 #43031410 未加载
aidenn03 months ago
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 months ago
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 months ago
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 months ago
isn&#x27;t this exactly what libffi does?
评论 #43031969 未加载
评论 #43031109 未加载
评论 #43031707 未加载
评论 #43031331 未加载
poisonta3 months ago
I can sense why it didn’t go to tenderlovemaking.com
评论 #43030953 未加载
IshKebab3 months ago
&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 months ago
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 months ago
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 未加载