SlideShare a Scribd company logo
MessagePack(msgpack):
A Compact and Fast Serialization Library
Takatoshi Kondo
2015/5/14 1Copyright OGIS-RI 2015
• Taka (Takatoshi Kondo)
– from TOKYO, JAPAN
• OGIS-RI Co., Ltd.
– Developing Pub/Sub IoT Platform using MessagePack
• A committer on the msgpack-c OSS project
• Other experience: Wrote the “Boost.MSM Guide”
– http://redboltz.wikidot.com/boost-msm-guide
2015/5/14 Copyright OGIS-RI 2015 2
About me
What is MessagePack?
2015/5/14 Copyright OGIS-RI 2015 3
• Same as JSON
– Portable
– Contain basic type information
• For example, map, array, string, boolean...
– Composite data structure
• Different from JSON
– Smaller size
– Binary coded
– Can handle binary data without text encoding such as Base64
– Easier to parse, requires less computing power
2015/5/14 Copyright OGIS-RI 2015 4
MessagePack vs. JSON
• Same as JSON
– Portable
– Contain basic type information
• For example, map, array, string, boolean...
– Composite data structure
• Different from JSON
– Smaller size
– Binary coded
– Can handle binary data without text encoding such as Base64
– Easier to parse, requires less computing power
2015/5/14 Copyright OGIS-RI 2015 5
MessagePack vs. JSON
msgpack::object
nil boolean u64 i64 f64
str bin ext array map
*
*
double, float object_kv
key val
MessagePack/Boost.Serialization
2015/5/14 6Copyright OGIS-RI 2015
• The goals of the msgpack and the
Boost.Serialization are different.
– msgpack: Interexchange the data with
different programming languages.
– Boost.Serialization: Serialize every data and relations.
e.g.) shared_ptr
• msgpack can be used as a portable binary
archive of the Boost.Serialization.
– https://github.com/redboltz/rui/tree/support_boost_1_57_0
• Forked from Norihisa Fujita’s implementation
C++
msgpack
ruby
python
shared_ptr
shared_ptr
shared_ptr
42
42
Boost.Serialization
format
Supported Programming Languages
2015/5/14 Copyright OGIS-RI 2015 7
Pack/Unpack
2015/5/14 8
#include <tuple>
#include <string>
#include <sstream>
#include <msgpack.hpp>
int main() {
auto t1 = std::make_tuple("hello", true, 42, 12.3);
std::stringstream ss;
msgpack::pack(ss, t1);
msgpack::unpacked unpacked
= msgpack::unpack(ss.str().data(), ss.str().size());
msgpack::object obj = unpacked.get();
auto t2 = obj.as<std::tuple<std::string, bool, int, double>>();
assert(t1 == t2);
}
You can use any types that have
the member function
write(const char*, std::size_t);
Copyright OGIS-RI 2015
packing
unpacking
convering
Stream Deserialization
2015/5/14 9
class unpacker {
public:
// Constructor is omitted in this presentation
void reserve_buffer(std::size_t size);
char* buffer();
void buffer_consumed(std::size_t size);
bool next(unpacked& result);
};
Copyright OGIS-RI 2015
Stream Deserialization
2015/5/14 10Copyright OGIS-RI 2015
std::size_t const try_read_size = 100;
msgpack::unpacker unp;
while (/* block until input becomes readable */) {
unp.reserve_buffer(try_read_size);
std::size_t actual_read_size = input.readsome(
unp.buffer(), try_read_size);
unp.buffer_consumed(actual_read_size);
msgpack::unpacked result;
// MessagePack data loop
while(unp.next(result)) {
msgpack::object obj(result.get());
}
}
Stream Deserialization
2015/5/14 11Copyright OGIS-RI 2015
std::size_t const try_read_size = 100;
msgpack::unpacker unp;
while (/* block until input becomes readable */) {
unp.reserve_buffer(try_read_size);
std::size_t actual_read_size = input.readsome(
unp.buffer(), try_read_size);
unp.buffer_consumed(actual_read_size);
msgpack::unpacked result;
// MessagePack data loop
while(unp.next(result)) {
msgpack::object obj(result.get());
}
}
The size may decided by receive performance,
transmit layer's protocol and so on.
Stream Deserialization
2015/5/14 12Copyright OGIS-RI 2015
std::size_t const try_read_size = 100;
msgpack::unpacker unp;
while (/* block until input becomes readable */) {
unp.reserve_buffer(try_read_size);
std::size_t actual_read_size = input.readsome(
unp.buffer(), try_read_size);
unp.buffer_consumed(actual_read_size);
msgpack::unpacked result;
// MessagePack data loop
while(unp.next(result)) {
msgpack::object obj(result.get());
}
}
The size may decided by receive performance,
transmit layer's protocol and so on.
unp has at least try_read_size buffer on this point.
Stream Deserialization
2015/5/14 13Copyright OGIS-RI 2015
std::size_t const try_read_size = 100;
msgpack::unpacker unp;
while (/* block until input becomes readable */) {
unp.reserve_buffer(try_read_size);
std::size_t actual_read_size = input.readsome(
unp.buffer(), try_read_size);
unp.buffer_consumed(actual_read_size);
msgpack::unpacked result;
// MessagePack data loop
while(unp.next(result)) {
msgpack::object obj(result.get());
}
}
The size may decided by receive performance,
transmit layer's protocol and so on.
unp has at least try_read_size buffer on this point.
input is a kind of I/O library object.
read message to msgpack::unpacker's internal buffer directly.
Stream Deserialization
2015/5/14 14Copyright OGIS-RI 2015
std::size_t const try_read_size = 100;
msgpack::unpacker unp;
while (/* block until input becomes readable */) {
unp.reserve_buffer(try_read_size);
std::size_t actual_read_size = input.readsome(
unp.buffer(), try_read_size);
unp.buffer_consumed(actual_read_size);
msgpack::unpacked result;
// MessagePack data loop
while(unp.next(result)) {
msgpack::object obj(result.get());
}
}
The size may decided by receive performance,
transmit layer's protocol and so on.
unp has at least try_read_size buffer on this point.
input is a kind of I/O library object.
read message to msgpack::unpacker's internal buffer directly.
notify msgpack::unpacker actual consumed size.
Stream Deserialization
2015/5/14 15Copyright OGIS-RI 2015
std::size_t const try_read_size = 100;
msgpack::unpacker unp;
while (/* block until input becomes readable */) {
unp.reserve_buffer(try_read_size);
std::size_t actual_read_size = input.readsome(
unp.buffer(), try_read_size);
unp.buffer_consumed(actual_read_size);
msgpack::unpacked result;
// MessagePack data loop
while(unp.next(result)) {
msgpack::object obj(result.get());
}
}
The size may decided by receive performance,
transmit layer's protocol and so on.
unp has at least try_read_size buffer on this point.
input is a kind of I/O library object.
read message to msgpack::unpacker's internal buffer directly.
notify msgpack::unpacker actual consumed size.
Use obj. convert to C++ types
All complete msgpack message is processed at this point,
then continue to read addtional message.
Zero-Copy Deserialization
2015/5/14 16Copyright OGIS-RI 2015
unpacker buffer int string bin
header payload header payload
array
client memory
msgpack memory
Zero copy deserialization
2015/5/14 17Copyright OGIS-RI 2015
zone
chunk_list
finalizer_array
object
(array)
object
(int)
object
(string)
object
(bin)
via.array.ptr
chunk
unpacker buffer int string bin
header payload header payload
via.str.ptr via.bin.ptr
array
(msgpack format)
client memory
msgpack memory
managed using reference counter
unpacked
copy or reference is selectable
Zero copy deserialization
2015/5/14 18Copyright OGIS-RI 2015
zone
chunk_list
finalizer_array
object
(array)
object
(int)
object
(string)
object
(bin)
via.array.ptr
chunk
unpacker buffer int string bin
header payload header payload
via.str.ptr via.bin.ptr
array
(msgpack format)
client memory
msgpack memory
managed using reference counter
unpacked
convert
Convert to any types that adapts MessagePack
std::tuple<int, boost::string_ref, std::vector<char>>
copyref
copy or reference is selectable
MessagePack Adaptors
2015/5/14 19Copyright OGIS-RI 2015
https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_adaptor
C++ type msgpack::object type
bool bool
char* str
std::deque array
char positive/negative integer
signed ints *1 positive/negative integer
unsigned ints *2 positive integer
std::list array
std::map array
std::pair array
std::set array
std::string str
std::vector array
std::vector<char> bin
*1 signed ints signed char, signed short, signed int, signed long, signed long long
*2 unsigned ints unsigned char, unsigned short, unsigned int, signed long, signed long long
C++11 type
msgpack::
object type
std::array array
std::array<char> bin
std::forward_list array
std::tuple array
std::array array
std::unordered_map array
std::unordered_set array
boost type
msgpack::
object type
boost::optional<T> T
boost::string_ref str
MessagePack Adaptors
2015/5/14 20Copyright OGIS-RI 2015
https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_adaptor
#include <msgpack.hpp>
struct your_class : base1, base2 {
int a;
std::string b;
// You can choose any order.
// It is represented to the msgpack array elements order.
MSGPACK_DEFINE(a, b, MSGPACK_BASE(base1), MSGPACK_BASE(base2));
};
• If you have questions, feel free to contact me :)
• Takatoshi Kondo
– redboltz@gmail.com
– twitter: redboltz
• Resources
– MessagePack
• http://msgpack.org/
– msgpack-c
• https://github.com/msgpack/msgpack-c
– msgpack-c Documents
• https://github.com/msgpack/msgpack-c/wiki
– msgpack-c stream unpack algorithm
• A little old but concept is the same
• http://www.slideshare.net/taka111/msgpackc
2015/5/14 Copyright OGIS-RI 2015 21
Thank you
Extra Slides
2015/5/14 Copyright OGIS-RI 2015 22
Format
name
first byte (in binary) first byte (in hex)
positive
fixint
0xxxxxxx 0x00 - 0x7f
fixmap 1000xxxx 0x80 - 0x8f
fixarray 1001xxxx 0x90 - 0x9f
fixstr 101xxxxx 0xa0 - 0xbf
nil 11000000 0xc0
(never
used)
11000001 0xc1
false 11000010 0xc2
true 11000011 0xc3
bin 8 11000100 0xc4
bin 16 11000101 0xc5
bin 32 11000110 0xc6
ext 8 11000111 0xc7
ext 16 11001000 0xc8
ext 32 11001001 0xc9
float 32 11001010 0xca
float 64 11001011 0xcb
uint 8 11001100 0xcc
uint 16 11001101 0xcd
uint 32 11001110 0xce
uint 64 11001111 0xcf2015/5/14 Copyright OGIS-RI 2015 23
MessagePack Formats
https://github.com/msgpack/msgpack/blob/master/spec.md
Format
name
first byte (in binary) first byte (in hex)
int 8 11010000 0xd0
int 16 11010001 0xd1
int 32 11010010 0xd2
int 64 11010011 0xd3
fixext 1 11010100 0xd4
fixext 2 11010101 0xd5
fixext 4 11010110 0xd6
fixext 8 11010111 0xd7
fixext 16 11011000 0xd8
str 8 11011001 0xd9
str 16 11011010 0xda
str 32 11011011 0xdb
array 16 11011100 0xdc
array 32 11011101 0xdd
map 16 11011110 0xde
map 32 11011111 0xdf
negative
fixint
111xxxxx 0xe0 - 0xff
Format name first byte (in binary) first byte (in hex)
positive fixint 0xxxxxxx 0x00 - 0x7f
fixmap 1000xxxx 0x80 - 0x8f
fixarray 1001xxxx 0x90 - 0x9f
fixstr 101xxxxx 0xa0 - 0xbf
nil 11000000 0xc0
(never used) 11000001 0xc1
false 11000010 0xc2
true 11000011 0xc3
bin 8 11000100 0xc4
bin 16 11000101 0xc5
bin 32 11000110 0xc6
2015/5/14 Copyright OGIS-RI 2015 24
MessagePack Formats
https://github.com/msgpack/msgpack/blob/master/spec.md
2015/5/14 Copyright OGIS-RI 2015 25
MessagePack Format
https://github.com/msgpack/msgpack/blob/master/spec.md#int-format-family
2015/5/14 Copyright OGIS-RI 2015 26
MessagepPack format
What is MessagePack?
2015/5/14 Copyright OGIS-RI 2015 27

