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

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?



What is a 'branch prediction of the CPU'?


The 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
     */
  }


that's exactly what it's doing




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: