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.
This commit is contained in:
Christian Hesse 2025-10-16 15:13:14 +02:00
parent 6ad6f9aa08
commit d7a6eb1d00

View file

@ -1008,13 +1008,25 @@
:global MAX; :global MAX;
:global MIN; :global MIN;
:global NetMask6Cache;
:if ([ :typeof ($NetMask6Cache->$CIDR) ] = "ip6") do={
:return ($NetMask6Cache->$CIDR);
}
:if ([ :typeof $NetMask6Cache ] = "nothing") do={
:set NetMask6Cache ({});
}
:local Mask ""; :local Mask "";
:for I from=0 to=7 do={ :for I from=0 to=7 do={
:set Mask ($Mask . \ :set Mask ($Mask . \
[ :convert from=num to=hex (0xffff - (0xffff >> [ :tonum [ $MIN [ $MAX ($CIDR - (16 * $I)) 0 ] 16 ] ])) ] . \ [ :convert from=num to=hex (0xffff - (0xffff >> [ :tonum [ $MIN [ $MAX ($CIDR - (16 * $I)) 0 ] 16 ] ])) ] . \
[ $IfThenElse ($I < 7) ":" ]); [ $IfThenElse ($I < 7) ":" ]);
} }
:return [ :toip6 $Mask ]; :set Mask [ :toip6 $Mask ];
:set ($NetMask6Cache->$CIDR) $Mask;
:return $Mask;
} }
# prepare NotificationFunctions array # prepare NotificationFunctions array