Ruby gem native extension with Crystal?

678 Views Asked by At

I'm just trying to gather some general information, as I'm looking to improve performance of ActiveModelSerializers.

I've also never written a native extension before, but have looked at some C extensions and Rust extensions.

My Questions:

  • Has this been done? (writing a crystal native extension in a ruby gem?)
  • How does interacting with ruby objects / structures work in crystal? (Does there need to be any sort of marshalling or anything for communication between the two languages? )
2

There are 2 best solutions below

6
asterite On

This question has been maybe asked a million times now. You can try to do it, but remember that Crystal has a GC, so it really doesn't make much sense to write native extensions in Crystal. Use C or Rust, which don't have a GC.

4
Julien Portalier On

Since the Ruby API is written in C, it should be possible to create Crystal bindings for it, then write Ruby extensions in Crystal.

Have a look to the crystalyzed_ruby project for example: https://github.com/phoffer/crystalized_ruby