Diff

util/ip.lua @ 8436:ab9ddfb03d4d

util.ip: Cache return values of all methods in one place
author Kim Alvefur <zash@zash.se>
date Fri, 01 Dec 2017 02:25:25 +0100
parent 8435:47195f035d2f
child 8437:021129f7b0a3
line wrap: on
line diff
--- a/util/ip.lua	Fri Dec 01 02:23:57 2017 +0100
+++ b/util/ip.lua	Fri Dec 01 02:25:25 2017 +0100
@@ -12,7 +12,11 @@
 
 local ip_mt = {
 	__index = function (ip, key)
-		return ip_methods[key](ip);
+		local method = ip_methods[key];
+		if not method then return nil; end
+		local ret = method(ip);
+		ip[key] = ret;
+		return ret;
 	end,
 	__tostring = function (ip) return ip.addr; end,
 	__eq = function (ipA, ipB) return ipA.addr == ipB.addr; end