kraih/mango — pull request #5 by avkhozov (2013-07-14 12:24:30+00:00)
Title: Improved performance of BSON parser

Hello, Sebastian.

1) I removed the conversion to numbers in the functions ``decode_int32`` and ``decode_int64``. So much so that extra conversion steps that affect performance. (test: https://gist.github.com/avkhozov/5993752). Although in this case, if the document is smaller than the required number of bytes, undef is returned. I believe undef more correct than 0.

2) I deleted the decoding of bytes in the UTF-8 string in a ``_decode_cstring`` function, since the function ``decode`` of the module ``Mojo::Util`` works with checks on the validity of the encoding, which slows down its operation. I believe that cstring is always are ASCII string, not a UTF-8 string. Not every UTF-8 string can be the key in BSON document.

3) I rewrote the implementation of ordered hash with only one internal array of keys, without an array of values. Now remove the item from such hash runs in linear time, which I think is not critical for BSON documents (since they are usually small and the delete operator is not too frequent), but STORE and FETCH operations are faster and the documents themselves take up less memory.

With such changes on some tests, I got a performance gain of 40%. However, this implementation is slower than the official driver about twice.

Thanks.