Rationalize the git config settings for index-pack performance.

BUG=

Review URL: https://codereview.chromium.org/202753003

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@257728 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/fetch.py b/fetch.py
index 8b84c99..dfd0386 100755
--- a/fetch.py
+++ b/fetch.py
@@ -109,9 +109,6 @@
     return os.path.exists(os.path.join(os.getcwd(), self.root))
 
   def init(self):
-    # TODO(dpranke): Work around issues w/ delta compression on big repos.
-    self.run_git('config', '--global', 'core.deltaBaseCacheLimit', '1G')
-
     # Configure and do the gclient checkout.
     self.run_gclient('config', '--spec', self.spec['gclient_spec'])
     if self.options.nohooks:
diff --git a/gclient_scm.py b/gclient_scm.py
index 637294d..e8ab140 100644
--- a/gclient_scm.py
+++ b/gclient_scm.py
@@ -230,8 +230,8 @@
       quiet = ['--quiet']
     self._UpdateBranchHeads(options, fetch=False)
 
-    fetch_cmd = [
-      '-c', 'core.deltaBaseCacheLimit=2g', 'fetch', self.remote, '--prune']
+    cfg = gclient_utils.DefaultIndexPackConfig()
+    fetch_cmd = cfg + ['fetch', self.remote, '--prune']
     self._Run(fetch_cmd + quiet, options, retry=True)
     self._Run(['reset', '--hard', revision] + quiet, options)
     self.UpdateSubmoduleConfig()
@@ -700,8 +700,9 @@
       print('')
     template_path = os.path.join(
         os.path.dirname(THIS_FILE_PATH), 'git-templates')
-    clone_cmd = ['-c', 'core.deltaBaseCacheLimit=2g', 'clone', '--no-checkout',
-                 '--progress', '--template=%s' % template_path]
+    cfg = gclient_utils.DefaultIndexPackConfig()
+    clone_cmd = cfg + [
+        'clone', '--no-checkout', '--progress', '--template=%s' % template_path]
     if self.cache_dir:
       clone_cmd.append('--shared')
     if options.verbose:
@@ -911,7 +912,8 @@
                     '^\\+refs/branch-heads/\\*:.*$']
       self._Run(config_cmd, options)
       if fetch:
-        fetch_cmd = ['-c', 'core.deltaBaseCacheLimit=2g', 'fetch', self.remote]
+        cfg = gclient_utils.DefaultIndexPackConfig()
+        fetch_cmd =  cfg + ['fetch', self.remote]
         if options.verbose:
           fetch_cmd.append('--verbose')
         self._Run(fetch_cmd, options, retry=True)
diff --git a/gclient_utils.py b/gclient_utils.py
index 1801a6b..7003fc8 100644
--- a/gclient_utils.py
+++ b/gclient_utils.py
@@ -9,6 +9,7 @@
 import logging
 import os
 import pipes
+import platform
 import Queue
 import re
 import stat
@@ -960,3 +961,23 @@
     # Mac OS 10.6 only
     # pylint: disable=E1101
     return int(os.sysconf('SC_NPROCESSORS_ONLN'))
+
+def DefaultDeltaBaseCacheLimit():
+  """Return a reasonable default for the git config core.deltaBaseCacheLimit.
+
+  The primary constraint is the address space of virtual memory.  The cache
+  size limit is per-thread, and 32-bit systems can hit OOM errors if this
+  parameter is set too high.
+  """
+  if platform.architecture()[0].startswith('64'):
+    return '2g'
+  else:
+    return '512m'
+
+def DefaultIndexPackConfig():
+  """Return reasonable default values for configuring git-index-pack.
+
+  Experiments suggest that higher values for pack.threads don't improve
+  performance."""
+  return ['-c', 'pack.threads=5', '-c',
+          'core.deltaBaseCacheLimit=%s' % DefaultDeltaBaseCacheLimit()]
diff --git a/git_cache.py b/git_cache.py
index dfda1d7..9ef4873 100755
--- a/git_cache.py
+++ b/git_cache.py
@@ -181,8 +181,8 @@
     d = ['--depth', '%d' % options.depth]
 
   def _config(directory):
-    RunGit(['config', 'core.deltaBaseCacheLimit', '2g'],
-           cwd=directory)
+    RunGit(['config', 'core.deltaBaseCacheLimit',
+            gclient_utils.DefaultDeltaBaseCacheLimit()], cwd=directory)
     RunGit(['config', 'remote.origin.url', url],
            cwd=directory)
     RunGit(['config', '--replace-all', 'remote.origin.fetch',