From what I've seen in the past, StackOverflow seems to like programming challenges, such as the fast char to string exercise problem which got dozens of responses. This is an optimization challenge: take a very simple function and see if you can come up with a smarter way of doing it.
I've had a function that I've wanted to further optimize for quite some time but I always find that my optimizations have some hole that result in incorrect output--some rare special case in which they fail. But, given the function, I've always figured one should be able to do better than this.
The function takes an input datastream (effectively random bits, from an entropy perspective) and wraps it into a NAL unit. This involves placing escape codes: any byte sequence of 00 00 00, 00 00 01, 00 00 02, or 00 00 03 gets replaced with 00 00 03 XX, where XX is that last byte of the original sequence. As one can guess, these only get placed about 1 in every 4 million bytes of input, given the odds against such a sequence--so this is a challenge where one is searching an enormous amount of data and doing almost nothing to it except in very rare cases. However, because "doing something" involves inserting bytes, it makes things a bit trickier. The current unoptimized code is the following C:
src and dst are pointers to arrays of bytes, and end is the pointer to the end of the input data.
int i_count = 0;
while( src < end )
{
if( i_count == 2 && *src <= 0x03 )
{
*dst++ = 0x03;
i_count = 0;
}
if( *src == 0 )
i_count++;
else
i_count = 0;
*dst++ = *src++;
}
Common input sizes to this function range from roughly between 1000 and 1000000 bytes of data.
Initial ideas of mine include a function which (somehow) quickly searches the input for situations where an escape code is needed, to avoid more complex logic in the vast majority of inputs where escape codes don't need to be placed.