4 ms·
I've asked ChatGPT to summarize (granted my prompt might not be ideal), but some points to note, here just first in details others in the link at the bottom:
by malkia 1y ago
I've asked ChatGPT to summarize (granted my prompt might not be ideal), but some points to note, here just first in details others in the link at the bottom:
Calling C from Go (or vice versa) often requires switching from Go's lightweight goroutine model to a full OS thread model because:
- Go's scheduler manages goroutines on M:N threads, but C doesn't cooperate with Go's scheduler.
- If C code blocks (e.g., on I/O or mutex), Go must assume the worst and parks the thread, spawning another to keep Go alive.
* Cost: This means entering/exiting cgo is significantly more expensive than a normal Go call. There’s a syscall-like overhead.
... This was only the first issue, but then it follows with "Go runtime can't see inside C to know is it allocating, blocking, spinning, etc.", then "Stack switching", "Thread Affinity and TLS", "Debug/Profiling support overhead", "Memory Ownership and GC barriers"
All here - https://chatgpt.com/share/688172c3-9fa4-800a-9b8f-e1252b57d08d https://chatgpt.com/share/688172c3-9fa4-800a-9b8f-e1252b57d0...
- johnisgood 1y agoJust to roll with your way: https://chatgpt.com/share/688177c9-ebc0-8011-88cc-9514d8e1674b https://chatgpt.com/share/688177c9-ebc0-8011-88cc-9514d8e167... Please do not take the numbers below at face value. I still expect an actual reply to my initial comment. Per-call overhead: C (baseline) - ~30 ns Rust (unsafe) - ~30 ns C# (P/Invoke) - ~30-50 ns LuaJIT - ~30-50 ns Go (cgo) - ~40-60 ns Java (22, FFM) - ~40-70 ns Java (JNI) - ~300-1000 ns Perl (XS) - ~500-1000 ns Python (ctypes) - ~10,000-30,000 ns Common Lisp (SBCL) - ~500-1500 ns Seems like Go is still fast enough as opposed to other programming languages with GC, so I am not sure it is fair to Go.
- malkia 1y agooh wow I got downvoted a lot - I guess I'm bad at prompting :)
- johnisgood 1y agoThat is not it. Everyone who copy pastes output from an LLM gets downvoted (and likely flagged). Even though I simply went with your method, I got down-voted too, when down-voting the parent comment (yours) would have sufficed. Oh well.
- malkia 1y agoNoted! Well maybe it makes sense! Thanks for the info!!!
- johnisgood 1y agoI even got down-voted for telling you the truth. Lmao. "ants_everywhere" is right.
- ants_everywhere 1y agoDon't take it too personally. The anti-LLM activists that patrol HN will often downvote comments just for stating facts that disagree with their hatred of LLMs.
- Sesse__ 1y agoYou are being downvoted because pasting AI output with no attempt at fact-checking is not bringing any real value to the discussion.
- Cyph0n 1y ago> Rust (unsafe) As if there is an alternative :) More seriously, it’s “unsafe” from the perspective of the library calling into C, but usually “safe” for any layer above.
- johnisgood 1y agoHey, since I am in a thread where we are sharing what ChatGPT spits out, I just copy pasted it from there, too. :) For what it is worth, I asked about LuaJIT after I have shared the link, and the numbers are now different for some languages, albeit not by much. Go (cgo) became ~50-100 ns. That said, I still believe it is unfair to single out Go when it does way better than some other GC languages.
- throwaway7783 1y agoJava now has FFM, that is far better and simpler than JNI, FWIW. and chatgpt says Language/API | Call Overhead (no-op C) | Notes Go (cgo) | ~40–60 ns | Stack switch + thread pinning Java FFM | ~50 ns (downcall) | Similar to JNI, can be ~30 ns with isTrivial() Java FFM (leaf) | ~30–40 ns | Optimized (isTrivial=true) JNI | ~50–60 ns | Slightly slower than FFM Rust (unsafe) | ~5–20 ns | Near-zero overhead C# (P/Invoke) | ~20–50 ns | Depends on marshaling Python (cffi) | 1000–10000 ns | Orders of magnitude slower |
- johnisgood 1y agoThanks, I added it to the list. Keep in mind that the numbers may be off (both yours and mine), so I would not take them at face value. It is interesting how in yours JNI is still pretty good. Also Rust is "~5–20 ns" in yours, so I assume "0" is the baseline.
- throwaway7783 1y agoThis is chatgpt. Not my own benchmark. So it is probably hallucinating
- billywhizz 1y agoi can't see how these numbers can be anywhere near correct (nor the ones above). in JavaScript on an old Core i5 the overhead of a simple ffi call is on the order of 5 nanoseconds. on a recent x64/arm64 cpu it's more like 2 nanoseconds. you can verify this easily with Deno ffi which is pretty much optimal for JS runtimes. also, from everything i have seen and read, luajit should be even lower overhead than this. you really shouldn't be asking chatgpt questions like this imo. these are facts, that need to be proven, not just vibes.
- throwaway7783 1y agoI agree. was just following the parents pattern, to make it work for me :)