git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[StGit PATCH 1/5] Create index and worktree objects just once

From
Karl Hasselström <kha@treskal.com>
Date
Jan 29, 2008, 03:02 UTC
Message-ID
<20080129030233.926.84197.stgit@yoghurt>
In-Reply-To
<20080129030059.926.29897.stgit@yoghurt>

Create the objects for a repository's default index, worktree, and index+worktree just once. Both for performance (though the gain is probably negligible), and for future-proofing if we ever add mutable state to those objects.

And make them properties while we're at it.
Signed-off-by: Karl Hasselström <kha@treskal.com>
---
 stgit/commands/coalesce.py |    2 +-
 stgit/commands/commit.py   |    2 +-
 stgit/commands/goto.py     |    2 +-
 stgit/lib/git.py           |   34 ++++++++++++++++++++++++----------
 4 files changed, 27 insertions(+), 13 deletions(-)
diff --git a/stgit/commands/coalesce.py b/stgit/commands/coalesce.py
index d2cba3e..291a537 100644
--- a/stgit/commands/coalesce.py
+++ b/stgit/commands/coalesce.py
@@ -118,5 +118,5 @@ def func(parser, options, args):
                                           + list(stack.patchorder.unapplied)))
     if len(patches) < 2:
         raise common.CmdException('Need at least two patches')
-    return _coalesce(stack, stack.repository.default_iw(), options.name,
+    return _coalesce(stack, stack.repository.default_iw, options.name,
                      options.message, options.save_template, patches)
diff --git a/stgit/commands/commit.py b/stgit/commands/commit.py
index 1d741b3..bff94ce 100644
--- a/stgit/commands/commit.py
+++ b/stgit/commands/commit.py
@@ -68,7 +68,7 @@ def func(parser, options, args):
     if not patches:
         raise common.CmdException('No patches to commit')
 
-    iw = stack.repository.default_iw()
+    iw = stack.repository.default_iw
     trans = transaction.StackTransaction(stack, 'stg commit')
     try:
         common_prefix = 0
diff --git a/stgit/commands/goto.py b/stgit/commands/goto.py
index 763a8af..fe13e49 100644
--- a/stgit/commands/goto.py
+++ b/stgit/commands/goto.py
@@ -35,7 +35,7 @@ def func(parser, options, args):
     patch = args[0]
 
     stack = directory.repository.current_stack
-    iw = stack.repository.default_iw()
+    iw = stack.repository.default_iw
     trans = transaction.StackTransaction(stack, 'stg goto')
     if patch in trans.applied:
         to_pop = set(trans.applied[trans.applied.index(patch)+1:])
diff --git a/stgit/lib/git.py b/stgit/lib/git.py
index 118c9b2..2af1844 100644
--- a/stgit/lib/git.py
+++ b/stgit/lib/git.py
@@ -211,6 +211,9 @@ class Repository(RunWithEnv):
         self.__refs = Refs(self)
         self.__trees = ObjectCache(lambda sha1: Tree(sha1))
         self.__commits = ObjectCache(lambda sha1: Commit(self, sha1))
+        self.__default_index = None
+        self.__default_worktree = None
+        self.__default_iw = None
     env = property(lambda self: { 'GIT_DIR': self.__git_dir })
     @classmethod
     def default(cls):
@@ -220,21 +223,32 @@ class Repository(RunWithEnv):
                                ).output_one_line())
         except run.RunException:
             raise RepositoryException('Cannot find git repository')
+    @property
     def default_index(self):
-        return Index(self, (os.environ.get('GIT_INDEX_FILE', None)
-                            or os.path.join(self.__git_dir, 'index')))
+        if self.__default_index == None:
+            self.__default_index = Index(
+                self, (os.environ.get('GIT_INDEX_FILE', None)
+                       or os.path.join(self.__git_dir, 'index')))
+        return self.__default_index
     def temp_index(self):
         return Index(self, self.__git_dir)
+    @property
     def default_worktree(self):
-        path = os.environ.get('GIT_WORK_TREE', None)
-        if not path:
-            o = run.Run('git', 'rev-parse', '--show-cdup').output_lines()
-            o = o or ['.']
-            assert len(o) == 1
-            path = o[0]
-        return Worktree(path)
+        if self.__default_worktree == None:
+            path = os.environ.get('GIT_WORK_TREE', None)
+            if not path:
+                o = run.Run('git', 'rev-parse', '--show-cdup').output_lines()
+                o = o or ['.']
+                assert len(o) == 1
+                path = o[0]
+            self.__default_worktree = Worktree(path)
+        return self.__default_worktree
+    @property
     def default_iw(self):
-        return IndexAndWorktree(self.default_index(), self.default_worktree())
+        if self.__default_iw == None:
+            self.__default_iw = IndexAndWorktree(self.default_index,
+                                                 self.default_worktree)
+        return self.__default_iw
     directory = property(lambda self: self.__git_dir)
     refs = property(lambda self: self.__refs)
     def cat_object(self, sha1):
Previous: Karl HasselströmNext: Karl Hasselström
Message 3 of 25 in “kha/safe and kha/experimental updated”
  1. Karl HasselströmJan 29, 2008
  2. 0/5 Various cleanupsKarl Hasselström, Jan 29, 2008
  3. 1/5 Create index and worktree objects just onceKarl Hasselström, Jan 29, 2008
  4. 2/5 Wrap excessively long lineKarl Hasselström, Jan 29, 2008
  5. 3/5 Eliminate temp variable that's used just onceKarl Hasselström, Jan 29, 2008
  6. 4/5 Simplify editor selection logicKarl Hasselström, Jan 29, 2008
  7. Peter OberndorferJan 29, 2008
  8. Karl HasselströmJan 30, 2008
  9. Jay SoffianJan 30, 2008
  10. Karl HasselströmJan 30, 2008
  11. 5/5 Let the caller supply the diff text to diffstat()Karl Hasselström, Jan 29, 2008
  12. 0/2 "stg clean" test+bugfixKarl Hasselström, Jan 29, 2008
  13. 1/2 Add test to ensure that "stg clean" preserves conflicting patchesKarl Hasselström, Jan 29, 2008
  14. 2/2 Don't clean away patches with conflictsKarl Hasselström, Jan 29, 2008
  15. 0/4 Rewrite "stg edit" to use new infrastructureKarl Hasselström, Jan 29, 2008
  16. 1/4 Teach new infrastructure about the default author and committerKarl Hasselström, Jan 29, 2008
  17. 2/4 Teach new infrastructure to apply patchesKarl Hasselström, Jan 29, 2008
  18. 3/4 Teach new infrastructure to diff two treesKarl Hasselström, Jan 29, 2008
  19. David KågedalJan 29, 2008
  20. Karl HasselströmJan 29, 2008
  21. 4/4 Convert "stg edit" to the new infrastructureKarl Hasselström, Jan 29, 2008
  22. 0/3 "stg edit" fixupsKarl Hasselström, Feb 1, 2008
  23. 1/3 Parse the date instead of treating it as an opaque stringKarl Hasselström, Feb 1, 2008
  24. 2/3 Convert "stg edit" to the new infrastructureKarl Hasselström, Feb 1, 2008
  25. 3/3 It's possible to edit unapplied patches nowKarl Hasselström, Feb 1, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.