0

In C++, I have to make some user-defined actions on each cells of a big table. Because of the size of the table, I'd like not to use interpreted instructions but to compile during runtime a function that I will call on each cell.

The user-defined actions are pretty simple :

if ((state1 && state2) || state3) then change_a_value_in_memory

That's why I don't need to use the LLVM or other JIT libraries.

I hesitate to just use mmap and add the code in hex directly.

I'd like to know if it exists better solutions, or, if not, where I can find the basic format of a C++ function code to directly write it in memory.

Thanks, and sorry for my english :/

Jilano
  • 1
  • 2
  • C++ is *usually* neither interpreted nor just-in-time compiled. If yours is, you might want to add what environment you are using. – Jongware Oct 08 '14 at 21:37
  • Maybe you can hook up to some scripting language like Lua – Neil Kirk Oct 08 '14 at 21:38
  • If the conditions are really so simple, are you sure they are a bottleneck? I would expect this kind of operation to be I/O bound. – MooseBoys Oct 08 '14 at 21:45
  • All I need is just pure speed (the table can grow really big), that's why I prefer to use a function call instead of all the complexity of a lua compiler / handler. The code will be as simple as a condition => a result. I'm creating an Cellular Automaton, the function will describe the rules of the automaton, and will be called on each cell of the grid. – Jilano Oct 08 '14 at 21:46
  • 1
    Why not just write a parser for your "language"? – Mats Petersson Oct 08 '14 at 21:59

1 Answers1

2

This is not the most elegant, but it works always: generate a file with the C++ code of your function, call the compiler using system(), load the generated .so file using dlopen, and use the function!

This will take some time (due to the compiler call), but if you keep the function and maintain a database of the functions you already have, then you can save the amount of compilations.

Walter
  • 44,150
  • 20
  • 113
  • 196