faster implementation

This is like colomon++'s version takes from https://gist.github.com/colomon/c2d2c1940b92d8d9df01
but improved to be about 20% faster.
That is still slow compared to other languages, but it should improve when the MATCH deopts shown
here are gone: http://www.harmonyware.com/files/profile-1409750803.51175.html#osrdeopt