avoid allocation in `hash(ValidIpAddress)` (#433)

* avoid allocation in `hash(ValidIpAddress)`

While casually browsing the profiler output, to my great surprise I
found that an allocating string conversion function (gasp!) in the hash
function for ip addresses - this PR carefully excises this evil
construct from the codebase.

* bump nim version
This commit is contained in:
Jacek Sieka 2021-11-29 20:58:45 +01:00 committed by GitHub
parent 139c6fa2a8
commit 2baa4c02a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 1 deletions

View File

@ -16,7 +16,10 @@ type
limit*: uint limit*: uint
ips: Table[ValidIpAddress, uint] ips: Table[ValidIpAddress, uint]
func hash(ip: ValidIpAddress): Hash = hash($ip) func hash(ip: ValidIpAddress): Hash =
case ip.family
of IpAddressFamily.IPv6: hash(ip.address_v6)
of IpAddressFamily.IPv4: hash(ip.address_v4)
func inc*(ipLimits: var IpLimits, ip: ValidIpAddress): bool = func inc*(ipLimits: var IpLimits, ip: ValidIpAddress): bool =
let val = ipLimits.ips.getOrDefault(ip, 0) let val = ipLimits.ips.getOrDefault(ip, 0)