[OpenSIPS-Users] best way to do "foreach"

Kingsley Tart kingsley at dns99.co.uk
Wed May 12 11:12:04 EST 2021


On Wed, 2021-05-12 at 13:46 +0300, Liviu Chircu wrote:
> On 12.05.2021 13:32, Kingsley Tart wrote:
> >          $var(valindex) = $var(valindex) + 1;
> 
> Also worth mentioning that since 3.1, this statement can be re-
> written as:
> 
> 	$var(valindex) += 1;

Oh excellent.

> Additionally, during your iteration, you may also take advantage of
> the fact that "$(var(valCSV){csv.param,$var(valindex)})" returns NULL
> on "index out of bounds".  Many other "indexable" transformations
> behave like this as well.

Handy to know, thanks :)

> There is actually a "foreach" statement available, applicable to any 
> variable that can be indexed [1], so maybe you could write some cleaner 
> code using that, depending on your available inputs.
> [1]: https://www.opensips.org/Documentation/Script-Statements-3-2#toc4

Thanks. The reason for actually counting the loop for the AVP version
was that it's possible (depending on where this function was called
from) that there might already be an AVP with the same name.
Specifically counting through the loop ensures I only remove the values
I put on, thus leaving it in its original state when the function
returns.

> As far as performance is concerned, the differences are negligible.  As 
> long as you follow the rule of "$var is always faster than $avp", you 
> should be writing nearly optimal script code.

I do that where I can.

> Ultimately, the bottlenecks will forever be DNS queries, SQL queries, 
> HTTP queries, TCP I/O, SIP retransmission timers, etc., with scripting 
> technique being near the bottom of the optimization priority list...

To help with that, I'm using cachedb_local with cache_store() and
cache_fetch(), populating some things in a timer route, and also using
dns_cache, so hopefully avoiding as many of those bottlenecks as is
reasonably possible to do.

Thanks for your help.

-- 
Cheers,
Kingsley.




More information about the Users mailing list