]> Sergey Matveev's repositories - btrtrc.git/commitdiff
iplist: Make range descriptions copies of substring, saves 8MB heap on level1 blocklist!
authorMatt Joiner <anacrolix@gmail.com>
Fri, 30 Jan 2015 14:53:01 +0000 (01:53 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 30 Jan 2015 14:53:01 +0000 (01:53 +1100)
client.go
iplist/iplist.go
iplist/iplist_test.go

index cd65f55de54994213f2b8c759148b0129b673c2b..513e7871d2231177ad8abccffb396b421ff22788 100644 (file)
--- 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
index 9fe9871efe88e2e85bce725fc4cd78a0b2dd6e7e..ed8ac06e8db807a3c839a4eadb0983c0673bd030 100644 (file)
@@ -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
        }
index cc934c104d652ee27181ef126f8596404541a8ff..08f5b80fdeb61a608db7de214ab4c5be791b418f 100644 (file)
@@ -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)