Skip to content

split names without escapes at the dots in dns.name.from_text() - #1427

Merged
rthalley merged 2 commits into
rthalley:mainfrom
g4lb:name-from-text-split
Nov 21, 2026
Merged

rthalley merged 2 commits into
rthalley:mainfrom
g4lb:name-from-text-split

Conversation

@g4lb

@g4lb g4lb commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

dns.name.from_text() read the text one byte at a time and made a 1-byte bytes object for each byte. When the text has no backslash, it now splits the text at the dots. When the text has escapes, the loop now uses the byte values directly. The output does not change.

Benchmark (CPython 3.15) Before After Change
200 names such as www1.example.com. 565 µs 279 µs -51%
200 names with 6 to 12 labels 2390 µs 616 µs -74%
100 names with escapes 351 µs 248 µs -29%
8 rdata, from text and to text 110 µs 100 µs -9%

I measured no slowdown on names with escapes, bytearray input, @, ., names that are not valid, and zone files. I used interleaved rounds and a Mann-Whitney test (Apple M5, 20 rounds on CPython 3.15, 12 rounds on CPython 3.11). On PyPy 3.11, I did 3 paired runs, and all name inputs were faster. Import time did not change (20 rounds). I did not measure other architectures.

Checks:

  • NO_INTERNET=1 pytest passes on CPython 3.11, CPython 3.15, and PyPy 3.11.
  • ruff check dns passes. black --check passes on the changed files. pyright dns and ty check dns give the same results as on main.
  • Old and new outputs matched on 1,000,000 random inputs. The inputs include bytes, bytearray, and memoryview text.
  • The new test makes sure that bytearray and memoryview input still work.

from_text() read the text one byte at a time, also when the name has
no escapes, and made a one-octet bytes object for each byte.  If there
is no backslash in the text, split it at the dots instead.  For a name
with escapes, work on the octet values and append them to the label.
@rthalley

Copy link
Copy Markdown
Owner

A nice speed improvement that is a clean change, thanks!

@rthalley
rthalley merged commit 2ff0dbc into rthalley:main Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants