_parallel_compile_patch.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright 2018 The gRPC Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Patches the compile() to allow enable parallel compilation of C/C++.
  15. build_ext has lots of C/C++ files and normally them one by one.
  16. Enabling parallel build helps a lot.
  17. """
  18. import distutils.ccompiler
  19. import os
  20. try:
  21. BUILD_EXT_COMPILER_JOBS = int(
  22. os.environ['GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS'])
  23. except KeyError:
  24. import multiprocessing
  25. BUILD_EXT_COMPILER_JOBS = multiprocessing.cpu_count()
  26. except ValueError:
  27. BUILD_EXT_COMPILER_JOBS = 1
  28. # monkey-patch for parallel compilation
  29. def _parallel_compile(self,
  30. sources,
  31. output_dir=None,
  32. macros=None,
  33. include_dirs=None,
  34. debug=0,
  35. extra_preargs=None,
  36. extra_postargs=None,
  37. depends=None):
  38. # setup the same way as distutils.ccompiler.CCompiler
  39. # https://github.com/python/cpython/blob/31368a4f0e531c19affe2a1becd25fc316bc7501/Lib/distutils/ccompiler.py#L564
  40. macros, objects, extra_postargs, pp_opts, build = self._setup_compile(
  41. str(output_dir), macros, include_dirs, sources, depends, extra_postargs)
  42. cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
  43. def _compile_single_file(obj):
  44. try:
  45. src, ext = build[obj]
  46. except KeyError:
  47. return
  48. self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  49. # run compilation of individual files in parallel
  50. import multiprocessing.pool
  51. multiprocessing.pool.ThreadPool(BUILD_EXT_COMPILER_JOBS).map(
  52. _compile_single_file, objects)
  53. return objects
  54. def monkeypatch_compile_maybe():
  55. """Monkeypatching is dumb, but the build speed gain is worth it."""
  56. if BUILD_EXT_COMPILER_JOBS > 1:
  57. distutils.ccompiler.CCompiler.compile = _parallel_compile