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

Merged
merged 14 commits into from
Aug 19, 2025

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()

Related issue: #3456

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

This comment has been minimized.

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 18, 2025

(Not a full review.)

Nice, this is a major improvement! I measured a ~35% performance improvement in import torch when cached, compared to using the old cache format. Once deserialization is faster, we can hopefully further improve warm runtimes by improving the performance of remaining bottlenecks.

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 19, 2025

I did a quick review, and this seems good enough to merge (or at least close enough). Let's leave this open for another day in case somebody still wants to have a look. Before making this an official feature, there are some changes that I think would be nice to have (beyond a way of distributing this), but these are better done as follow-up PRs and I won't discuss them here. I also have some smaller comments that are easier for me to address in a follow-up PR than writing detailed review comments here.

I also want to remove the JSON based format eventually, maybe a few releases after we've made this the default. This may require a way of converting the binary cache to JSON, since at least at Dropbox we have some tools that process mypy cache files, and processing a binary format in tools sounds pretty painful. I have some ideas about how to achieve this without making the serialization code significantly harder to maintain or less efficient. I'll write about this in a follow-up issue.

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 19, 2025

Added link to related issue #3456 in summary.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting another day doesn't bring any value, so let's merge this soon. Left one comment, otherwise looks good. We can start iterating on this once this is merged.

mypy/main.py Outdated
incremental_group.add_argument(
"--fixed-format-cache",
action="store_true",
help="Use experimental binary fixed format cache",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide the flag from --help output (help=argparse.SUPPRESS) until we've figured out how to distribute this properly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hoped we will do this before next mypy release, but sure, we can expose this easily when ready.

Copy link
Contributor

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

@ilevkivskyi
Copy link
Member Author

@JukkaL

Added link to related issue #3456 in summary.

Oh, I completely forgot about that issue, and I see you even posted a proposal there. It is interesting that I wrote something very similar to what you propose, few comments:

  • I actually had write_short_int() and read_short_int() for type/symbol tags (one byte per tag) at some point. I didn't include them in the PR because it had only minor effect on performance. But they will definitely allow to cut a big chunk of cache size (IIRC they reduced cache size by at least 20% in my experiments).
  • As you mentioned, attribute name tags are technically redundant, and actually debugging was not that hard without them, so I think we don't really need them.
  • I think we can add few dedicated tags for most common instances like builtins.str and return same object for them (essentially what we do in type-checker with named_type()).

Before making this an official feature, there are some changes that I think would be nice to have (beyond a way of distributing this), but these are better done as follow-up PRs and I won't discuss them here. I also have some smaller comments that are easier for me to address in a follow-up PR than writing detailed review comments here.

OK, yeah, this will definitely speed things up.

@ilevkivskyi ilevkivskyi merged commit 657bdd8 into python:master Aug 19, 2025
20 checks passed
@ilevkivskyi ilevkivskyi deleted the ff-cache branch August 19, 2025 19:33
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.

2 participants