Skip to content

About

Fast MessagePack for MicroPython, in C: a drop-in replacement for Peter Hinch's umsgpack, up to 120x faster dumps and 25x faster loads on ESP32. One mip command installs it, no firmware build. msgpack.org[MicroPython]

Topics

Resources

Contributing

Security policy

Stars

8 stars

Watchers

0 watching

Forks

Repository files navigation

msgpackc — fast MessagePack for MicroPython, in C

CI Release MicroPython ESP32 | S3 | C3 | C6 | P4 License: MIT

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.

Install

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.json

It copies one file, msgpackc.mpy, to the board. Nothing is compiled and the firmware is not touched.

Use

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.

Speed

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

Compatibility

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.

Built into the firmware

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.py commands. 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]))"

Features

  • 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_serializable decorator, for your classes and for built-in types such as set or complex; umsgpack's mpk_*.py extension modules work unchanged.
  • Streams: load reads exactly one object from a file, socket or UART; dump writes it in one call.
  • Incremental decoding with Unpacker, for data that arrives in pieces, and asyncio decoding with ALoader.
  • 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.

Documentation

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

Acknowledgements

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 msgpackc follows, its test suite checks msgpackc today, 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 .mpy build 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.

License

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.

About

Fast MessagePack for MicroPython, in C: a drop-in replacement for Peter Hinch's umsgpack, up to 120x faster dumps and 25x faster loads on ESP32. One mip command installs it, no firmware build. msgpack.org[MicroPython]

Topics

Resources

Contributing

Security policy

Stars

8 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages