Fast MUTF-8 encoder & decoder
Low maintenance activity. Last release 5 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install mutf8mutf-8
This package contains simple pure-python as well as C encoders and decoders for the MUTF-8 character encoding.
These days, you'll most likely encounter MUTF-8 when working on files or
protocols related to the JVM. Strings in a Java .class file are encoded using
MUTF-8, strings passed by the JNI, as well as strings exported by the object
serializer.
This library was extracted from Lawu, a Python library for working with JVM class files.
Installation
Prebuilt binary packages are available for most platforms and Python versions. Install the package from PyPi:
pip install mutf8
Usage
Encoding and decoding is simple:
from mutf8 import encode_modified_utf8, decode_modified_utf8
unicode = decode_modified_utf8(byte_like_object)
bytes = encode_modified_utf8(unicode)
This module does not register itself globally as a codec, since importing should be side-effect-free.
Benchmarks
The C extension is dramatically faster than the pure-Python fallback — from ~10× on tiny strings (where per-call overhead dominates) to several hundred times on realistic payloads, and over 1000× when encoding large ASCII.
MUTF-8 Decoding
| Input | C | Python | Speedup |
|---|---|---|---|
| ASCII (16 B) | 81.5 ± 26 ns | 1.04 ± 0.18 µs | 13× |
| ASCII (1 KB) | 185 ± 38 ns | 65.7 ± 8.5 µs | 356× |
| ASCII (64 KB) | 5.58 ± 0.66 µs | 4.35 ± 0.23 ms | 780× |
| CJK (16 B) | 127 ± 15 ns | 1.33 ± 0.15 µs | 10× |
| CJK (1 KB) | 681 ± 117 ns | 99.3 ± 9.4 µs | 146× |
| CJK (64 KB) | 31.8 ± 3.3 µs | 6.28 ± 0.063 ms | 198× |
| Emoji (16 B) | 100 ± 21 ns | 882 ± 122 ns | 9× |
| Emoji (1 KB) |