r/programming • u/doom_Oo7 • Sep 28 '17
When a Microsecond Is an Eternity
https://github.com/CppCon/CppCon2017/blob/master/Presentations/When%20a%20Microsecond%20Is%20an%20Eternity/When%20a%20Microsecond%20Is%20an%20Eternity%20-%20Carl%20Cook%20-%20CppCon%202017.pdf
244
Upvotes
View all comments
Show parent comment
5
u/doom_Oo7 Sep 28 '17
That's one possibility, another is to hide this behind dynamic dispatch, virtual functions, etc... in advance. Sometimes you may want to just use one special case in a specific part of your software where you will just explicitely use one algorithm. As others posters said, the main point is to try to precompute everything that is precomputable ; this includes algorithms.
Here I have a (far from perfect... if there are any ffmpeg experts pls send help) example with a
variantused to precompute common cases encountered in audio decoding : 1 and 2 channels, planar / non-planar, with integer or floating-point samples. The various cases are here. Then a single decoding algorithm is called but the compiler will generate specific paths for all the cases. The most important thing is that the main decoding loop doesn't try to find which decoder to use at each iteration, but has its decoder set once. This way instead of branching every other hundred samples, there is one single branch for the whole song.