backport optimizations from gdnsd a1c4e5f8

Change-Id: I2791f0d74ea4d5fbf9f42a089baa851dd6c291f6