Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Storage is cheap - the primary win here is computation time.

if(ip1 == ip2) is a lot faster as ints than strings.



Seeing as how the largest dotted-quad IP address fits inside rax:rdx on a modern CPU, and that two of them fit in a single cache line, I'm guessing x == y, while faster, is not "much" faster with strings than integers.

I wouldn't populate an address trie with strings, but I also wouldn't give a second thought to passing them around a random C program as charstars either.


Most string libs aren't that smart though - string comparisons are still byte by byte. You're now comparing something 15 times instead of 1. You can do an int32/int64 (depending on architecture) compare in a single op.

The point I guess is, you can keep 'em around as charstars, but eventually you'll have to do this cast to compare them...


All memcmp's are this smart. But that's kind of besides the point, right? 1 time, 14 times, if we're talking about L1 cache, we're really epsilon from pure reg/reg ALU operations, implementing effectively constant-time algorithms.

I agree, int32 is faster. Like I said, it's just not "much" faster.


But what if I want to count all the 10.x requests, not sure I can bitshift in an SQL query.


  select count(*) from ipTable
  where ip >= 167772160 and ip < 184549376
IP is four digits in a 256-base integer. You are looking for ips with the first digit 10. So, the value boundaries are: 167772160 = 256 * 256 * 256 * 10; 184549376 = 256 * 256 * 256 * 11.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: