Skip to content

Commit a10eb6d

Browse files
TMHSDigitalclaude
andcommitted
Bucket -hosts names by target once (#465)
With -hosts and -dL, each target's For walked every host name and recomputed its owner, so selection was O(names x targets), run once per target and again for the lookahead. LimitTo now buckets the entry indexes by owner in one pass and counts the names under no target; For walks only its bucket and Outside returns the count. BenchmarkHostsManyTargets (200k names, 2000 targets): 8.93 s/op before, 11.4 ms/op after. Fixes #465 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5fe6e26 commit a10eb6d

3 files changed

Lines changed: 74 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- `-max-duration` is kept under a low `-rate`: workers waiting for a rate slot when the budget ran out still sent their queries afterwards, so a 2 s budget at `-rate 5` ran about 43 s. No slot is handed out past the deadline now, a name's queries are sent all together or not at all, and names that never got a slot are counted as skipped and stay resumable (#460).
1616
- With `-dL` and `-rate`, the next target's `-axfr` zone transfer and `-nsec-walk` no longer run beside the current target's scan, where their queries came on top of `-rate`; only its passive sources are fetched ahead, and the transfer and walk run when that target starts. Without `-rate` nothing changes (#461).
1717
- Takeover candidates and internal addresses found by the `-permute` and `-ptr` passes now count in `takeover_candidates` and `internal_addresses` (in `-stats`, `subenum report` and the Action's `takeovers` and `internal` outputs), as they already did in the results; under `-permute-only` they were always 0. The Action's `found` output and the report's Found column also include `ptr_found` (#458).
18+
- `-hosts` with `-dL` no longer walks the whole host list once per target: the names are sorted by target once, so a list of 200,000 names over 2,000 targets is split in about 11 ms instead of about 9 s of CPU before and between targets (#465).
1819

1920
## [0.12.0] - 2026-11-29
2021

‎internal/wordlist/prepare_test.go‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math/rand/v2"
66
"reflect"
77
"regexp"
8+
"slices"
89
"strings"
910
"testing"
1011
)
@@ -187,3 +188,58 @@ func BenchmarkFor1M(b *testing.B) {
187188
p.For("example.com")
188189
}
189190
}
191+
192+
// BenchmarkHostsManyTargets measures selecting every target's names from a
193+
// -hosts list under -dL (#465): 200k names spread over 2000 targets. Each
194+
// For walks only its own target's names, so the whole selection is linear
195+
// in the names rather than names x targets.
196+
func BenchmarkHostsManyTargets(b *testing.B) {
197+
const targets, perTarget = 2000, 100
198+
lines := make([]string, 0, targets*perTarget)
199+
roots := make([]string, targets)
200+
for t := range targets {
201+
roots[t] = fmt.Sprintf("t%d.example", t)
202+
for n := range perTarget {
203+
lines = append(lines, fmt.Sprintf("h%d.%s", n, roots[t]))
204+
}
205+
}
206+
p := Prepare(lines)
207+
b.ReportAllocs()
208+
for b.Loop() {
209+
p.LimitTo(roots)
210+
for _, r := range roots {
211+
p.For(r)
212+
}
213+
}
214+
}
215+
216+
// TestLimitToBuckets covers #465: with names spread over many targets, each
217+
// target gets exactly its own names, and Outside counts the rest.
218+
func TestLimitToBuckets(t *testing.T) {
219+
var lines []string
220+
for t := range 50 {
221+
for n := range 3 {
222+
lines = append(lines, fmt.Sprintf("h%d.t%d.example", n, t))
223+
}
224+
}
225+
lines = append(lines, "stray.other.test", "h0.t0.example")
226+
p := Prepare(lines)
227+
roots := make([]string, 50)
228+
for i := range roots {
229+
roots[i] = fmt.Sprintf("t%d.example", i)
230+
}
231+
p.LimitTo(roots)
232+
for i, r := range roots {
233+
got, dup, _ := p.For(r)
234+
want := []string{"h0", "h1", "h2"}
235+
if !slices.Equal(got, want) {
236+
t.Fatalf("%s: entries %q, want %q", r, got, want)
237+
}
238+
if wantDup := map[bool]int{true: 1, false: 0}[i == 0]; dup != wantDup {
239+
t.Errorf("%s: duplicates %d, want %d", r, dup, wantDup)
240+
}
241+
}
242+
if n := p.Outside(); n != 1 {
243+
t.Errorf("Outside = %d, want 1", n)
244+
}
245+
}

‎internal/wordlist/reader.go‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ type Prepared struct {
159159
// scope, when set, makes the entries full host names (-hosts, #351): a
160160
// target uses only the names under it, and none of the others.
161161
scope map[string]struct{}
162+
// byOwner holds, per LimitTo target, the indexes of the entries it owns,
163+
// and outside counts the entries no target owns. LimitTo fills both in
164+
// one pass, so selecting every target's names is linear in the names,
165+
// not names x targets (#465).
166+
byOwner map[string][]uint32
167+
outside int
162168
}
163169

164170
// LimitTo makes p a list of full host names for these targets (-hosts,
@@ -170,6 +176,14 @@ func (p *Prepared) LimitTo(targets []string) {
170176
for _, t := range targets {
171177
p.scope[strings.ToLower(strings.TrimSuffix(t, "."))] = struct{}{}
172178
}
179+
p.byOwner, p.outside = make(map[string][]uint32, len(targets)), 0
180+
for i, entry := range p.entries {
181+
if o := p.owner(entry); o != "" {
182+
p.byOwner[o] = append(p.byOwner[o], uint32(i)) //nolint:gosec // entries are indexed by uint32 (entryIndex)
183+
} else {
184+
p.outside++
185+
}
186+
}
173187
}
174188

175189
// owner returns the most specific LimitTo target that name is under, or "".
@@ -190,23 +204,15 @@ func (p *Prepared) owner(name string) string {
190204
// Outside counts the distinct names of a LimitTo list that are under none
191205
// of its targets.
192206
func (p *Prepared) Outside() int {
193-
n := 0
194-
for _, entry := range p.entries {
195-
if p.owner(entry) == "" {
196-
n++
197-
}
198-
}
199-
return n
207+
return p.outside
200208
}
201209

202210
// under is For for a LimitTo list.
203211
func (p *Prepared) under(domain string) (entries []string, duplicates, skipped int) {
204212
limit := maxNameLen - 1 - len(domain)
205213
skipped = p.invalid
206-
for i, entry := range p.entries {
207-
if p.owner(entry) != domain {
208-
continue
209-
}
214+
for _, i := range p.byOwner[domain] {
215+
entry := p.entries[i]
210216
prefix := entry[:len(entry)-len(domain)-1]
211217
if len(prefix) > limit {
212218
skipped += int(p.counts[i])

0 commit comments

Comments
 (0)