Choose the right way to integrate c++ code

Hello everyone,

I would like to integrate some c++ code to Elixir. The library is GitHub - online-go/score-estimator: The score estimator and dead stone removal suggester used on online-go.com. · GitHub.

This is a c++ library with only one functionality, to estimate the score of a go game.

The problem is this is quite expensive (about 500ms).

I learned nif should be short timed functions, otherwise performance will decrease. This is not really the use-case for a port because it is just a command line running.

I have done a wrapper with System, like this

defmodule GoEstimator do
  @moduledoc false
  
  @cmd "#{System.cwd}/priv/estimator"
  
  def estimate(filename) do
    case System.cmd @cmd, [filename] do
      {result, 0} -> 
        {:ok, result}
      {_, code} -> 
        {:error, "could not estimate game, failed with code #{code}"}
    end
  end
end

It is working, but not really useful for communicating with Elixir. I have to create input file, and process stdout.

What would be your advice to integrate c++ simple command line code? With possibly a better data passing between c code and Elixir? Should I?

  • nifs
  • ports/porcelain
  • keep System wrapper, and transform stdin/stdout
  • other not mentionned

Thanks for taking time.