From d7a6eb1d0083c1d788e8febedaea67464361d271 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 16 Oct 2025 15:13:14 +0200 Subject: [PATCH] global-functions: $NetMask6: implement simple caching The calculation is quite complex for something that needs to be done frequently, for example by `fw-addr-lists`. The number of possible netmasks is limited, so let's cache the results that were calculated already. --- global-functions.rsc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/global-functions.rsc b/global-functions.rsc index ffa5277..5ede654 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -1008,13 +1008,25 @@ :global MAX; :global MIN; + :global NetMask6Cache; + + :if ([ :typeof ($NetMask6Cache->$CIDR) ] = "ip6") do={ + :return ($NetMask6Cache->$CIDR); + } + + :if ([ :typeof $NetMask6Cache ] = "nothing") do={ + :set NetMask6Cache ({}); + } + :local Mask ""; :for I from=0 to=7 do={ :set Mask ($Mask . \ [ :convert from=num to=hex (0xffff - (0xffff >> [ :tonum [ $MIN [ $MAX ($CIDR - (16 * $I)) 0 ] 16 ] ])) ] . \ [ $IfThenElse ($I < 7) ":" ]); } - :return [ :toip6 $Mask ]; + :set Mask [ :toip6 $Mask ]; + :set ($NetMask6Cache->$CIDR) $Mask; + :return $Mask; } # prepare NotificationFunctions array