Skip to content

Commit

Permalink
Use simple scanner instead of regexp for etchosts.Delete
Browse files Browse the repository at this point in the history
Benchmark results:
benchmark           old ns/op     new ns/op     delta
BenchmarkDelete     4315186       2245829       -47.96%

benchmark           old allocs     new allocs     delta
BenchmarkDelete     4645           10             -99.78%

benchmark           old bytes     new bytes     delta
BenchmarkDelete     1590011       4832          -99.70%

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
  • Loading branch information
LK4D4 committed Oct 23, 2015
1 parent ea0e4dd commit 9536e8e
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions etchosts/etchosts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package etchosts

import (
"bufio"
"bytes"
"fmt"
"io"
Expand Down Expand Up @@ -138,19 +139,36 @@ func Delete(path string, recs []Record) error {
if len(recs) == 0 {
return nil
}

old, err := ioutil.ReadFile(path)
old, err := os.Open(path)
if err != nil {
return err
}

regexpStr := fmt.Sprintf("\\S*\\t%s\\n", regexp.QuoteMeta(recs[0].Hosts))
for _, r := range recs[1:] {
regexpStr = regexpStr + "|" + fmt.Sprintf("\\S*\\t%s\\n", regexp.QuoteMeta(r.Hosts))
var buf bytes.Buffer

s := bufio.NewScanner(old)
eol := []byte{'\n'}
loop:
for s.Scan() {
b := s.Bytes()
if b[0] == '#' {
buf.Write(b)
buf.Write(eol)
continue
}
for _, r := range recs {
if bytes.HasSuffix(b, []byte("\t"+r.Hosts)) {
continue loop
}
}
buf.Write(b)
buf.Write(eol)
}

var re = regexp.MustCompile(regexpStr)
return ioutil.WriteFile(path, re.ReplaceAll(old, []byte("")), 0644)
old.Close()
if err := s.Err(); err != nil {
return err
}
return ioutil.WriteFile(path, buf.Bytes(), 0644)
}

// Update all IP addresses where hostname matches.
Expand Down

0 comments on commit 9536e8e

Please sign in to comment.