less memory use
This commit is contained in:
parent
0206070abf
commit
fb82119b66
27
sanitize.go
27
sanitize.go
|
@ -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 {
|
||||
|
@ -25,12 +31,13 @@ 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 {
|
||||
|
@ -38,7 +45,7 @@ func Dedup(rrs []RR) []RR {
|
|||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
return rrs[:j]
|
||||
}
|
||||
|
||||
// normalizedString returns a normalized string from r. The TTL
|
||||
|
|
|
@ -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())
|
||||
|
@ -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 )
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue