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.

When FFI function calls beat native C

129 pointsby goranmoominabout 3 years ago

11 comments

gavinrayabout 3 years ago
Can someone ELI5 what exactly happens when the external method call is &quot;JIT&quot;ed in a language like LuaJIT -- what does that mean?<p>I understand calling dlopen() and dlsym()<p>And I understand this idea of a PLT and it&#x27;s indirection<p>But this idea of something external to the JIT&#x27;ed program being JIT&#x27;ed I do not understand.<p>Does it mean it inlined the instructions of the external function into the JIT&#x27;ed code?
评论 #30908761 未加载
评论 #30908678 未加载
timsneathabout 3 years ago
Note that this article is four years old. I was trying to figure out why the language I look after (Dart) looked so bad, but then I realized that the benchmark is using a completely obsolete (now removed) approach to FFI and running all the code in interpreted mode, rather than compiling it.
评论 #30914573 未加载
MuffinFlavoredabout 3 years ago
Is it possible to use Lua&#x2F;LuaJIT in the opposite FFI direction (aka instead of invoking FFI functions, providing them)<p>aka, upon DLL invocation (on Win32 they call it DllMain I guess, I&#x27;ve seen it call ctor&#x2F;dtor on *nix), spawn the runtime, and expose FFI functions?<p><a href="http:&#x2F;&#x2F;www.drewtech.com&#x2F;support&#x2F;passthru.html" rel="nofollow">http:&#x2F;&#x2F;www.drewtech.com&#x2F;support&#x2F;passthru.html</a><p>This spec is really big on FFI exposing functions. I always find it an edge case when trying to play with certain technologies (like the one in the article).
评论 #30910593 未加载
评论 #30954726 未加载
评论 #30909574 未加载
twicabout 3 years ago
Essentially, a JIT does static linking, just at runtime.
评论 #30908718 未加载
symmetricsaurusabout 3 years ago
Previously: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17171252" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17171252</a><p>(Linked from the article)
emergedabout 3 years ago
It’s been a while since I’ve worked with PE format but isn’t this the purpose of a fixup table? The executable loader can patch the machine language to make direct calls? Step further you have LTCG which may copy and paste the actual code and recompile it inline.<p>So this is a Linux or *nix specific quirk, rather than a C quirk. Apologies if my memory isn’t accurate.
评论 #30911234 未加载
评论 #30909506 未加载
eesmithabout 3 years ago
For similar reasons, PyPy&#x27;s Python implementation can outperform C.<p><a href="https:&#x2F;&#x2F;www.pypy.org&#x2F;posts&#x2F;2011&#x2F;02&#x2F;pypy-faster-than-c-on-carefully-crafted-5614784244310486765.html" rel="nofollow">https:&#x2F;&#x2F;www.pypy.org&#x2F;posts&#x2F;2011&#x2F;02&#x2F;pypy-faster-than-c-on-car...</a> - JIT&#x27;ing across compilation units<p><a href="https:&#x2F;&#x2F;www.pypy.org&#x2F;posts&#x2F;2011&#x2F;08&#x2F;pypy-is-faster-than-c-again-string-6756589731691762127.html" rel="nofollow">https:&#x2F;&#x2F;www.pypy.org&#x2F;posts&#x2F;2011&#x2F;08&#x2F;pypy-is-faster-than-c-aga...</a> - JIT&#x27;ing % interpolation.<p>(Wow, those are 11 years old. I remember when PyPy was a new project.)
CyberRabbiabout 3 years ago
Are direct calls really all that much faster than indirect calls on current x86 archs? I was under the impression that it’s more or less the same on the current generation of CPUs. Those CPUs do a decent job of branch predicting indirect calls, especially in a micro benchmark loop. The BTB generally works well.
评论 #30909009 未加载
vientabout 3 years ago
&gt; If the JIT code needed to call two different dynamic functions separated by more than 2GB, then it’s not possible for both to be direct.<p>Well, you can do<p><pre><code> MOV rax, 0x1122334455667788 PUSH rax RET</code></pre> in this case. Still direct, just a bit slower. Wonder if modern CPUs speculate past this construction.
评论 #30910971 未加载
marcodiegoabout 3 years ago
Considering this is more a ELF&#x2F;Linux thing than C, it means there is space for performance improvement of ffi&#x2F;so-heavy processes on Linux. I wonder why nobody cared to improve it.
评论 #30911536 未加载
评论 #30911660 未加载
评论 #30916674 未加载
forrestthewoodsabout 3 years ago
Does anyone know what this benchmark would look like on Windows?
评论 #30909473 未加载