Skip to content

Commit be51e9c

Browse files
authored
Add proper lazy deserialization (python#21198)
On Linux with Python 3.12 this gives (compared to 1.20.0): * almost 2x less memory overhead per parallel worker * 3-4% faster cold parallel runs * (most notably) ~2x faster `mypy -c 'import torch'` with warm cache, on my desktop warm torch now takes 250-300ms
1 parent c6d938e commit be51e9c

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

mypy/nodes.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4767,6 +4767,8 @@ class SymbolTableNode:
47674767
__slots__ = (
47684768
"kind",
47694769
"_node",
4770+
"_node_bytes",
4771+
"_node_tag",
47704772
"module_public",
47714773
"module_hidden",
47724774
"cross_ref",
@@ -4790,6 +4792,8 @@ def __init__(
47904792
) -> None:
47914793
self.kind = kind
47924794
self._node = node
4795+
self._node_bytes = b""
4796+
self._node_tag: Tag = 0
47934797
self.module_public = module_public
47944798
self.implicit = implicit
47954799
self.module_hidden = module_hidden
@@ -4824,6 +4828,9 @@ def node(self) -> SymbolNode | None:
48244828
if self.cross_ref is not None:
48254829
node_fixer.resolve_cross_ref(self)
48264830
else:
4831+
if self._node is None:
4832+
self._node = read_symbol(ReadBuffer(self._node_bytes), self._node_tag)
4833+
self._node_bytes = b""
48274834
node = self._node
48284835
assert node is not None
48294836
if self.stored_info is not None:
@@ -4958,11 +4965,8 @@ def read(cls, data: ReadBuffer) -> SymbolTableNode:
49584965
if tag == TYPE_INFO:
49594966
sym._node = TypeInfo.read(data)
49604967
else:
4961-
# This logic is temporary, to make sure we don't introduce
4962-
# regressions until we have proper lazy deserialization.
4963-
# It has negligible performance impact.
4964-
node_bytes = extract_symbol(data)
4965-
sym._node = read_symbol(ReadBuffer(node_bytes), tag)
4968+
sym._node_bytes = extract_symbol(data)
4969+
sym._node_tag = tag
49664970
sym.unfixed = True
49674971
else:
49684972
sym.cross_ref = cross_ref

0 commit comments

Comments
 (0)