From c6cc38c9cf0c1be72ad608dd662df1799fe63438 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 31 Jan 2015 01:53:01 +1100 Subject: [PATCH] iplist: Make range descriptions copies of substring, saves 8MB heap on level1 blocklist! --- client.go | 2 +- iplist/iplist.go | 17 ++++++++--------- iplist/iplist_test.go | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index cd65f55d..513e7871 100644 --- a/client.go +++ b/client.go @@ -347,7 +347,7 @@ func (cl *Client) setEnvBlocklist() (err error) { var ranges []iplist.Range scanner := bufio.NewScanner(f) for scanner.Scan() { - r, ok, lineErr := iplist.ParseBlocklistP2PLine(scanner.Text()) + r, ok, lineErr := iplist.ParseBlocklistP2PLine(scanner.Bytes()) if lineErr != nil { err = fmt.Errorf("error reading torrent blocklist line: %s", lineErr) return diff --git a/iplist/iplist.go b/iplist/iplist.go index 9fe9871e..ed8ac06e 100644 --- a/iplist/iplist.go +++ b/iplist/iplist.go @@ -5,7 +5,6 @@ import ( "fmt" "net" "sort" - "strings" ) type IPList struct { @@ -52,16 +51,16 @@ func (me *IPList) Lookup(ip net.IP) (r *Range) { // Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but // no error if a line doesn't contain a range but isn't erroneous, such as // comment and blank lines. -func ParseBlocklistP2PLine(l string) (r Range, ok bool, err error) { - l = strings.TrimSpace(l) - if len(l) == 0 || strings.HasPrefix(l, "#") { +func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) { + l = bytes.TrimSpace(l) + if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) { return } - colon := strings.IndexByte(l, ':') - hyphen := strings.IndexByte(l[colon+1:], '-') + colon + 1 - r.Description = l[:colon] - r.First = net.ParseIP(l[colon+1 : hyphen]) - r.Last = net.ParseIP(l[hyphen+1:]) + colon := bytes.IndexByte(l, ':') + hyphen := bytes.IndexByte(l[colon+1:], '-') + colon + 1 + r.Description = string(l[:colon]) + r.First = net.ParseIP(string(l[colon+1 : hyphen])) + r.Last = net.ParseIP(string(l[hyphen+1:])) if r.First == nil || r.Last == nil { return } diff --git a/iplist/iplist_test.go b/iplist/iplist_test.go index cc934c10..08f5b80f 100644 --- a/iplist/iplist_test.go +++ b/iplist/iplist_test.go @@ -16,7 +16,7 @@ b:1.2.8.0-1.2.8.255` func sampleRanges(tb testing.TB) (ranges []Range, err error) { scanner := bufio.NewScanner(strings.NewReader(sample)) for scanner.Scan() { - r, ok, _ := ParseBlocklistP2PLine(scanner.Text()) + r, ok, _ := ParseBlocklistP2PLine(scanner.Bytes()) if ok { // tb.Log(r) ranges = append(ranges, r) -- 2.44.0