Skip to content

Avoidable allocations: wordlist-sized maps, lab query counter, CNAME buffer #321

Closed

Description

@TMHSDigital

Summary

Three independent, small fixes that matter with large wordlists or high concurrency: -ct, -sources, -axfr and -permute each build a map as large as the wordlist per target; the lab DNS server keeps a per-name counter only tests read; and every CNAME query allocates a 64 KB read buffer.


-ct, -sources, -axfr and -permute each build a map as large as the wordlist, per target

Where

  • main_ct.go:119-124
  • main_permute.go:73-81
  • internal/passive/passive.go:94-105

Details

candidates.add builds c.have = make(map[string]bool, len(c.entries)+len(names)) and inserts every wordlist entry (main_ct.go:119-124) the first time a source or zone transfer returns any name, only to test a few hundred or thousand source names against it in passive.Merge. permutationCandidates does the same again for the permute pass: skip := make(map[string]bool, len(entries)+len(rel)); for _, e := range entries { skip[e] = true } (main_permute.go:74-77). Both run once per target (main_session.go:635-652, 676-681). This is the map shape #248 removed from wordlist loading (internal/wordlist/reader.go:182-186).

Impact

About 30 to 45 bytes per entry (24-byte slot plus control bytes and slack) and one hashed insert per entry: for a 5M-line list roughly 200 MB of extra peak heap and about half a second, per feature, per target, on top of the roughly 160 MB the entries themselves hold. In a -dL run it is rebuilt for every target. Irrelevant for the bundled 5k list; hurts from about 1M entries.

Steps to reproduce

subenum -ct -permute -w <5M-line list> example.com (or with -seeds), compare peak RSS with the same run without -ct -permute.

Suggested fix

Invert the lookup: put the small side in the map. Normalize the source names (or generated permutations) into a set, then make one pass over entries deleting the ones present; what remains in the set is new. That is O(len(entries)) string hashes with a map of a few thousand entries and no wordlist-sized allocation. passive.Merge is shared with pkg/subenum (passive.go:88-93), so change it there.


Lab DNS server keeps a per-name query counter that only tests read

Where

  • internal/dnsserver/server.go:100-102
  • internal/dnsserver/server.go:423-427
  • internal/dnsserver/server.go:250-254
  • main_lab.go:19

Details

handle does s.mu.Lock(); s.counts[countKey{q.Name, q.Type}]++; s.mu.Unlock() for every query (server.go:425-427). counts is read only by QueriesFor (server.go:250-254), which has no caller outside _test.go files (grep over non-test Go files finds only the definition). The same server backs -simulate-zone (main_lab.go:19) and the MCP lab_scan tool (main_mcp.go:295), where each distinct candidate name and record type adds an entry that is never read or removed.

Impact

About 80 to 90 bytes per distinct (name, type): a 32-byte key slot plus 8-byte value with map slack, plus the lowercased name string. A -simulate-zone run over a 1M-line wordlist with the default A and AAAA types holds 2M entries, about 170 MB; a 5M-line list about 850 MB, in the same process as the scan. Every query also takes one global mutex. MCP lab_scan is capped at 50,000 words per call so it stays under 10 MB. Hurts from about 1M candidates in lab mode.

Steps to reproduce

subenum -simulate-zone examples/labs/lab1-first-scan.zone -w <1M-line list> lab.example; heap grows by roughly 170 MB over the run.

Suggested fix

Make per-name counting opt-in (Options.CountNames, set by internal/dnstest), and leave only the atomic total in the production path.


Each CNAME query allocates a 64 KB read buffer

Where

  • internal/dns/cname.go:111
  • internal/dns/cname.go:163

Details

cnameExchange runs once per candidate when -type includes CNAME (resolver.go:417-418) and does buf := make([]byte, 65535) (cname.go:111) before reading the reply, on UDP as well as TCP, although the query it just built advertises a 1232-byte EDNS(0) payload (opt.SetEDNS0(1232, ...), cname.go:163).

Impact

64 KB zeroed and discarded per CNAME lookup, against roughly 97 bytes for the rest of a candidate's engine cost. Live heap is 64 KB per in-flight CNAME query (64 MB at -t 1000, 320 MB at -t 5000), and at 5,000 CNAME queries per second it is about 320 MB/s of allocation for the garbage collector. Hurts at high -t or high query rates with CNAME enabled.

Steps to reproduce

go test -run '^$' -bench . -benchmem on a small benchmark that calls lookupCNAME against internal/dnstest shows about 64 KB/op; none exists today.

Suggested fix

On a net.PacketConn read into a 1232-byte buffer (or 4096 to be lenient), as Go's own resolver does; on a stream connection allocate exactly the length the two-byte prefix announces. A sync.Pool is an alternative if the full size is wanted.


Acceptance criteria

  • Merging source or permutation names no longer allocates a map sized by the wordlist
  • Per-name query counting in the lab server is opt-in for tests
  • A CNAME lookup over UDP reads into a buffer sized for the advertised EDNS payload

Getting started

  • Relevant files: main_ct.go, main_permute.go, internal/passive/passive.go, internal/dnsserver/server.go
  • How to run tests: make test (or go test ./...). Tests must stay hermetic: use -simulate, -simulate-zone or the local DNS test server, never a live domain.
  • Related docs: docs/DEVELOPER_GUIDE.md, docs/ARCHITECTURE.md

Filed by repo-audit/full-audit at d0c56f0. Severity: low. Effort: S. audit-key: performance|main_ct.go|candidates.add

Activity

  1. added
    good first issue
    performance
    severity: low
    on Nov 15, 2026
  2. TMHSDigital commented on Nov 15, 2026

    @TMHSDigital
    OwnerAuthor

    Fixed in 87cc6b6. All three items are done.

    1. Wordlist-sized maps: a new wordlist.NotIn(names, entries) puts the few proposed names in a set and makes one pass over the entries. passive.Merge, the CLI's candidates.add and permutation pass, the TUI and pkg/subenum use it, and the which-source-found-it labels from Record which source (wordlist, ct, wayback, axfr, permute) found each name #273 still work. TestCandidatesNoWordlistSizedMap (200k entries) allows at most 1 MiB; the old permutation code allocated about 14 MB.
    2. Lab server counter: per-name counting is opt-in (dnsserver.Options.CountNames, set by internal/dnstest). -simulate-zone and MCP lab_scan keep only the atomic total and take no mutex per query. TestCountNamesOptIn covers it.
    3. CNAME buffer: a UDP reply is read into a 1232-byte buffer, the payload size the query advertises, and a TCP reply into exactly the length its prefix announces. TestCNAMEReadBuffer allows 16 KiB per lookup; with the 64 KB buffer it measured about 70 KB, now about 6 KB.

    go test ./... and golangci-lint run pass, and the new tests pass with -race -count=5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueperformanceseverity: low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions