Remix.run Logo
drzaiusx11 6 days ago

Many times with JIT you don't even need to drop to a more performant language. You can actually get some speed gains by moving C library code back to Ruby so you don't have to pay the translation costs

dismalaf 6 days ago | parent [-]

True. Ruby JIT has increased performance for some tasks you'd drop down to C for back in the day...

All the gains Ruby has made in the last few years are pretty incredible. I never picked it because of performance reasons but it's pretty nice that it's getting faster.

Either way though, Ruby FFI is super super easy. Just for fun, because of this thread, I spent a whole 2 minutes (lol) linking an Odin (my new low-level hobby language) library to Ruby. Literally all it took. Credit to Odin for having such easy export and Ruby for having such easy FFI.

Alifatisk 5 days ago | parent [-]

> I spent a whole 2 minutes (lol) linking an Odin (my new low-level hobby language) library to Ruby.

Would you mind creating a gist of your steps?

dismalaf 5 days ago | parent [-]

Odin code:

    package main

    import "core:fmt"

    @export
    sayhi :: proc() {
      fmt.println("Hello World")
    }
Key is the "@export" statement. Then build Linux dynamic library by doing "odin build main.odin -file -build-mode:shared". -file flag is since I'm just building a single Odin file, didn't set up a project structure or anything. -build-mode:shared is pretty obvious, there's separate build mode flags for Windows or Mac platforms (called "dll" and "dynamic" I think).

Ruby code:

    require 'ffi'
    module MyLib
      extend FFI::Library
      ffi_lib "./main.so"
      attach_function :sayhi, [], :void
    end

    MyLib.sayhi
That's literally all there is to it. Odin lib is a single file. Ruby code is a single file. Don't even need a project setup since Odin can compile single files into programs or libraries and Ruby can of course link to a shared lib that's anywhere.
Alifatisk 4 days ago | parent [-]

Thank you so much, that was way easier than what I expected, amazing!