Skip to content

Instantly share code, notes, and snippets.

@uupaa
Created January 12, 2015 14:16
Show Gist options
  • Select an option

  • Save uupaa/d33cdc6bbd34a0898ef3 to your computer and use it in GitHub Desktop.

Select an option

Save uupaa/d33cdc6bbd34a0898ef3 to your computer and use it in GitHub Desktop.
MessagePack optimization
function _encodeMessagePackNumber(source, view) {
    // https://github.com/msgpack/msgpack/blob/master/spec.md#int-format-family
    // https://github.com/msgpack/msgpack/blob/master/spec.md#float-format-family
    var type = 0, high = 0, low = 0, u8;
    var buffer = view.buffer, cursor = view.cursor;

    if (source !== source) {
        buffer.set(QUIET_NAN, cursor);
        cursor += 9;
    } else if (source === Infinity) {
        buffer.set(POSITIVE_INFINITY, cursor);
        cursor += 9;
    } else if (Math.floor(source) !== source) { // float or double?
        view["double"][0] = source; // set double value
        buffer[cursor++] = TYPE_FLOAT64;
        u8 = view["byte"];
        buffer.set(BIG_ENDIAN ? u8 : [ u8[7], u8[6], u8[5], u8[4], u8[3], u8[2], u8[1], u8[0] ], cursor); // get byte representation
        cursor += 8;
    } else if (source < 0) { // negative integer
        if (source >= -32) {                // [TYPE_FIX_INT | 0xNNNNN]
            buffer[cursor++] = TYPE_FIX_INT + source + 32;
        } else if (source >= -0x80) {       // [TYPE_INT8, value] ( INT8: -128(-0x80) - 127(0x7f) )
            buffer[cursor++] = TYPE_INT8;
            buffer[cursor++] = source + 0x100;
        } else if (source >= -0x8000) {     // [TYPE_INT16, value x 2] ( INT16: -32768(-0x8000) - 32767(0x7fff) )
         // http://gyazo.com/67d91c7783c33c3e95d9bca5320f0817.png
         // buffer.set([TYPE_INT16, source >>  8, source], cursor);
         // cursor += 3;
         // http://gyazo.com/6e348112a7b3e5abb6d601e76e700b92.png
            buffer[cursor++] = TYPE_INT16;
            buffer[cursor++] = source >>  8;
            buffer[cursor++] = source;
        } else if (source >= -0x80000000) { // [TYPE_INT32, value x 4]
         // http://gyazo.com/fdd442197944213234254edfc9544468.png
         // buffer.set([TYPE_INT32, source >> 24, source >> 16, source >>  8, source], cursor);
         // cursor += 5;
         // http://gyazo.com/81014bcc18892cceda43301c9a0ba0c3.png
            buffer[cursor++] = TYPE_INT32;
            buffer[cursor++] = source >> 24;
            buffer[cursor++] = source >> 16;
            buffer[cursor++] = source >>  8;
            buffer[cursor++] = source;
        } else {                            // [TYPE_INT64, value x 8]
            high = Math.floor(source / 0x100000000);
            low  = source & 0xffffffff;
         // http://gyazo.com/4aeba7e3bc5c33dd07225e3eaedda098.png
         // buffer.set([TYPE_INT64, high >> 24, high >> 16, high >>  8, high,
         //                         low  >> 24, low  >> 16, low  >>  8, low], cursor);
         // cursor += 9;
         // http://gyazo.com/66442ebc511d2a87b045cacc74e69df9.png
            buffer[cursor++] = TYPE_INT64;
            buffer[cursor++] = high >> 24;
            buffer[cursor++] = high >> 16;
            buffer[cursor++] = high >>  8;
            buffer[cursor++] = high;
            buffer[cursor++] = low  >> 24;
            buffer[cursor++] = low  >> 16;
            buffer[cursor++] = low  >>  8;
            buffer[cursor++] = low;
        }
    } else { // positive integer
        if (source < 0x80) {                // [TYPE_FIX_UINT]
            buffer[cursor++] = TYPE_FIX_UINT + source;
        } else  if (source < 0x100) {       // [TYPE_UINT8, value]
            buffer[cursor++] = TYPE_UINT8;
            buffer[cursor++] = source;
        } else if (source < 0x10000) {      // [TYPE_UINT16, value x 2]
         // http://gyazo.com/9a5409ae7739514b93b2ec50f4eb9d64.png
         // buffer.set([TYPE_UINT16, source >>  8, source], cursor);
         // cursor += 3;
         // http://gyazo.com/6182e2e32de87de49441f02a59fceae3.png
            buffer[cursor++] = TYPE_UINT16;
            buffer[cursor++] = source >>  8;
            buffer[cursor++] = source;
        } else if (source < 0x100000000) {  // [TYPE_UINT32, value x 4]
         // http://gyazo.com/9ef8d93ff8f7cd267352287c971bd14f.png
         // buffer.set([TYPE_UINT32, source >> 24, source >> 16,
         //                          source >>  8, source], cursor);
         // cursor += 5;
         // http://gyazo.com/aaee3eb913047d8f092b4d0b33f8debd.png
            buffer[cursor++] = TYPE_UINT32;
            buffer[cursor++] = source >> 24;
            buffer[cursor++] = source >> 16;
            buffer[cursor++] = source >>  8;
            buffer[cursor++] = source;
        } else {                            // [TYPE_UINT64, value x 8]
            high = Math.floor(source / 0x100000000);
            low  = source & 0xffffffff;
         // http://gyazo.com/072027a8f917ff39d6670af21fa7306b.png
         // buffer.set([TYPE_UINT64, high >> 24, high >> 16, high >>  8, high,
         //                          low  >> 24, low  >> 16, low  >>  8, low], cursor);
         // cursor += 9;
         // http://gyazo.com/5d411011791dd15beb4481d2d0f8d246.png
            buffer[cursor++] = TYPE_UINT64;
            buffer[cursor++] = high >> 24;
            buffer[cursor++] = high >> 16;
            buffer[cursor++] = high >>  8;
            buffer[cursor++] = high;
            buffer[cursor++] = low  >> 24;
            buffer[cursor++] = low  >> 16;
            buffer[cursor++] = low  >>  8;
            buffer[cursor++] = low;
        }
    }
    view.cursor = cursor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment