Asimov

Asimov

Hi all,

This is a general programming question, not specific to Elixir. I do my fare share of scripting in LUA. Specifically I code IVR on freeswitch / Asterisk. So a common scenario I have is that there is a bunch of configurations parameters that I have to load in memory every time I run a script (e.g. every time a call hits the IVR). I am coding a project now where the number of configuration parameters is quite large (about 200) - please do not focus on the why of this, that is not the aim of the question.

So, what I usually do is to have a config.lua somehere where I define all these parameters as global variables:

__parameter001__ = value01
__parameter001__ = value02
__parameter_N__ = value_N

Not elegant but it does the job. So, I was thinking, would it be more efficient to have a “config getcher” fuction? Something like this :

function  get_parameter(parameter)
   if(parameter == '__parameter001__')then return value01; 
     elseif(parameter == '__parameter002__')then return value02;
     elseif(parameter == '__parameter_N__')then return value_N; 
     else return 'no_value_set';
    end;
end

In my code I would simply call this function everytime I need a parameter value:

my_parameter = get_parameter('__parameter001__');

So, instead of loading a lot of global variables I would be calling a function every time to get the value of a parameter.

My question is: is this something sensible to do? Should I just simply keep loading the parameters in memory?

I know this is not an Elixir question and my example is LUA based but I believe this is language independent discussion. Also, this forum is much more professional than any LUA forum out there (that I know of), so I’d love to know your opinion.

Thank you!

Showing Posts 1 to 4

dimitarvp

dimitarvp

I don’t see why not. Can’t comment on Lua in particular but in Elixir I’d just load these into a Map and then store it and use it like so:

defmodule MyConfig do
  @config_key :my_config

  def load(file_path) do
    # load your configuration parameters into an Elixir map.
    # ...your code here...

    # then, assuming your map with the loaded config is called `cfg`:
    :persistent_term.put(@config_key, cfg)
  end

  def get(param), do: :persistent_term.get(@config_key) |> Map.get(param)
end

That way, you are putting that 200+ keyed map in an in-memory cache that does not get copied when accessed and you have a hyper-fast config map.

(This approach assumes that you’d change that config map rather rarely, by the way.)

Asimov

Asimov OP

Thank you @dimitarvp. No need to comment on LUA, it was just to set the example. So, I think what you do with the map is similar to option 1: loading all variables into memory but in a more efficient way (the map) which in turn is accessed by a function. I never change the parameters by the way, they are inmutable..

That absolutely helps me to have a better grasp on how to approach this efficiently.

Thank you again for taking the time to reply.

dimitarvp

dimitarvp

I’d advise you to do a quick read on Erlang’s :persistent_term module. It’s absolutely perfect for rarely (or never) changing data.

Glad to help.

Asimov

Asimov OP

I ended up doing just this but on LUA: an object constructor loads a map with config parameters.Thanks @dimitarvp!

— All posts loaded —

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews