5 ms·
I got disappointed at the beginning. Not only folding string checking into a tree of letter by letter checks doesn't buy you performance with 4 letters, but als
by viraptor 17y ago
I got disappointed at the beginning. Not only folding string checking into a tree of letter by letter checks doesn't buy you performance with 4 letters, but also if you bring that as an example of performance improvement in a server you're just showing your ignorance.
Great speed improvement:
switch (*((uint32*)m)) {
case 0x54534f50: r->method = NGX_HTTP_POST;
Does it matter now if I spend `O(no_reqests^2)` time looking for the next connection to serve?
- jared314 17y agoThat snippet appears to be out of date. Version 0.8.34 does something similar to what you suggest. #define ngx_str3Ocmp(m, c0, c1, c2, c3) \ *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)
- yosh 17y agoCareful with casting strings to ints like that, it'll break on architectures that require aligned accesses, i.e. if you try to load a uint32, the address of the memory location must be a multiple of 4. x86 happens to be fine with non-aligned access, but many other architectures aren't. If you look at the nginx source, that #define is guarded under NGX_HAVE_NONALIGNED and there is a fallback that does straight byte by byte comparison.
- viraptor 17y ago> fallback that does straight byte by byte comparison That's not even funny anymore... I wonder how many iterations will it take, until they discover that strncmp compiles down to "repz cmpsb" (or equivalent) and you really cannot do better than that with if-s on each character.
- oldgregg 17y agoI like to think I'm a decent programmer. Then I read threads like this. :o) Keep learning...
- jws 17y agoSo, how long does it take to identify a verb on a 2.66GHz Core2 MacBook Pro… FOURCC 4.9ns // The parent comment code FOURCC-without-alignment-bug 6.1ns // ... safely assemble the word NGINX 10.3ns // main ariticle's code STRCMP 17.6ns // most commen verbs first The FOURCC codes as in the parent only work if you can assume you know the character after the verb to be a space or other known character because GET and PUT are too short. They also require disambiguating code for the 5+ letter verbs but these are all relatively rare (probably). My strings are all 4 byte aligned, so the FOURCC case may be getting an unfair boost, it never generates an unaligned read which possibly is slower. Test data is a set of jumbled GET,HEAD,POST,PUT lookups that I made up to meet my idea of a web server. There are some worst cases hiding out there, for instance all PROPPATCH verbs… FOURCC 14.7ns FOURCC-without-alignment-bug 17.7ns NGINX 19.8ns STRCMP 37.4ns Feels like gcc sequentially tests case values. Make sure to put them in the order for common verbs to match first. The FOURCC codes get the most control here. NGINX is most consistent, probably from its two layered lookup nature. So there you have it: nginx's algorithm is significantly better then strcmp, and fairly independent of verb distribution. A FOURCC lookup is faster, even if made safe for all architectures. And finally, strcmp isn't that bad (freebsd anyway, the GNU libc is about half as fast as seen in further testing). (I am left with a nagging desire to check a perfect hashing function though.) Edit: gperf is about 50% slower than the nginx method. No need to go that direction for perfect hashing.
- jws 17y agoSo what about other architectures? How about a 1.6GHz Atom 330 processor. This doesn't have out of order execution and should more closely match ARM and MIPS type small machines. (Sorry, I can't get one of those booted from where I am now.) FOURCC 23.3ns FOURCC-without-alignment-bug 27.6ns NGINX 29.8ns STRCMP 70.5ns (glibc) The difference between FOURCC and NGINX is vanishing here. How about a 3GHz core-i7 840 FOURCC 4.8ns FOURCC-without-alignment-bug 5.6ns NGINX 8.3ns STRCMP 13.9ns (freebsd) Not a lot faster than a Core-2, but some. Of course it has twice the cores plus hyperthreading ready to go. How about if you are cheap (like me) and got a 2.93GHz core i3 for your server: FOURCC 5.3ns FOURCC-without-alignment-bug 5.8ns NGINX 9.9ns STRCMP 33.6ns (glibc) Not bad for the little guy. (Saved $100 on the cpu, plus didn't need to add a video card with some goofey driver, and its system wide low power state is almost as good at the Atom's.)