3

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? )
NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352

2 Answers2

2

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

Julien Portalier
  • 2,959
  • 1
  • 20
  • 22
  • that's def an interesting project. Anyway to work with ActiveRecord objects in Crystal using this? – NullVoxPopuli Sep 13 '16 at 17:02
  • 1
    @NullVoxPopuli While native extensions are possible/have been done, there's no way to work with ActiveRecord objects efficiently using Crystal. There's only two ways to interact with those complex objects: 1) convert to JSON and send to Crystal. This is going to be worse than just using ActiveModelSerializers. The other way 2) would be to pass the Ruby object, and then send methods from Crystal to that object. This would be less efficient than just calling the methods in Ruby. I'd be happy to discuss this more in depth elsewhere if you would like (I'm the author of `crystalized_ruby`) – Paul Hoffer Oct 23 '16 at 01:54
  • @PaulHoffer do you think it'd be worth it to use a crystal ORM, and send objects to ruby for business logic? maybe having DB and Serialization stuff in crystal? though, I guess at that point, most of my project would be in crystal already. ha – NullVoxPopuli Nov 21 '17 at 15:34
  • @NullVoxPopuli exactly, at that point you're running a Crystal app. It could be useful to gradually transform performance-bound APIs to Crystal, for example. There has been so much progress since last October too. Amber Framework is one to check out – Paul Hoffer Nov 22 '17 at 06:15
1

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.

asterite
  • 2,906
  • 1
  • 14
  • 14
  • 4
    People have been writing MRI and YARV extensions in Haskell and OCaml, both of which also have GCs. Extensions for JRuby are written almost exclusively in Java, which also has GC. Extensions for IronRuby are written almost exclusively in C♯, again, with GC. Extensions for Topaz are written in RPython, for Opal in ECMAScript, for MagLev in Smalltalk, and so on. Not having GC is not a requirement for writing Ruby extensions. – Jörg W Mittag Sep 12 '16 at 20:37
  • do you have any good rust examples? I've seen this project: https://github.com/rustbridge/helix but it looks like it still has a ways to go. – NullVoxPopuli Sep 12 '16 at 23:59
  • Potential use cases I thought about for using Crystal for Ruby extensions: computationally expensive code, as well as parsing/generating binary protocols is damn easier. – Julien Portalier Sep 13 '16 at 16:13
  • @JulienPortalier that's what I want to do. JSON API is really slow in ActiveModelSerializers atm, and I'm wanting to speed it up. – NullVoxPopuli Sep 13 '16 at 17:05
  • 1
    There are better gems for handling JSON, like oj, and the json gem is already written in C. I don't think you'll get better results with Crystal. – Julien Portalier Sep 13 '16 at 18:22
  • i'm more loooking into how the JSON API structure is created, if you get a chance, and checkout the AMS repo and run bin/bench, you'll see what i'm talking about. lots of object allocation and such. – NullVoxPopuli Sep 13 '16 at 19:27