Skip to content

Simplify and speed up DNS name decompression

Tony Finch requested to merge 3655-decompress-faster-v9_16 into v9_16

The aim is to do less work per byte:

  • Check the bounds for each label, instead of checking the bounds for each character.

  • Instead of copying one character at a time from the wire to the name, copy entire runs of sequential labels using memmove() to make the most of its fast loop.

  • To remember where the name ends, we only need to set the end marker when we see a compression pointer or when we reach the root label. There is no need to check if we jumped back and conditionally update the counter for every character.

  • To parse a compression pointer, we no longer take a diversion around the outer loop in between reading the upper byte of the pointer and the lower byte.

  • The parser state machine is now implicit in the instruction pointer, instead of being an explicit variable. Similarly, when we reach the root label we break directly out of the loop instead of setting a second state machine variable.

  • DNS_NAME_DOWNCASE is never used with dns_name_fromwire() so that option is no longer supported.

I have removed this comment which dated from January 1999 when dns_name_fromwire() was first introduced:

/* * Note: The following code is not optimized for speed, but * rather for correctness. Speed will be addressed in the future. */

No functional change, apart from removing support for the unused DNS_NAME_DOWNCASE option. The new code is about 2x faster than the old code: best case 11x faster, worst case 1.4x faster.

Closes #3655 (closed)

Merge request reports