msgpackc packs and unpacks MessagePack on
MicroPython tens of times faster than pure Python,
using a fraction of the heap. It is written in C and is a drop-in replacement
for Peter Hinch's umsgpack:
same API, options, exceptions and bytes on the wire.
On a board running MicroPython v1.29 from micropython.org, one command from your computer (here for an ESP32; the table below has the other boards):
mpremote mip install https://github.com/Tangerino/micropython-msgpackc/releases/latest/download/msgpackc-xtensawin.jsonIt copies one file, msgpackc.mpy, to the board. Nothing is compiled and the
firmware is not touched.
import msgpackc
data = msgpackc.dumps({"id": "sensor-01", "temp": 21.5, "ok": True, "raw": b"\x01\x02"})
print(len(data), msgpackc.loads(data))36 {'id': 'sensor-01', 'temp': 21.5, 'ok': True, 'raw': b'\x01\x02'}
(40 bytes on a firmware with double precision floats.)
MessagePack is like JSON, but binary: smaller messages, a bytes type, no
text to parse, and a library in every language to read it.
docs/USAGE.md has the API, with examples: streams,
extension types, incremental and asyncio decoding.
Coming from umsgpack? Replace import umsgpack with
import msgpackc as umsgpack, or see
Moving from umsgpack.
Time to pack and unpack a list of 20 telemetry records (1 KiB), and heap allocated, per call, measured on boards; built into the firmware unless the row says native module. Every payload and board, and the method, are in docs/BENCHMARKS.md.
| msgpackc dumps | umsgpack dumps | msgpackc loads | umsgpack loads | |||
|---|---|---|---|---|---|---|
| ESP32, internal RAM | 1204 µs, 1040 B | 147757 µs, 60928 B | 122.7x faster | 3833 µs, 3296 B | 97049 µs, 19680 B | 25.3x faster |
| ESP32, PSRAM | 2059 µs, 1040 B | 169922 µs, 60928 B | 82.5x faster | 5829 µs, 3296 B | 118470 µs, 19680 B | 20.3x faster |
| ESP32, PSRAM, native module | 3444 µs, 1680 B | 207946 µs, 60928 B | 60.4x faster | 8440 µs, 3296 B | 105776 µs, 19680 B | 12.5x faster |
| ESP32-P4 | 323 µs, 1040 B | 24668 µs, 60928 B | 76.3x faster | 791 µs, 3296 B | 17104 µs, 19680 B | 21.6x faster |
MicroPython v1.29. The native module is one file per architecture:
| Your board | mip install .../msgpackc-<arch>.json |
Run on |
|---|---|---|
| ESP32, ESP32-S2, ESP32-S3 | xtensawin |
an ESP32 board |
| ESP32-C3, ESP32-C6, ESP32-P4 | rv32imc |
an ESP32-P4 board, emulator |
| Raspberry Pi Pico (RP2040), Cortex-M0/M0+ | armv6m |
emulator only |
| Cortex-M3 | armv7m |
emulator only |
| Cortex-M4/M7, single precision firmware (STM32F4, STM32L4...) | armv7emsp |
emulator only |
| Cortex-M7, double precision firmware (STM32F7, STM32H7...) | armv7emdp |
emulator only |
"Run on" is where the test suites of version 1.1.0 ran; "emulator" is MicroPython's QEMU port, in CI. natmod/README.md has the details, and what the native module costs against the firmware build.
Compiled into MicroPython, msgpackc is faster (see the table above) and
takes about 12 KiB of flash (15 KiB on RISC-V chips) and reserves no RAM:
only the calls allocate, from the heap.
-
Ready-made firmware for ESP32 (with and without PSRAM), S3, C3, C6 and P4 is in each release, with the
esptool.pycommands. Writing a firmware erases the board. The ESP32 and ESP32-P4 (early revisions) files were run on boards; the others are built only. -
Your own firmware build, any port: add the module to it.
make USER_C_MODULES=/path/to/micropython-msgpackc/cmodules/micropython.cmake # esp32, rp2 make USER_C_MODULES=/path/to/micropython-msgpackc/cmodules # unix, stm32, ...
-
Never built MicroPython? docs/INSTALL.md goes step by step from a fresh computer to a board running
msgpackc. The first steps need no board: they build MicroPython for your computer, with the module.git clone --recursive https://github.com/Tangerino/micropython-msgpackc.git cd micropython-msgpackc make setup make unix build/unix-standard/micropython -c "import msgpackc; print(msgpackc.dumps([1, 2, 3]))"
dumps,dump,loads,load, with the options of umsgpack (force_float_precision,use_tuple,use_ordered_dict,allow_invalid_utf8).- Extension types with the same
@ext_serializabledecorator, for your classes and for built-in types such assetorcomplex; umsgpack'smpk_*.pyextension modules work unchanged. - Streams:
loadreads exactly one object from a file, socket or UART;dumpwrites it in one call. - Incremental decoding with
Unpacker, for data that arrives in pieces, and asyncio decoding withALoader. - Safe on untrusted data: corrupt or hostile input raises an exception; it never crashes the board nor allocates memory it does not need. Fuzzed under AddressSanitizer and UndefinedBehaviorSanitizer.
- Conforms to the cross-language msgpack-test-suite and interoperates with msgpack-python.
| docs/USAGE.md | the API, with examples, and the differences from umsgpack |
| natmod/README.md | the native module: files, install, limits |
| docs/INSTALL.md | building and flashing a firmware, for beginners |
| docs/TESTING.md | how msgpackc is tested, and the results |
| docs/BENCHMARKS.md | speed and memory, every payload and board |
| docs/HARDWARE_TESTING.md | testing on a board without losing what is on it |
| CONTRIBUTING.md | how to contribute, and the repository layout |
This project stands on the work of others, with gratitude:
- Peter Hinch, for micropython-msgpack
(umsgpack). Its careful API, its extension mechanism and its asyncio
loader are the design
msgpackcfollows, its test suite checksmsgpackctoday, and its behaviour is the reference of our differential tests. If you prefer pure Python, or need to run on a stock firmware, use umsgpack: it is excellent. Peter's many other MicroPython libraries and documents are a treasure for the community. - Vanya A. Sergeev, for u-msgpack-python, from which umsgpack was ported, and WZab, whose first port to MicroPython started it.
- Matt Trentini, for micropython-msgpackc-native,
the port to the native module API that the
.mpybuild grew from, and Jon Nordby, who asked for a module that needs no firmware build. - Damien George and the MicroPython contributors, for MicroPython.
- Yusuke Kawasaki, for the msgpack-test-suite.
- Sadayuki Furuhashi and the MessagePack community, for the format.
msgpackc began as the MessagePack encoder written in C for the firmware of
the coIoTe IoT devices, as an accelerator of their Python packer; it was then
extracted, completed with a decoder, streams and extension types, and made a
general purpose module.
MIT, see LICENSE. umsgpack, msgpack-test-suite and
nativejson-benchmark, used by the tests as git submodules, and the
JSONTestSuite cases copied in third_party/JSONTestSuite, are MIT licensed
by their authors.