Changeset 926

Show
Ignore:
Timestamp:
12/14/06 21:24:11 (2 years ago)
Author:
ahu
Message:

speed up both parsing of IP addresses from textual form as well as writing out
saves around 6 usec for a round trip

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/pdns/pdns/rcpgenerator.cc

    r850 r926  
    6767  if(!isdigit(d_string.at(d_pos))) 
    6868    throw RecordTextException("while parsing IP address, expected digits at position "+lexical_cast<string>(d_pos)+" in '"+d_string+"'"); 
    69    
    70   string ip; 
    71   xfrLabel(ip); 
    72   if(!IpToU32(ip, &val)) 
    73     throw RecordTextException("unable to parse IP address '"+ip+"'"); 
     69 
     70  uint32_t octet=0; 
     71  val=0; 
     72  char count=0; 
     73   
     74  for(;;) { 
     75    if(d_string.at(d_pos)=='.') { 
     76      val<<=8; 
     77      val+=octet; 
     78      octet=0; 
     79      count++; 
     80      if(count > 3) 
     81        break; 
     82    } 
     83    else if(isdigit(d_string.at(d_pos))) { 
     84      octet*=10; 
     85      octet+=d_string.at(d_pos) - '0'; 
     86      if(octet > 255) 
     87        throw RecordTextException("unable to parse IP address"); 
     88    } 
     89    else if(dns_isspace(d_string.at(d_pos)))  
     90      break; 
     91    else 
     92      throw RecordTextException("unable to parse IP address, strange character: "+d_string.at(d_pos)); 
     93 
     94    d_pos++; 
     95    if(d_pos == d_string.length()) 
     96      break; 
     97  } 
     98  if(count<=3) { 
     99    val<<=8; 
     100    val+=octet; 
     101  } 
     102  val=ntohl(val); 
    74103} 
    75104 
     
    251280 
    252281  char tmp[17]; 
    253   snprintf(tmp, sizeof(tmp)-1, "%u.%u.%u.%u",  
    254            (val >> 24)&0xff, 
    255            (val >> 16)&0xff, 
    256            (val >>  8)&0xff, 
    257            (val      )&0xff); 
    258    
    259   d_string+=tmp; 
     282  uint32_t ip=htonl(val); 
     283  uint8_t vals[4]; 
     284 
     285  memcpy(&vals[0], &ip, sizeof(ip)); 
     286 
     287  char *pos=tmp; 
     288 
     289  for(int n=0; n < 4; ++n) { 
     290    if(vals[n]<10) { 
     291      *(pos++)=vals[n]+'0'; 
     292    } else if(vals[n] < 100) { 
     293      *(pos++)=(vals[n]/10) +'0'; 
     294      *(pos++)=(vals[n]%10) +'0'; 
     295    } else { 
     296      *(pos++)=(vals[n]/100) +'0'; 
     297      vals[n]%=100; 
     298      *(pos++)=(vals[n]/10) +'0'; 
     299      *(pos++)=(vals[n]%10) +'0'; 
     300    } 
     301    if(n!=3) 
     302      *(pos++)='.'; 
     303  } 
     304  *pos=0; 
     305  d_string.append(tmp, pos); 
    260306} 
    261307