Skip to content

Commit 498a9cc

Browse files
authored
Reduce memory usage by using __slots__ (#4904)
In some cases this reduces the memory usage of mypy and mypy-daemon over 30%. This seems to have only a minor effect on speed. Depending on the workload, this seems to either speed up or slow down runtimes by a few percent. Notes: * Refactor various attribute initializations away from class bodies, as they conflict with __slots__. * This only adds __slots__ to classes that use the most memory, based on the output from the memory profiler. * Some attributes had None defaults even though the type was not optional. Now we use a cast for some of these, which has the same effect. * Various __dict__ manipulations had to be modified to support __slots__ as well.
1 parent 76e763d commit 498a9cc

File tree

6 files changed

+420
-250
lines changed

6 files changed

+420
-250
lines changed

mypy/fixup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def visit_symbol_table(self, symtab: SymbolTable) -> None:
6969
for key, value in list(symtab.items()):
7070
cross_ref = value.cross_ref
7171
if cross_ref is not None: # Fix up cross-reference.
72-
del value.cross_ref
72+
value.cross_ref = None
7373
if cross_ref in self.modules:
7474
value.node = self.modules[cross_ref]
7575
else:
@@ -152,7 +152,7 @@ def visit_instance(self, inst: Instance) -> None:
152152
type_ref = inst.type_ref
153153
if type_ref is None:
154154
return # We've already been here.
155-
del inst.type_ref
155+
inst.type_ref = None
156156
inst.type = lookup_qualified_typeinfo(self.modules, type_ref, self.quick_and_dirty)
157157
# TODO: Is this needed or redundant?
158158
# Also fix up the bases, just in case.

0 commit comments

Comments
 (0)