Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

optimisation when you play with atoms #6

Open
silviucpp opened this issue Oct 29, 2016 · 0 comments
Open

optimisation when you play with atoms #6

silviucpp opened this issue Oct 29, 2016 · 0 comments

Comments

@silviucpp
Copy link

Hello,

Looking through the code I see when you play with atoms (at least for bool true and false) you are creating all the time them or extract the string in order to compare.

What you can do instead is:

You can declare a global struct with all atoms:

struct atoms
{
    ERL_NIF_TERM atomTrue;
    ERL_NIF_TERM atomFalse;
    //whatever else ...
};

In on_nif_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) you can initialize them:

ATOMS.atomTrue = make(env, str_atom("true"));
ATOMS.atomFalse = make(env, str_atom("false"));

I will also changed the following functions like


inline TERM make(ErlNifEnv *env, const str_atom &var)
{
    if(enif_make_existing_atom(env, var.c_str(), &ret, ERL_NIF_LATIN1))
        return TERM(ret);

    return TERM(enif_make_atom(env, var.c_str()));
}

// bool
inline int get(ErlNifEnv *env, ERL_NIF_TERM term, bool &var)
{
      if(enif_is_identical(term, ATOMS.atomTrue)
      {
           var = true;
           return 1;
      }

      if(enif_is_identical(term, ATOMS.atomFalse)
      {
            var = false;
           return 1;
      }

     return 0; // some other atom, return error
}
inline TERM make(ErlNifEnv *env, const bool var)
{
     return TERM(var ? ATOMS.atomTrue: ATOMS.atomFalse);
}

Kind regards,
Silviu

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant