6 ms·
The performance engineering in NGINX back then was really quite something. This classic 2007 tutorial starts by pointing out that NGINX parses the HTTP verb by
by simonw 5y ago
The performance engineering in NGINX back then was really quite something.
This classic 2007 tutorial starts by pointing out that NGINX parses the HTTP verb by looking at the second letter first, so that if it's O it knows to check for POST or COPY!
https://web.archive.org/web/20070505051653/http://www.riceonfire.org/emiller/nginx-modules-guide.html https://web.archive.org/web/20070505051653/http://www.riceon...
- danachow 5y agoDoes it really look at second letter first or is that snippet taken out of context (it isn’t implied that it does in that email, just that it doesn’t use a library function) ? Since most requests are GET it still makes sense to handle that case first. Though after trying to common cases looking at the second letter for the P subcases may save some branching.
- MaxBarraclough 5y agoDoing Boyer and Moore proud. I think that if you want to support all verbs, you face at least 3 'ambiguities' whether you first check the first, second, or third character of the string. (It must be at most the third, as the shortest verbs are 3 characters long.) First checking the first character is ambiguous between POST, PUT and PATCH. First checking the second character is ambiguous between HEAD, DELETE, and GET. First checking the third character is ambiguous between GET, PUT, OPTIONS, and PATCH. [0] edit As danachow points out, the verbs are not all used with the same frequency. If real-world performance is the goal we'd presumably want to optimise for the GET case, which presumably means first checking the first character, as the 'G' is unique to GET. [0] https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
- stormbrew 5y agoSeems likely an optimal algorithm could probably get there just looking sequentially at the first three letters in order. Would be an interesting code-golf.
- sseagull 5y agoA bit of a micro-optimization, but couldn’t you interpret 4 bytes (including the trailing null on the three-letter verbs) as a 32-bit unsigned int and then do integer comparisons or a case statement?
- dlsa 5y agoYou could add a computed goto to really spice things up.
- stazz1 5y agocall it "adventure plumbing"
- morelisp 5y agoThis is close to what nginx does today, if your platform supports unaligned reads. The check for 'O' is a specific fast-path only for HEAD, most others are done via ordinary comparisons.
- legulere 5y agoNGINX does exactly that (after finding the space character after the verb): https://github.com/nginx/nginx/blob/7587778a33bea0ce6f203a8c4de18e33f38b9582/src/http/ngx_http_parse.c#L38 https://github.com/nginx/nginx/blob/7587778a33bea0ce6f203a8c... There are several points to note: This trick only works if the architecture supports unaligned memory access. Macros are used instead of functions, so you don't have to rely on the compiler to do inlining. The shifts and ors should get optimized out by the compiler. I once tried something similar in C#, but sadly the JIT doesn't do that optimization.
- sseagull 5y agoAh nice! I was interested in looking but don’t know the codebase. That method does look better. My idea assumes that the method has already been checked to see if it is a valid method, which has its own cost. Otherwise, POSABC would be parsed as POST. Their method does that more or less on the fly.
- cft 5y agoMost requests in reality are GET. So if you first check for G, if (m[0] == 'G') { r->method = NGX_HTTP_GET; break; } else if (m[0] == 'P') { switch(m[1]) { ... } } else if ... this should be fast enough.
- deleted 5y ago[deleted]
- tentacleuno 5y agoHow would this work if the method field was something like "Gibberish"? Would this have any security vulnerabilities?
- deleted 5y ago[deleted]
- w-ll 5y agoIf its "Gibberish" I'd suggest it's safer to assume it as a Get request regardless. Something I don't see many people talk about, but I've setup web instances with both read and write clusters similar to db's, and have the nicety to getting more umph from the read cluster by making sure it only hits read db nodes and disabling any framework stuff for tracking changes on a model.
- jcampbell1 5y agoDid you experience any read after write inconsistencies that present as heisenbugs?
- dotancohen 5y agoWould you expect that to be more of an issue under GP's method than with traditional sharding and replication? Presumably writes are then replicated to all instances, just not all instances accept them.
- Cthulhu_ 5y ago
- zxcvbn4038 5y agoYes, nginx code is something you spend a lot of time studying and hope one day you can do as well.
- patcon 5y agoNice! I can't be the only one who went looking and spent way to much time trying to acquaint myself with c++ :) Here's there I ended up: https://github.com/nginx/nginx/blob/363505e806feebb7ceb1f9edb0e3f75c1253384f/src/http/v2/ngx_http_v2.c#L3580 https://github.com/nginx/nginx/blob/363505e806feebb7ceb1f9ed... PS, I'm not totally sure, but they definitely use the count of letters as an optimization, and it seems they increment the bits associated with each type, so the order of the bits behind each NGX_HTTP_GET etc seems to matter...! https://github.com/nginx/nginx/blob/67d160bf25e02ba6679bb6c3b9cbdfeb29b759de/src/http/ngx_http_request.h#L28-L44 https://github.com/nginx/nginx/blob/67d160bf25e02ba6679bb6c3... Someday I will understand :)
- Brybry 5y agoIt's been a while since I have C/C++'d so some of my terminology is probably wrong. ngx_http_v2_parse_method iterates through all the tests, starting with the first test (GET). They compare request method string length to test string length then on matching lengths compare each character in the request method string to the test string. Fully matching strings set the request method numerical value from the test value and returns OK. Any non-matching characters GOTO the next test. After that it does a sanity check on the request method string characters (A to Z or _ or -) and returns OK or DECLINED as appropriate. For the macros defining the HTTP method numerical values I think they're set up that way as bitmasks for bitwise operations. For example, they do things like[1] if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) Here HTTP request method value is bitwise AND against (0x00000002 OR 0x00000004) Any non-zero value here would be true and any zero value is false. So if the request method bit value AND matches either GET or HEAD bits then this conditional is false. [1] https://github.com/nginx/nginx/blob/a64190933e06758d50eea926e6a55974645096fd/src/http/modules/ngx_http_stub_status_module.c#L92 https://github.com/nginx/nginx/blob/a64190933e06758d50eea926...
- morelisp 5y agoThese tricks are cute, but at the time nginx's performance came from much more fundamental design decisions. First, the async model was literally years ahead of Apache. It leaned heavily on interfaces like epoll to manage large connection pools with a small number of processes, while Apache still used a thread or process per connection. Second, it removed exactly the right features - those with minimal benefit and high performance impact. The classic example is .htaccess, which adds (at least) one stat to every single request, but in practice was only needed for the horrible multi-tenant LAMP reseller setups of the day - everyone else was fine with static centralized configuration.
- stormbrew 5y agonote: this thread is about nginx and lighttpd. I don't think anyone is under any illusions that apache was way behind the curve either of those were setting.
- e12e 5y agoMore on nginx vs apache here, for those interested: The Architecture of Open Source Applications - volume II - nginx: https://www.aosabook.org/en/nginx.html https://www.aosabook.org/en/nginx.html
- fomine3 5y agoFor who want to check current implementation: https://github.com/nginx/nginx/blob/429150c1fa78317bdb19de380ce709651dbc042c/src/http/ngx_http_parse.c#L142 https://github.com/nginx/nginx/blob/429150c1fa78317bdb19de38...