More Related Content

PDF
知っているようで知らないPAMのお話
PDF
136 covek s bicem
PPTX
開発環境の認証を改善して Redmineを社内標準にした話
PPT
「Android案件できます」って言ったら、ヒドい目にあった話
PDF
Mutualized Oblivious DNS (μODNS): Hiding a tree in the wild forest
PDF
はじめてのふりかえり
PPTX
よわよわPCによる姿勢推定 -PoseNet-
PDF
IIJmio meeting 28 5G SAについて
知っているようで知らないPAMのお話
136 covek s bicem
開発環境の認証を改善して Redmineを社内標準にした話
「Android案件できます」って言ったら、ヒドい目にあった話
Mutualized Oblivious DNS (μODNS): Hiding a tree in the wild forest
はじめてのふりかえり
よわよわPCによる姿勢推定 -PoseNet-
IIJmio meeting 28 5G SAについて

What's hot (20)

PDF
Rust-DPDK
PDF
[B11] 基礎から知るSSD(いまさら聞けないSSDの基本) by Hironobu Asano
PDF
nginxの紹介
PDF
並列クエリを実行するPostgreSQLのアーキテクチャ
PDF
About GStreamer 1.0 application development for beginners
PDF
Hybrid Public Key Encryption (HPKE)
PDF
IIJmio meeting 16 スマートフォンがつながる仕組み
PPTX
Phantom4 RTKで、後処理キネマティック(PPK)をする方法
PPTX
PostgreSQL開発コミュニティに参加しよう! ~2022年版~(Open Source Conference 2022 Online/Kyoto 発...
PDF
PyQtではじめるGUIプログラミング
PDF
SDCCオープンネットワークのご紹介【2021/01版】
PDF
PostgreSQLのリカバリ超入門(もしくはWAL、CHECKPOINT、オンラインバックアップの仕組み)
PDF
オススメのJavaログ管理手法 ~コンテナ編~(Open Source Conference 2022 Online/Spring 発表資料)
PDF
ソフトウェアでのパケット処理あれこれ〜何故我々はロードバランサを自作するに至ったのか〜
PDF
シスコ装置を使い倒す!組込み機能による可視化からセキュリティ強化
PDF
IIJmio meeting 14 IIJmioタイプAとSIMフリー端末について
PDF
BGP Session Culling - BGPに優しいIXのメンテナンスを目指して
PDF
実践 Git - 低レベルに知る Git
PDF
IIJmio meeting 7 MVNOとSIMフリー端末の問題について
Rust-DPDK
[B11] 基礎から知るSSD(いまさら聞けないSSDの基本) by Hironobu Asano
nginxの紹介
並列クエリを実行するPostgreSQLのアーキテクチャ
About GStreamer 1.0 application development for beginners
Hybrid Public Key Encryption (HPKE)
IIJmio meeting 16 スマートフォンがつながる仕組み
Phantom4 RTKで、後処理キネマティック(PPK)をする方法
PostgreSQL開発コミュニティに参加しよう! ~2022年版~(Open Source Conference 2022 Online/Kyoto 発...
PyQtではじめるGUIプログラミング
SDCCオープンネットワークのご紹介【2021/01版】
PostgreSQLのリカバリ超入門(もしくはWAL、CHECKPOINT、オンラインバックアップの仕組み)
オススメのJavaログ管理手法 ~コンテナ編~(Open Source Conference 2022 Online/Spring 発表資料)
ソフトウェアでのパケット処理あれこれ〜何故我々はロードバランサを自作するに至ったのか〜
シスコ装置を使い倒す!組込み機能による可視化からセキュリティ強化
IIJmio meeting 14 IIJmioタイプAとSIMフリー端末について
BGP Session Culling - BGPに優しいIXのメンテナンスを目指して
実践 Git - 低レベルに知る Git
IIJmio meeting 7 MVNOとSIMフリー端末の問題について
Ad

Similar to MessagePack(msgpack): Compact and Fast Serialization Library (20)

PPTX
Boost.Python: C++ and Python Integration
PDF
Solid C++ by Example
PDF
Multithreaded sockets c++11
DOCX
Final Project SkeletonCipherClient.javaFinal Project SkeletonC.docx
PPTX
Unpack mechanism of the msgpack-c
PPT
Os Reindersfinal
PPT
Os Reindersfinal
PDF
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
PPTX
The TCP/IP Stack in the Linux Kernel
PPTX
Synchronization
PPT
Hooking signals and dumping the callstack
PPT
typemap in Perl/XS
PDF
Pascal script maxbox_ekon_14_2
PDF
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
PDF
Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2 「エッジAIモダン計測制御の世界」オ...
PPTX
Why learn Internals?
PDF
Start Wrap Episode 11: A New Rope
PDF
OSA Con 2022: Streaming Data Made Easy
PDF
OSA Con 2022 - Streaming Data Made Easy - Tim Spann & David Kjerrumgaard - St...
PDF
Golang
Boost.Python: C++ and Python Integration
Solid C++ by Example
Multithreaded sockets c++11
Final Project SkeletonCipherClient.javaFinal Project SkeletonC.docx
Unpack mechanism of the msgpack-c
Os Reindersfinal
Os Reindersfinal
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
The TCP/IP Stack in the Linux Kernel
Synchronization
Hooking signals and dumping the callstack
typemap in Perl/XS
Pascal script maxbox_ekon_14_2
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2 「エッジAIモダン計測制御の世界」オ...
Why learn Internals?
Start Wrap Episode 11: A New Rope
OSA Con 2022: Streaming Data Made Easy
OSA Con 2022 - Streaming Data Made Easy - Tim Spann & David Kjerrumgaard - St...
Golang
Ad

More from Takatoshi Kondo (10)

PPTX
CppCon2016 report and Boost.SML
PPTX
Pub/Sub model, msm, and asio
PPTX
Effective Modern C++ study group Item39
PDF
Boost sg msgpack
PPTX
Emcpp0506
PDF
Boostsapporomsmpost 111106070819-phpapp02
PDF
Boostsapporomsmpre 111030054504-phpapp02
PPTX
N3495 inplace realloc
PPTX
N3701 concept lite
PPTX
Aho-Corasick string matching algorithm
CppCon2016 report and Boost.SML
Pub/Sub model, msm, and asio
Effective Modern C++ study group Item39
Boost sg msgpack
Emcpp0506
Boostsapporomsmpost 111106070819-phpapp02
Boostsapporomsmpre 111030054504-phpapp02
N3495 inplace realloc
N3701 concept lite
Aho-Corasick string matching algorithm

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
System and Network Administraation Chapter 3
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
System and Network Administration Chapter 2
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Transform Your Business with a Software ERP System
Nekopoi APK 2025 free lastest update
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Design an Analysis of Algorithms I-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
Operating system designcfffgfgggggggvggggggggg
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
System and Network Administraation Chapter 3
Digital Systems & Binary Numbers (comprehensive )
Computer Software and OS of computer science of grade 11.pptx
System and Network Administration Chapter 2
Navsoft: AI-Powered Business Solutions & Custom Software Development
Softaken Excel to vCard Converter Software.pdf
Understanding Forklifts - TECH EHS Solution
VVF-Customer-Presentation2025-Ver1.9.pptx
Transform Your Business with a Software ERP System

MessagePack(msgpack): Compact and Fast Serialization Library

  • 1. MessagePack(msgpack): A Compact and Fast Serialization Library Takatoshi Kondo 2015/5/14 1Copyright OGIS-RI 2015
  • 2. • Taka (Takatoshi Kondo) – from TOKYO, JAPAN • OGIS-RI Co., Ltd. – Developing Pub/Sub IoT Platform using MessagePack • A committer on the msgpack-c OSS project • Other experience: Wrote the “Boost.MSM Guide” – http://redboltz.wikidot.com/boost-msm-guide 2015/5/14 Copyright OGIS-RI 2015 2 About me
  • 3. What is MessagePack? 2015/5/14 Copyright OGIS-RI 2015 3
  • 4. • Same as JSON – Portable – Contain basic type information • For example, map, array, string, boolean... – Composite data structure • Different from JSON – Smaller size – Binary coded – Can handle binary data without text encoding such as Base64 – Easier to parse, requires less computing power 2015/5/14 Copyright OGIS-RI 2015 4 MessagePack vs. JSON
  • 5. • Same as JSON – Portable – Contain basic type information • For example, map, array, string, boolean... – Composite data structure • Different from JSON – Smaller size – Binary coded – Can handle binary data without text encoding such as Base64 – Easier to parse, requires less computing power 2015/5/14 Copyright OGIS-RI 2015 5 MessagePack vs. JSON msgpack::object nil boolean u64 i64 f64 str bin ext array map * * double, float object_kv key val
  • 6. MessagePack/Boost.Serialization 2015/5/14 6Copyright OGIS-RI 2015 • The goals of the msgpack and the Boost.Serialization are different. – msgpack: Interexchange the data with different programming languages. – Boost.Serialization: Serialize every data and relations. e.g.) shared_ptr • msgpack can be used as a portable binary archive of the Boost.Serialization. – https://github.com/redboltz/rui/tree/support_boost_1_57_0 • Forked from Norihisa Fujita’s implementation C++ msgpack ruby python shared_ptr shared_ptr shared_ptr 42 42 Boost.Serialization format
  • 7. Supported Programming Languages 2015/5/14 Copyright OGIS-RI 2015 7
  • 8. Pack/Unpack 2015/5/14 8 #include <tuple> #include <string> #include <sstream> #include <msgpack.hpp> int main() { auto t1 = std::make_tuple("hello", true, 42, 12.3); std::stringstream ss; msgpack::pack(ss, t1); msgpack::unpacked unpacked = msgpack::unpack(ss.str().data(), ss.str().size()); msgpack::object obj = unpacked.get(); auto t2 = obj.as<std::tuple<std::string, bool, int, double>>(); assert(t1 == t2); } You can use any types that have the member function write(const char*, std::size_t); Copyright OGIS-RI 2015 packing unpacking convering
  • 9. Stream Deserialization 2015/5/14 9 class unpacker { public: // Constructor is omitted in this presentation void reserve_buffer(std::size_t size); char* buffer(); void buffer_consumed(std::size_t size); bool next(unpacked& result); }; Copyright OGIS-RI 2015
  • 10. Stream Deserialization 2015/5/14 10Copyright OGIS-RI 2015 std::size_t const try_read_size = 100; msgpack::unpacker unp; while (/* block until input becomes readable */) { unp.reserve_buffer(try_read_size); std::size_t actual_read_size = input.readsome( unp.buffer(), try_read_size); unp.buffer_consumed(actual_read_size); msgpack::unpacked result; // MessagePack data loop while(unp.next(result)) { msgpack::object obj(result.get()); } }
  • 11. Stream Deserialization 2015/5/14 11Copyright OGIS-RI 2015 std::size_t const try_read_size = 100; msgpack::unpacker unp; while (/* block until input becomes readable */) { unp.reserve_buffer(try_read_size); std::size_t actual_read_size = input.readsome( unp.buffer(), try_read_size); unp.buffer_consumed(actual_read_size); msgpack::unpacked result; // MessagePack data loop while(unp.next(result)) { msgpack::object obj(result.get()); } } The size may decided by receive performance, transmit layer's protocol and so on.
  • 12. Stream Deserialization 2015/5/14 12Copyright OGIS-RI 2015 std::size_t const try_read_size = 100; msgpack::unpacker unp; while (/* block until input becomes readable */) { unp.reserve_buffer(try_read_size); std::size_t actual_read_size = input.readsome( unp.buffer(), try_read_size); unp.buffer_consumed(actual_read_size); msgpack::unpacked result; // MessagePack data loop while(unp.next(result)) { msgpack::object obj(result.get()); } } The size may decided by receive performance, transmit layer's protocol and so on. unp has at least try_read_size buffer on this point.
  • 13. Stream Deserialization 2015/5/14 13Copyright OGIS-RI 2015 std::size_t const try_read_size = 100; msgpack::unpacker unp; while (/* block until input becomes readable */) { unp.reserve_buffer(try_read_size); std::size_t actual_read_size = input.readsome( unp.buffer(), try_read_size); unp.buffer_consumed(actual_read_size); msgpack::unpacked result; // MessagePack data loop while(unp.next(result)) { msgpack::object obj(result.get()); } } The size may decided by receive performance, transmit layer's protocol and so on. unp has at least try_read_size buffer on this point. input is a kind of I/O library object. read message to msgpack::unpacker's internal buffer directly.
  • 14. Stream Deserialization 2015/5/14 14Copyright OGIS-RI 2015 std::size_t const try_read_size = 100; msgpack::unpacker unp; while (/* block until input becomes readable */) { unp.reserve_buffer(try_read_size); std::size_t actual_read_size = input.readsome( unp.buffer(), try_read_size); unp.buffer_consumed(actual_read_size); msgpack::unpacked result; // MessagePack data loop while(unp.next(result)) { msgpack::object obj(result.get()); } } The size may decided by receive performance, transmit layer's protocol and so on. unp has at least try_read_size buffer on this point. input is a kind of I/O library object. read message to msgpack::unpacker's internal buffer directly. notify msgpack::unpacker actual consumed size.
  • 15. Stream Deserialization 2015/5/14 15Copyright OGIS-RI 2015 std::size_t const try_read_size = 100; msgpack::unpacker unp; while (/* block until input becomes readable */) { unp.reserve_buffer(try_read_size); std::size_t actual_read_size = input.readsome( unp.buffer(), try_read_size); unp.buffer_consumed(actual_read_size); msgpack::unpacked result; // MessagePack data loop while(unp.next(result)) { msgpack::object obj(result.get()); } } The size may decided by receive performance, transmit layer's protocol and so on. unp has at least try_read_size buffer on this point. input is a kind of I/O library object. read message to msgpack::unpacker's internal buffer directly. notify msgpack::unpacker actual consumed size. Use obj. convert to C++ types All complete msgpack message is processed at this point, then continue to read addtional message.
  • 16. Zero-Copy Deserialization 2015/5/14 16Copyright OGIS-RI 2015 unpacker buffer int string bin header payload header payload array client memory msgpack memory
  • 17. Zero copy deserialization 2015/5/14 17Copyright OGIS-RI 2015 zone chunk_list finalizer_array object (array) object (int) object (string) object (bin) via.array.ptr chunk unpacker buffer int string bin header payload header payload via.str.ptr via.bin.ptr array (msgpack format) client memory msgpack memory managed using reference counter unpacked copy or reference is selectable
  • 18. Zero copy deserialization 2015/5/14 18Copyright OGIS-RI 2015 zone chunk_list finalizer_array object (array) object (int) object (string) object (bin) via.array.ptr chunk unpacker buffer int string bin header payload header payload via.str.ptr via.bin.ptr array (msgpack format) client memory msgpack memory managed using reference counter unpacked convert Convert to any types that adapts MessagePack std::tuple<int, boost::string_ref, std::vector<char>> copyref copy or reference is selectable
  • 19. MessagePack Adaptors 2015/5/14 19Copyright OGIS-RI 2015 https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_adaptor C++ type msgpack::object type bool bool char* str std::deque array char positive/negative integer signed ints *1 positive/negative integer unsigned ints *2 positive integer std::list array std::map array std::pair array std::set array std::string str std::vector array std::vector<char> bin *1 signed ints signed char, signed short, signed int, signed long, signed long long *2 unsigned ints unsigned char, unsigned short, unsigned int, signed long, signed long long C++11 type msgpack:: object type std::array array std::array<char> bin std::forward_list array std::tuple array std::array array std::unordered_map array std::unordered_set array boost type msgpack:: object type boost::optional<T> T boost::string_ref str
  • 20. MessagePack Adaptors 2015/5/14 20Copyright OGIS-RI 2015 https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_adaptor #include <msgpack.hpp> struct your_class : base1, base2 { int a; std::string b; // You can choose any order. // It is represented to the msgpack array elements order. MSGPACK_DEFINE(a, b, MSGPACK_BASE(base1), MSGPACK_BASE(base2)); };
  • 21. • If you have questions, feel free to contact me :) • Takatoshi Kondo – redboltz@gmail.com – twitter: redboltz • Resources – MessagePack • http://msgpack.org/ – msgpack-c • https://github.com/msgpack/msgpack-c – msgpack-c Documents • https://github.com/msgpack/msgpack-c/wiki – msgpack-c stream unpack algorithm • A little old but concept is the same • http://www.slideshare.net/taka111/msgpackc 2015/5/14 Copyright OGIS-RI 2015 21 Thank you
  • 23. Format name first byte (in binary) first byte (in hex) positive fixint 0xxxxxxx 0x00 - 0x7f fixmap 1000xxxx 0x80 - 0x8f fixarray 1001xxxx 0x90 - 0x9f fixstr 101xxxxx 0xa0 - 0xbf nil 11000000 0xc0 (never used) 11000001 0xc1 false 11000010 0xc2 true 11000011 0xc3 bin 8 11000100 0xc4 bin 16 11000101 0xc5 bin 32 11000110 0xc6 ext 8 11000111 0xc7 ext 16 11001000 0xc8 ext 32 11001001 0xc9 float 32 11001010 0xca float 64 11001011 0xcb uint 8 11001100 0xcc uint 16 11001101 0xcd uint 32 11001110 0xce uint 64 11001111 0xcf2015/5/14 Copyright OGIS-RI 2015 23 MessagePack Formats https://github.com/msgpack/msgpack/blob/master/spec.md Format name first byte (in binary) first byte (in hex) int 8 11010000 0xd0 int 16 11010001 0xd1 int 32 11010010 0xd2 int 64 11010011 0xd3 fixext 1 11010100 0xd4 fixext 2 11010101 0xd5 fixext 4 11010110 0xd6 fixext 8 11010111 0xd7 fixext 16 11011000 0xd8 str 8 11011001 0xd9 str 16 11011010 0xda str 32 11011011 0xdb array 16 11011100 0xdc array 32 11011101 0xdd map 16 11011110 0xde map 32 11011111 0xdf negative fixint 111xxxxx 0xe0 - 0xff
  • 24. Format name first byte (in binary) first byte (in hex) positive fixint 0xxxxxxx 0x00 - 0x7f fixmap 1000xxxx 0x80 - 0x8f fixarray 1001xxxx 0x90 - 0x9f fixstr 101xxxxx 0xa0 - 0xbf nil 11000000 0xc0 (never used) 11000001 0xc1 false 11000010 0xc2 true 11000011 0xc3 bin 8 11000100 0xc4 bin 16 11000101 0xc5 bin 32 11000110 0xc6 2015/5/14 Copyright OGIS-RI 2015 24 MessagePack Formats https://github.com/msgpack/msgpack/blob/master/spec.md
  • 25. 2015/5/14 Copyright OGIS-RI 2015 25 MessagePack Format https://github.com/msgpack/msgpack/blob/master/spec.md#int-format-family
  • 26. 2015/5/14 Copyright OGIS-RI 2015 26 MessagepPack format
  • 27. What is MessagePack? 2015/5/14 Copyright OGIS-RI 2015 27