Skip to content

Commit

Permalink
perf: move distutils import into Language.build_library
Browse files Browse the repository at this point in the history
Importing `distutils` takes more than 90% of the total import time of
`tree_sitter`, but it is only used when compiling new languages. The
common case should not need to pay for that.
  • Loading branch information
narpfel authored Nov 12, 2023
1 parent 600438f commit 690bf56
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions tree_sitter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import enum
from ctypes import c_void_p, cdll
from distutils.ccompiler import new_compiler
from distutils.unixccompiler import UnixCCompiler
from os import path
from platform import system
from tempfile import TemporaryDirectory
Expand Down Expand Up @@ -65,13 +63,18 @@ def build_library(output_path: str, repo_paths: List[str]):
path.getmtime(path_) for path_ in source_paths
]

if max(source_mtimes) <= output_mtime:
return False

# local import saves import time in the common case that nothing is
# compiled
from distutils.ccompiler import new_compiler
from distutils.unixccompiler import UnixCCompiler

compiler = new_compiler()
if isinstance(compiler, UnixCCompiler):
compiler.set_executables(compiler_cxx="c++")

if max(source_mtimes) <= output_mtime:
return False

with TemporaryDirectory(suffix="tree_sitter_language") as out_dir:
object_paths = []
for source_path in source_paths:
Expand Down

0 comments on commit 690bf56

Please sign in to comment.