5 ms·
A 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 i
by sseagull 5y ago
A 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.
- MaxBarraclough 5y ago> This trick only works if the architecture supports unaligned memory access Presumably then it's relying on undefined behaviour.
- stormbrew 5y agopretty sure it's just implementation defined. In the sense that the implementation defines the allowed alignment, if nothing else (ie. on x86 the allowed alignment is 1 byte even if that's not optimal).
- MaxBarraclough 5y agoI don't think so. What I can find on this topic with a quick search points to it being outright UB. https://stackoverflow.com/a/47622343/ https://stackoverflow.com/a/47622343/ https://stackoverflow.com/a/28895321/ https://stackoverflow.com/a/28895321/ https://blog.quarkslab.com/unaligned-accesses-in-cc-what-why-and-solutions-to-do-it-properly.html https://blog.quarkslab.com/unaligned-accesses-in-cc-what-why...