ref: c7e6d109bcd7e0d0b33c5e134c83e3efe9154ef9
parent: 28cea7b851f71fe07e4491dc8ec6eeee2e1c8011
author: Ori Bernstein <[email protected]>
date: Thu Nov 30 19:29:12 EST 2017
Lazily initialize the resolver. It turns out to make a decent difference to code size.
--- a/lib/std/resolve+posixy.myr
+++ b/lib/std/resolve+posixy.myr
@@ -66,18 +66,12 @@
const Resolvfile = "/etc/resolv.conf"
const Timeout = 2_000
+var initdone : bool = false
var hostmap : htab(byte[:], hostinfo)#
var dnscache : htab(byte[:], hostinfo)#
var search : byte[:][:]
var nameservers : netaddr[:]
-const __init__ = {
- hostmap = mkht()
- dnscache = mkht()
- loadhosts()
- loadresolv()
-}
-
const resolve = {host
/*TODO: v4 and v6 */
-> resolverec(host, `DnsA)
@@ -88,6 +82,17 @@
}
const resolverec = {host, t
+ if !initdone /* avoid extra contention */
+ lock(netlck)
+ if !initdone
+ hostmap = mkht()
+ dnscache = mkht()
+ loadhosts()
+ loadresolv()
+ ;;
+ unlock(netlck)
+ ;;
+
match hostfind(host)
| `Some hinf:
-> `Ok sldup([hinf][:])