Making dns_erlang Fast: Binary Tricks for the BEAM
11 points by jparise
11 points by jparise
GREAT write up. My favorite gem in the whole thing is this very salient comment near the end:
the BEAM is often smarter than you'd expect, and the only way to know is to measure.
You can substitute “the BEAM” with whatever stack works for you. It seems silly to shout out for the oh so obvious “only way to know is to measure”, but I am. Because again and again and again, I deal with people, often very experienced people, who fail to embrace this.
I would further qualify the statement with “the only way to know now is to measure now”. Underlying engines and executions change. What was expensive or cheap in yesteryear’s variant all too often becomes cheap or expensive in today’s.
The advice to look at the generated BEAM assembly is also golden. I remember writing some performance-sensitive code that dispatched based on function heads, and was wondering whether existing captures get reused, something to the effect of:
look_at(<<A:16, B:16, C:32, constant:32 >>) -> ...
look_at(<<A:16, B:16, C:32, other_constant:32>>) --> ...
and compiling down to assembly demonstrated that the erlang compiler indeed loaded the must-have matches into registers before trying the differing right-side matches in order.
I think I have a draft blog post kicking around somewhere but it's been like 10 years now, most of those 10 years spent outside the context of performance-sensitive Erlang, sadly.
I found the intro for the post in my drafts folder, if anyone is interested: https://paste.sr.ht/blob/15719cd5ea56414c9fd5c7e98f6aa02824379688
This is great stuff!
One thing caught my eye in the discussion of encoding compressed DNS names:
a name couldn't be looked up at all until it had been split, validated and lowercased
A DNS server can try to be case-preserving (like BIND) or it can canonicalize names to lower case (like NSD). The latter allows you to eliminate a lot of “peanut butter” slowdowns: no individual case-insensitive comparison is very expensive, but there are lots of them.
I’m surprised that a name needs to be validated when writing it to a message: surely all names inside the server should be valid by construction. If “valid” also requires lower case then you can skip that part too.