Mercurial > hg
comparison mercurial/util.py @ 51549:a452807df09b
nocg: make the utility work are both a decorator and context manager
In some case, the context manager version will be simpler.
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Tue, 26 Mar 2024 13:28:52 +0000 |
parents | 187c5769a629 |
children | 59f846fbc11d |
comparison
equal
deleted
inserted
replaced
51548:1b17eeba9deb | 51549:a452807df09b |
---|---|
33 import time | 33 import time |
34 import traceback | 34 import traceback |
35 import warnings | 35 import warnings |
36 | 36 |
37 from typing import ( | 37 from typing import ( |
38 Any, | |
38 Iterable, | 39 Iterable, |
39 Iterator, | 40 Iterator, |
40 List, | 41 List, |
41 Optional, | 42 Optional, |
42 Tuple, | 43 Tuple, |
1810 | 1811 |
1811 def never(fn): | 1812 def never(fn): |
1812 return False | 1813 return False |
1813 | 1814 |
1814 | 1815 |
1815 def nogc(func): | 1816 def nogc(func=None) -> Any: |
1816 """disable garbage collector | 1817 """disable garbage collector |
1817 | 1818 |
1818 Python's garbage collector triggers a GC each time a certain number of | 1819 Python's garbage collector triggers a GC each time a certain number of |
1819 container objects (the number being defined by gc.get_threshold()) are | 1820 container objects (the number being defined by gc.get_threshold()) are |
1820 allocated even when marked not to be tracked by the collector. Tracking has | 1821 allocated even when marked not to be tracked by the collector. Tracking has |
1823 containers. | 1824 containers. |
1824 | 1825 |
1825 This garbage collector issue have been fixed in 2.7. But it still affect | 1826 This garbage collector issue have been fixed in 2.7. But it still affect |
1826 CPython's performance. | 1827 CPython's performance. |
1827 """ | 1828 """ |
1828 | 1829 if func is None: |
1830 return _nogc_context() | |
1831 else: | |
1832 return _nogc_decorator(func) | |
1833 | |
1834 | |
1835 @contextlib.contextmanager | |
1836 def _nogc_context(): | |
1837 gcenabled = gc.isenabled() | |
1838 gc.disable() | |
1839 try: | |
1840 yield | |
1841 finally: | |
1842 if gcenabled: | |
1843 gc.enable() | |
1844 | |
1845 | |
1846 def _nogc_decorator(func): | |
1829 def wrapper(*args, **kwargs): | 1847 def wrapper(*args, **kwargs): |
1830 gcenabled = gc.isenabled() | 1848 with _nogc_context(): |
1831 gc.disable() | |
1832 try: | |
1833 return func(*args, **kwargs) | 1849 return func(*args, **kwargs) |
1834 finally: | |
1835 if gcenabled: | |
1836 gc.enable() | |
1837 | 1850 |
1838 return wrapper | 1851 return wrapper |
1839 | 1852 |
1840 | 1853 |
1841 if pycompat.ispypy: | 1854 if pycompat.ispypy: |