Hacker News new | past | comments | ask | show | jobs | submit login

Today I learned that for modern processors, quicksort code is small enough that the compiler can inline them if the definition is visible during compilation. See here for comparison of isort function implemented using plain static functions vs the macro in TFA: https://godbolt.org/z/arGjPGhKE . It's not as flexible as the macro form (not being able to pass two arrays at once like the sortByAge example in TFA), but I figure it should be friendlier to debuggers (though with all the inlining the experience is not going to be that great either).

IIRC, if LTO is enabled the calls to the less and swap functions can also be inlined even if the generic function is compiled to a separate translation unit. Haven't tried it for the quick sort code above, but I found that out for some other thing I experimented with.




Did you try inline as well? Why are they not as flexible as the macro? Couldn't the swap not also process two arrays?


The function is already inlined because the implementation is visible. You can check the assembly, it has no calls to the compare and swap functions.

I guess you're right, since the function accepts a void pointer, it could also point to an array of two array pointers. So yes, it can also process two arrays.




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

Search: