background image

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,场景需求不同,导致技术的应用有所差异。
 
PHP 试用 MessagePack 
 
It's like JSON. but fast 

and

 small. 

 
这句吸引了我,去瞧了下。

 

 
官网:

http:

//msgpack.org 

 
官方的安装方法忽悠人,

msgpack 目录下根本没 php 目录...只看到 csharp,erlang,go,java,ruby

等目录。