Skip to main content

mdict-mquery

A high-performance Python library for reading and querying MDX/MDD dictionary files.

Forked from the original mdict-query (2014, unmaintained) and rebuilt for modern Python with aggressive performance optimizations.

Features

  • SQLite-backed lookups — persistent, read-optimized SQLite connection for on-demand record lookups (immutable mode, mmap, no journaling)
  • Pickle cache — pre-built key structures cached to disk; subsequent loads bypass SQLite key queries (~20x faster init vs baseline)
  • Sorted key set — _SortedKeySet wrapper over sorted list using bisect for O(log n) membership testing with zero memory duplication
  • Kanji index — CJK reverse index for bracket-notation lookups
  • Block decompression cache — LRU cache for decompressed record blocks (256 blocks)
  • Result cache — LRU cache for lookup results (8,192 entries)
  • Persistent file handle — single file handle with thread lock (no open/close per lookup)
  • Thread-safe — safe for concurrent access from multiple threads
  • Zero RAM for record data — record positions stay in SQLite (B-tree indexed), not loaded into memory
  • Python 3 only — no legacy compatibility code

Installation

pip install mdict-mquery

Usage

from mdict_mquery import IndexBuilder

# Load a dictionary
ib = IndexBuilder("path/to/dictionary.mdx")

# Look up a word (SQLite B-tree + block cache + result cache)
results = ib.mdx_lookup("食べる")
for entry in results:
    print(entry)

# Key collections
sorted_keys = ib.sorted_keys  # sorted list — binary search ready
kanji_idx = ib.kanji_index    # CJK char → list of keys with that kanji in 【...】

# Legacy API still works
keys_list = ib.get_mdx_keys()
print(f"{len(keys_list):,} entries")

Benchmarks

Tested with 22 Japanese dictionaries (8.87 million keys total):

Init Performance

Scenario Time Notes
First run (build cache) ~9s Queries SQLite for sorted keys + saves pickle
Cached run ~2.4s Loads sorted_keys + kanji_index from pickle
Original baseline ~49s Old _mem_index approach

Lookup Performance (デジタル大辞泉, 956K keys)

Metric Latency Notes
Cold lookup 0.18ms/word SQLite B-tree + block decompress
Warm lookup 0.0003ms/word Result cache hit
Heavy (200 random) 0.49ms/word Mixed block cache

Architecture

.mdx file ──→ .mdx.db (SQLite index, built once)
                  │
                  ├── .mdx.cache (pickle: sorted_keys + kanji_index)
                  │   └── Loaded at init for search/membership
                  │
                  └── Persistent SQLite connection (immutable, mmap'd)
                      └── Queried on-demand for mdx_lookup()

Key insight: The .mdx.db SQLite files already contain all record positions with a B-tree index. Loading them into a Python dict (_mem_index) was redundant — ~2GB of RAM for data that SQLite can query in 0.18ms. By keeping record data in SQLite and caching only the lightweight key structures, we get:

  • 20x faster init (2.4s vs 49s)
  • ~200MB cache (vs ~636MB with full _mem_index pickle)
  • Zero RAM for record positions (SQLite handles it)
  • Same lookup speed (0.18ms cold, microseconds warm)

SQLite Optimizations (read-only)

  • immutable=1 — no file locking overhead
  • PRAGMA mmap_size=512MB — memory-mapped I/O (zero-copy reads)
  • PRAGMA journal_mode=OFF — no journaling
  • PRAGMA temp_store=MEMORY — temp tables in RAM
  • PRAGMA cache_size=-64000 — 64MB page cache

License

MIT

Metadata

Release files for mdict-mquery 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mdict-mquery 2.1.0
File Size Uploaded Provenance Actions
mdict_mquery-2.1.0.tar.gz 24.3 kB Without provenance Details for mdict_mquery-2.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mdict-mquery 2.1.0
File Interpreter ABI Platform Actions
mdict_mquery-2.1.0-py3-none-any.whl Python 3 none any Details for mdict_mquery-2.1.0-py3-none-any.whl

Total release size: 47.7 kB

Release files / mdict_mquery-2.1.0.tar.gz

Download URL mdict_mquery-2.1.0.tar.gz
Size 24.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d309b53e05b80a8b7a5b2a1787da57614028c98d3fcc1edf7c3cc8bd1929a843
BLAKE2b-256 checksum
How to use checksums
d4c74b67e749e843d94d816554b02a8fb5ba847cabc2747ed6fc34d3fc7d66b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / mdict_mquery-2.1.0-py3-none-any.whl

Download URL mdict_mquery-2.1.0-py3-none-any.whl
Size 23.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6322f0727367ffef441759a349ccc2451521deaf8ce558e9190ca06d08495550
BLAKE2b-256 checksum
How to use checksums
c26c8824c87a9b6dc1ed3b563f1fa6b1e84cfa1643690ee0f5184a0a6777e371
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Security for mdict-mquery 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page