Skip to content

Commit 9a2c625

Browse files
committed
Use more aggressive gc thresholds, and tune earlier
In some cases gc was consuming a significant fraction of CPU, so run gc less often. Also tune gc very early, to run gc less during import time -- not sure if this has any significant impact, but in an unrelated project this was somewhat helpful. This makes incremental checking of torch 27% faster (based on 100 measurements), and also speeds up self check by about 10% (both incremental and non-incremental).
1 parent 7e79c4a commit 9a2c625

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

mypy/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
from __future__ import annotations
44

5+
from mypy.gctune import tune_gc
6+
7+
# Tune GC as early as possible, before most imports
8+
tune_gc()
9+
510
import os
611
import sys
712
import traceback

mypy/build.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
import collections
1717
import contextlib
1818
import errno
19-
import gc
2019
import json
2120
import os
22-
import platform
2321
import re
2422
import stat
2523
import sys
@@ -46,6 +44,7 @@
4644
from mypy.checker import TypeChecker
4745
from mypy.error_formatter import OUTPUT_CHOICES, ErrorFormatter
4846
from mypy.errors import CompileError, ErrorInfo, Errors, report_internal_error
47+
from mypy.gctune import tune_gc
4948
from mypy.graph_utils import prepare_sccs, strongly_connected_components, topsort
5049
from mypy.indirection import TypeIndirectionVisitor
5150
from mypy.messages import MessageBuilder
@@ -217,9 +216,9 @@ def _build(
217216
stderr: TextIO,
218217
extra_plugins: Sequence[Plugin],
219218
) -> BuildResult:
220-
if platform.python_implementation() == "CPython":
221-
# This seems the most reasonable place to tune garbage collection.
222-
gc.set_threshold(150 * 1000)
219+
# We tune gc in __main__, but also do it here in case we are called from
220+
# another entry point such as a test.
221+
tune_gc()
223222

224223
data_dir = default_data_dir()
225224
fscache = fscache or FileSystemCache()

mypy/gctune.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Tuning cyclic garbage collector (GC) parameters for better performance.
2+
3+
Since this is called very early, before most imports, don't add new import
4+
dependencies!
5+
"""
6+
7+
import gc
8+
import platform
9+
10+
11+
def tune_gc() -> None:
12+
if platform.python_implementation() == "CPython":
13+
# Run gc less frequently, as otherwise we can spent a large fraction of
14+
# cpu in gc.
15+
gc.set_threshold(200 * 1000, 30, 30)

mypyc/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
from __future__ import annotations
1414

15+
from mypy.gctune import tune_gc
16+
17+
# Tune GC as early as possible, before most imports
18+
tune_gc()
19+
1520
import os
1621
import os.path
1722
import subprocess

0 commit comments

Comments
 (0)