kokolegorille

kokolegorille

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.

Marked As Solved

NobbZ

NobbZ

I took only a quick look in that repository and it seemed to me as if that project were actually meant to be used as a tool on the command line and not as a library.

So I’d definitifely use it as a CLI tool.

Perhaps file some issues to make the ouput easier to process and to make the tool able to read from STDIN.

Or even better, ask the project to extract a library which makes you then can wrap in a NIF-module, assuming recent enough releases of OTP it shouln’t be a problem as you can use dirty schedulers. You only need to get the hinting right :wink:

Also Liked

peerreynders

peerreynders

A C node seems to be missing as an option on that list. Ultimately the level and kind of parallelism you need is going to dictate how hairy that solution would be.

Last Post!

peerreynders

peerreynders

A C node seems to be missing as an option on that list. Ultimately the level and kind of parallelism you need is going to dictate how hairy that solution would be.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement