Skip to content

More efficient (fixed-format) serialization #19668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Aug 15, 2025

This makes deserialization ~2.5x faster compared to orjson. This is fully functional, but still PoC in terms of distribution logic. Some comments:

  • In you want to try this in compiled mode, simply install mypy using MYPY_USE_MYPYC=1, this will install the extension automatically (for now)
  • If you want to just play with the extension, or use it in interpreted mode, use pip install mypyc/lib-rt
  • I translated (de-)serialization logic from JSON methods almost verbatim (including comments)
  • This may be still not the most efficient way to do this, but I wanted to write something simple, that probably still gets us 90% there in terms of performance. I am still open to suggestions however
  • Please forgive me if the PR looks not very polished, I feel tired, but needed some kind of closure on this :-)

Some technical notes:

  • The huge try/except import blob in mypy/cache.py is temporary, it is needed for now to be able to run tests without installing mypy itself (only with test-requirements.txt).
  • There is certain asymmetry with read/write for literals, this is intentional because we allow complex and/or None in some cases, but not in other cases.
  • General convention is that during deserialization the type/symbol marker is consumer by the caller (except for MypyFile, which is special). There is no convention for few classes that are not types/symbols.
  • I add new primitive type for native_internal.Buffer (and possible more type in future from native) for better/automatic method call specializations. If this feels wrong/risky, I can convert this to a more ad-hoc logic in transform_call_expr()

@ilevkivskyi ilevkivskyi requested a review from JukkaL August 15, 2025 14:52

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Oh, hm I completely forgot 32-bit architectures are still a thing. Probably an easy fix, not sure I will have time to take a look at it today, but it should not stop any reviewers from reviewing.

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant