8 ms·
Is this first code snippet, where the HTTP parser checks for "POST" or "COPY" real? It certainly reads like a joke on how not to optimize your code. Also, woul
by phoboslab 17y ago
Is this first code snippet, where the HTTP parser checks for "POST" or "COPY" real? It certainly reads like a joke on how not to optimize your code.
Also, wouldn't it be faster to check all four characters at once with a 32bit compare? And what about branch prediction of the CPU?
- vier 17y agothat's exactly what it's doing
- ccarpenterg 17y agoWhat is a 'branch prediction of the CPU'?
- alexgartrell 17y agoThe CPU has an instruction cache (it reaches out to memory for the series of instructions and grabs a whole bunch at once to be more efficient). When there's a branch (logically, an if statement, a loop, or a goto, or a switch) it has to make a decision about whether or not it should cache the instructions in the if statement or the instructions following the if statement. Within the linux kernel the macros likely and unlikely are defined so that you can have code of the style if(likely(x > 10)) { /* These instructions, which probably will be executed, * will be cached */ } if(unlikely(ptr == NULL)) { /* These instructions, which probably won't need to be * executed, will not be cached */ }