2016-07-22 - mapping entries

some time ago i made a proposal for an improvement, in one of the projects i participate in. internals aside, the main concerns i received were about the performance of doing dynamic addressing, instead of static one, used currently. the problem boils down to select 8-bit channel number, based on 16-bit address. current solution assumed certain bits of the address are to be used to construct channel names. for instance first nibbles of each bytes, so that address 0x1234 would be mapped to channel 0x24.

having dynamic addressing allows more flexible configurations, code that is simpler to maintain and decoupling of elements addressing from their physical location. it looks like something worth while… so how about this performance? well – first of all, we're talking about message sending, thus task is I/O-bound, not CPU-bound… but still – can this be done efficiently?

from logical point of view, what we need to do is to provide a mapping from uint16 to uint8. there are different approaches that can be taken, to achieve that. each of them is some balance between time, memory and complexity. selecting “the solution” obviously must be based on measurements, since for different input data, different algorithms might win. however since there were so many questions about performance, i decided to make a list of possible approaches to try out. here's what i came up with

…and so we have 9 different approaches within 10 minutes, or so. plenty to choose from. the best one to be selected based on actual, measurements data, from observed load. the nice thing is that most of them are trivial to implement. in fact most of them is already available as part of STL/boost and other libraries. more options are waiting on the internet, to be found. ;) it's quite a fun! :)

1)
except for LUT, of course