From 2baa4c02a1c27ab8c36e30592d2b8ee46d1bb4cc Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Mon, 29 Nov 2021 20:58:45 +0100 Subject: [PATCH] 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 --- eth/net/utils.nim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/eth/net/utils.nim b/eth/net/utils.nim index 3c13854..1236dd9 100644 --- a/eth/net/utils.nim +++ b/eth/net/utils.nim @@ -16,7 +16,10 @@ type limit*: 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 = let val = ipLimits.ips.getOrDefault(ip, 0)