A new Python library called fastconstmap promises to dramatically reduce memory consumption for mapping strings to integers. Typically, a Python dictionary with one million keys can use over 100 bytes per key, but fastconstmap achieves just 9 bytes per key—a staggering 91% reduction.
The library, published by open-source developer Daniel Lemire, provides a constant-time mapping API similar to a standard dictionary but optimized for memory efficiency. Usage is straightforward: pass a regular dictionary to ConstMap and enjoy the same __getitem__ and get_many methods. In addition to memory savings, it can be up to twice as fast as Python's built-in dictionary for certain workloads.
fastconstmap also supports serialization and deserialization, making it easy to save and reuse mappings across sessions. The library is available on PyPI, and the source code is hosted on GitHub.
For developers working with large-scale string-to-integer mappings—common in machine learning, data processing, and configuration management—this tool could be a game-changer.