# HG changeset patch # User Pierre-Yves David # Date 2014-12-04 13:43:40 # Node ID b25f07cb5399e2fa919c81d7425d656df3953051 # Parent 3849b89459b03ebdb82b3b6e8f8e7e1b206a6a14 util: add a 'nogc' decorator to disable the garbage collection Garbage collection behave pathologically when creating a lot of containers. As we do that more than once it become sensible to have a decorator for it. See inline documentation for details. diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -20,6 +20,7 @@ import errno, shutil, sys, tempfile, tra import re as remod import os, time, datetime, calendar, textwrap, signal, collections import imp, socket, urllib +import gc if os.name == 'nt': import windows as platform @@ -544,6 +545,28 @@ def always(fn): def never(fn): return False +def nogc(func): + """disable garbage collector + + Python's garbage collector triggers a GC each time a certain number of + container objects (the number being defined by gc.get_threshold()) are + allocated even when marked not to be tracked by the collector. Tracking has + no effect on when GCs are triggered, only on what objects the GC looks + into. As a workaround, disable GC while building complexe (huge) + containers. + + This garbage collector issue have been fixed in 2.7. + """ + def wrapper(*args, **kwargs): + gcenabled = gc.isenabled() + gc.disable() + try: + return func(*args, **kwargs) + finally: + if gcenabled: + gc.enable() + return wrapper + def pathto(root, n1, n2): '''return the relative path from one place to another. root should use os.sep to separate directories