Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Compiling in Run Time?

julvrjulvr Posts: 3Member
I'm wondering if there are any tools to compile a function during runtime. Basically, my situation is this:

I have a function on a data path, where throughput is extremely important. There are a dozen variables (mostly bools, but a couple of ints) and one data type, which will rarely change (maybe every billion or so itterations). What I'd like to do is to compile the function in run time, treating the variables as constants, and get a pointer to an optimized function. I then use that pointer to run the booleans. The function itself does not access hardware, or have any external dependencies, so it would not have to be linked into anything.

I'm working on a Linux system, but I would be curious to know if this is possible on a Windows platform as well.

John

Comments

  • LundinLundin Posts: 3,711Member
    : I'm wondering if there are any tools to compile a function during
    : runtime. Basically, my situation is this:
    :
    : I have a function on a data path, where throughput is extremely
    : important. There are a dozen variables (mostly bools, but a couple
    : of ints) and one data type, which will rarely change (maybe every
    : billion or so itterations). What I'd like to do is to compile the
    : function in run time, treating the variables as constants, and get a
    : pointer to an optimized function. I then use that pointer to run
    : the booleans. The function itself does not access hardware, or have
    : any external dependencies, so it would not have to be linked into
    : anything.
    :
    : I'm working on a Linux system, but I would be curious to know if
    : this is possible on a Windows platform as well.
    :
    : John
    :


    It isn't possible, or at least it is too complicated to bother with. There is however no reason at all to compile C/C++ code in runtime, no matter the platform.

    [code]
    void func(void* generic_pointer, int behavior)
    {
    switch(behavior)
    {
    case SOMETHING:
    /* Assume that generic_pointer points at a certain struct of variables. */
    break;
    }
    }
    [/code]

  • bilderbikkelbilderbikkel Posts: 754Member
    Lundin is right (of course) and made a C-style suggestion. If you use C++ you might want to use a Strategy or State Design Pattern (for example at http://www.codepedia.com/1/CppStrategy ).

    See ya,
    bilderbikkel
  • julvrjulvr Posts: 3Member
    : Lundin is right (of course) and made a C-style suggestion. If you
    : use C++ you might want to use a Strategy or State Design Pattern
    : (for example at http://www.codepedia.com/1/CppStrategy ).
    :
    : See ya,
    : bilderbikkel




    I actually had a different strategy, which I liked better: I made a template version of my function, where the template would take in several variables. I then initialized a table of function pointers, one for each of the possible variable permutations, and when my device recieved a config file, I would select the best suited function pointer from my table. When the code compiled, the template function would automatically optimize out sections of code and decisions that involved the template parameters. This worked quite well. Unfortunately, with a new variable set, there would be millions of plausible combinations of the particular function. So, basically, I either can only optimize on three or four variables (and not the integer variables, which would have made the most difference), or I can do some sort of compilation at run-time.

    I am still investigating creating a makefile, and compiling the code into a library, then dinamically attaching the library. The only problem is I'm not sure what the overhead of calling a function from a library is.

    This is also somewhat of an academic discussion, as the speed of my table-lookup functions is probably 'good-enough', but having a bit better bandwidth never hurts. It might also be useful for future projects.


    Thanks for the responses.

    John
Sign In or Register to comment.