Following up on Making `dns_erlang` fast: binary tricks for the BEAM , there’s our second post in our series on making erldns, an authoritative DNS server, faster: it’s techniques for hot paths that work in any Erlang or Elixir codebase, I really hope you’ll learn a trick or two.
- Find, don’t count.
ets:select_count/2 =/= 0is the ETS version oflength(L) =/= 0when matching[_ | _]would do. Stopping at the first match cut our name servers’ CPU by a third. - One
ets:next/2instead of a match spec got many kinds of answers 1.8x faster. On an ordered_set, one step from a key that sorts just before the name tells you both whether the name exists and whether anything is below it. - Give short-lived processes the heap they need. Spawning a process with a well-chosen
min_heap_sizemade a query take less than half the time as it ran to completion without a single GC cycle.
In production, p90 dropped by about 25%, and by about 33% if the query requests DNSSEC.
Questions and feedback welcome!