MessagePack介绍

1,今天在hacknews上看到很多人对messagepack的争论。首先了解什么是MessagePack:MessagePack is a binary-based efficient object serialization library. It enables to exchange structured objects between many languages like JSON. But unlike JSON, it is very fast and small.

2,MessagePack的主要用途,作者解释说有两大用途:一是Space-efficient storage for Memcache entries (Pinterest),节省空间类型的mamcache应用;另一个是用于RPC传输, This use case is fairly close to my original intent. When one is designing an RPC system, one of the first tasks is to specify and implement a communication protocol. This process can get pretty hairy as you need to worry about a lot of low-level issues like Endian-ness. By using MessagePack, one can skip designing and implementing a communication protocol entirely and accelerate development.

3,争议的地方是MessagePack的benchmark说,他比protocolBuffer,Json快很多倍。但是有人不相信,做个javasript下的测试(json与messagePack)。发现MessagePack仅是压缩后的数据比json少10%左右,而压缩和解压时间则和json的解析器比起来要费时很多。

4,“MsgPack vs. JSON: Cut your client-server exchange traffic by 50% with one line of code”这篇文章使用了messagePack做服务器的优化,降低服务器的数据量,更加合理的利用带宽。作者强调了他们宁愿浪费客户端的0.5ms—1ms,但是服务器使用ruby的MessagePack解析器,效率能够比JSON快5倍。

The difference to JSON is, that MsgPack is binary-based - this gives the possibility to make the exchanged data a) smaller and use less bytes, I guess we all know the advantages of that, however there is an even bigger advantage: b) It is faster to parse and encode, having a parser parse 40 bytes takes about twice as long as parsing 20 bytes. 

myJSONString = JSON.stringify(myObject);

myObject = JSON.parse(myJSONString);

var myByteArray = msgpack.pack(myObject);

myObject = msgpack.unpack(myByteArray);

 

MessagePack作者也认为MessagePack may not be the best choice for client-side serialization as described by the blog author.引用2的作者有点小悲剧。

5,BSon是Json的二进制形式,但是与JSon有语法不兼容的地方。但是MessagePack保证语义上能够做到一致。

6,场景需求不同,导致技术的应用有所差异。

引用:

1,MessagePack官方网站

2,MsgPack vs. JSON: Cut your client-server exchange traffic by 50% with one line of code

HN评论地址:http://news.ycombinator.com/item?id=4090831

3,My thoughts on MessagePack

HN评论地址:http://news.ycombinator.com/item?id=4092969

JS下MessagePack与JSON性能对比

HN评论地址:http://news.ycombinator.com/item?id=4091051

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

PHP API:

扩展下载:https://pecl.php.net/package/msgpack

 

$/path/to/phpize
$./configure 
$make && make install

 

 

example:

 

<?php
$data = array(0=>1,1=>2,2=>3);
$msg = msgpack_pack($data);
$data = msgpack_unpack($msg);
?>

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值