Ticket #239: ip_filter_no_boost-ip_filter_no_boost-fast.patch

File ip_filter_no_boost-ip_filter_no_boost-fast.patch, 3.8 KB (added by yb, 2 years ago)

to make ip_filter_no_boost faster 20 times faster

  • src/core/ip_filter.cc

    diff -ruN rtorrent-0.8.5-ip_filter_no_boost/src/core/ip_filter.cc rtorrent-0.8.5-ip_filter_no_boost-new/src/core/ip_filter.cc
    old new  
    6060} 
    6161 
    6262int IpFilter::add_from_file( const std::string& fileName, range_map* rs, str_list* files ) { 
    63         std::ifstream in( fileName.c_str() ); 
    64         std::string line; 
     63        FILE *f = fopen(fileName.c_str(),"r"); 
    6564        int mergeCount = 0; 
    66  
    67         if( in.fail() || !in.is_open() ) 
    68                 return -1; 
    69  
    70         while( in.good() ) { 
    71                 std::getline( in, line ); 
    72                 utils::trim( line ); 
    73  
    74                 if( (line[0] == '#') || (line.length() == 0) || (line[0] == 0) ) 
     65        if (f==0) return -1; 
     66        char *line = (char *)malloc(64); 
     67        size_t sz=64; 
     68        int charsread = 0; 
     69        int linesread=0; 
     70        while( (charsread=getline(&line,&sz,f)) >=0 ) { 
     71                if( (line[0] == '#' ) || ( charsread <= 1 ) )  
    7572                        continue; 
    76  
    77                 IpRange* ir = IpRange::parse( line ); 
     73                 
     74                IpRange* ir = IpRange::parse( line, charsread ); 
    7875                if( !ir || !ir->get_from() || !ir->get_to() ) 
    7976                        continue; 
    8077 
    8178                mergeCount += merge_and_insert( rs, ir ); 
    8279        } 
     80        free(line); 
    8381        files->push_back( std::string(fileName) ); 
    84         in.close(); 
    85  
     82        fclose(f); 
    8683        m_merges += mergeCount; 
    8784        return mergeCount; 
    8885} 
  • src/core/ip_range.cc

    diff -ruN rtorrent-0.8.5-ip_filter_no_boost/src/core/ip_range.cc rtorrent-0.8.5-ip_filter_no_boost-new/src/core/ip_range.cc
    old new  
    4848        return r; 
    4949} 
    5050 
     51//fast version 
     52IpRange* IpRange::parse( const char *s, const int size ){ 
     53        static char description[256]; 
     54        static char ip1[24], ip2[24]; 
     55        int pos=0, post=0, enddesc=size-1; 
     56        while (enddesc>0 && s[enddesc]!=':') enddesc--; //find last ':' in the line 
     57        while((pos<enddesc) && (unsigned char)s[pos]<=' ') pos++; // strip from start 
     58        while ((pos<enddesc)){ 
     59          if (post<255) description[post++]=s[pos]; 
     60          pos++; 
     61        }  
     62        description[post]=0; 
     63        if (s[pos]==':') pos++; 
     64          post=0; 
     65          while ((pos<size) && s[pos]!='-'){ 
     66            if (post<23) ip1[post++]=s[pos]; 
     67            pos++; 
     68          }  
     69          ip1[post]=0; 
     70          if (s[pos]=='-'){ 
     71            pos++; 
     72            post=0; 
     73                while ((pos<size) && s[pos]>' '){ 
     74              if (post<23) ip2[post++]=s[pos]; 
     75              pos++; 
     76                } 
     77                ip2[post]=0; 
     78          } else ip2[0]=0; 
     79 
     80        IpAddress*      from            = IpAddress::parse(ip1); 
     81        IpAddress*      to            = IpAddress::parse(ip2); 
     82 
     83        if( !from ) { 
     84                std::cout << "!! from is invalid: description='" << description << ", ip1=" << ip1 << ", ip2=" << ip2 << std::endl; 
     85                return NULL; 
     86        } 
     87        if( !to ) { 
     88                std::cout << "!! to is invalid: description='" << description << ", ip1=" << ip1 << ", ip2=" << ip2 << std::endl; 
     89                return NULL; 
     90        } 
     91 
     92        IpRange* r = new IpRange(); 
     93        r->m_description        = description; 
     94        r->m_from               = from; 
     95        r->m_to                 = to; 
     96 
     97        if( (*to < *from) ) { 
     98                std::cout << "!! to < from: " << r->to_string() << std::endl; 
     99                delete r; 
     100                return NULL; 
     101        } 
     102         
     103        return r; 
     104} 
     105 
    51106std::string IpRange::to_string() const { 
    52107        std::stringstream result; 
    53108        result << m_description << ": [" << m_from->to_string() << " - " << m_to->to_string() << ']'; 
  • src/core/ip_range.h

    diff -ruN rtorrent-0.8.5-ip_filter_no_boost/src/core/ip_range.h rtorrent-0.8.5-ip_filter_no_boost-new/src/core/ip_range.h
    old new  
    3030        public: // static methods 
    3131                typedef IpRange* ptr; 
    3232                static IpRange* parse( const std::string& s ); 
     33                static IpRange* parse( const char *s, const int size ); 
    3334         
    3435        public: // dynamic methods 
    3536                IpRange( IpRange& rng ) { copy(rng); }