Skip to content

Commit

Permalink
less memory use
Browse files Browse the repository at this point in the history
  • Loading branch information
miekg committed Aug 31, 2015
1 parent 0206070 commit fb82119
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
27 changes: 17 additions & 10 deletions sanitize.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package dns

// Dedup removes identical RRs from rrs. It preserves the original ordering.
// The lowest TTL of any duplicates is used in the remaining one.
func Dedup(rrs []RR) []RR {
m := make(map[string]RR)
keys := make([]string, 0, len(rrs))
// The lowest TTL of any duplicates is used in the remaining one. Dedup modifies
// rrs.
// m is used to store the RRs temporay. If it is nil a new map will be allocated.
func Dedup(rrs []RR, m map[string]RR) []RR {
if m == nil {
m = make(map[string]RR)
}
// We need a (ordered) slice of keys to preserve the original ordering.
// Otherwise we could just range of the map directly.
keys := make([]*string, 0, len(rrs))

for _, r := range rrs {
key := normalizedString(r)
keys = append(keys, key)
keys = append(keys, &key)
if _, ok := m[key]; ok {
// Shortest TTL wins.
if m[key].Header().Ttl > r.Header().Ttl {
Expand All @@ -25,20 +31,21 @@ func Dedup(rrs []RR) []RR {
return rrs
}

ret := make([]RR, 0, len(rrs))
j := 0
for i, r := range rrs {
// If keys[i] lives in the map, we should copy and remove it.
if _, ok := m[keys[i]]; ok {
delete(m, keys[i])
ret = append(ret, r)
if _, ok := m[*keys[i]]; ok {
delete(m, *keys[i])
rrs[j] = r
j++
}

if len(m) == 0 {
break
}
}

return ret
return rrs[:j]
}

// normalizedString returns a normalized string from r. The TTL
Expand Down
5 changes: 3 additions & 2 deletions sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestDedup(t *testing.T) {
}

for rr, expected := range testcases {
out := Dedup([]RR{rr[0], rr[1], rr[2]})
out := Dedup([]RR{rr[0], rr[1], rr[2]}, nil)
for i, o := range out {
if o.String() != expected[i] {
t.Fatalf("expected %v, got %v", expected[i], o.String())
Expand All @@ -55,8 +55,9 @@ func BenchmarkDedup(b *testing.B) {
newRR(nil, "mieK.Nl. 1000 IN A 127.0.0.1"),
newRR(nil, "Miek.nL. 500 IN A 127.0.0.1"),
}
m := make(map[string]RR)
for i := 0; i < b.N; i++ {
Dedup(rrs)
Dedup(rrs,m )
}
}

Expand Down

0 comments on commit fb82119

Please sign in to comment.