Premature Optimization is Fun Sometimes

59 points by invlpg


jrandomhacker

I think the day I no longer find exercises like this one fun to think about is the day I need to get out of programming.

jbauer

Premature optimisation is fun all the time!

It's dealing with the consequences afterwards when you've learned why that optimisation was premature that usually isn't fun...

chrismorgan

I’m a bit confused by the 43 bits for timestamp: it sounds to me like 24 bits should be enough.

With the talk of a 512-element ring buffer, I presume it’s sending a new ping every two seconds, and tracking the most recent 17 minutes and 4 seconds of pings.

So, step one: use delta encoding relative to an ideal timer/sequence. You can easily determine when a packet should be sent based on a set last-sent time (which increments by two seconds) and the index in the ring buffer. Then, write whether it was sent exactly on time, 0.1ms late, 2.3ms late, &c.

Then, for time elapsed, I don’t suppose you need to go beyond 17 minutes and 4 seconds, since the ping will be expired after that; that’s 512 × 2s = 10240000 × 100μs, about 23.3 bits required at that precision. Round up to 24 if you like, though you may still be able to use the remaining invalid bit patterns (~6536216 of them!) for other things.

As a bonus, 24 bits allows you to increase the “sent” precision a lot, to reduce quantisation error—at microsecond precision, a ping can be sent up to 16 seconds late, which should be oodles (right?).

I can’t comment on the performance effects of reducing the sample from 64 bits to 48, whether it would help or hinder. Wouldn’t surprise me if the results were different across x86 and ARM 32- and 64-bit.