Преглед изворни кода

Support reusing known sha during object creation.

This results in > 10% performance improvement during operations like rev-list.
Jelmer Vernooij пре 11 година
родитељ
комит
ee89c0b394
3 измењених фајлова са 17 додато и 9 уклоњено
  1. 3 0
      NEWS
  2. 1 1
      dulwich/object_store.py
  3. 13 8
      dulwich/objects.py

+ 3 - 0
NEWS

@@ -8,6 +8,9 @@
 
  * Add a basic `dulwich.porcelain` module. (Jelmer Vernooij)
 
+ * Various performance improvements for object access.
+   (Jelmer Vernooij)
+
  CHANGES
 
   * Ref handling has been moved to dulwich.refs.

+ 1 - 1
dulwich/object_store.py

@@ -113,7 +113,7 @@ class BaseObjectStore(object):
     def __getitem__(self, sha):
         """Obtain an object by SHA1."""
         type_num, uncomp = self.get_raw(sha)
-        return ShaFile.from_raw_string(type_num, uncomp)
+        return ShaFile.from_raw_string(type_num, uncomp, sha=sha)
 
     def __iter__(self):
         """Iterate over the SHAs that are present in this store."""

+ 13 - 8
dulwich/objects.py

@@ -291,17 +291,20 @@ class ShaFile(object):
             self._deserialize(self._chunked_text)
             self._needs_parsing = False
 
-    def set_raw_string(self, text):
+    def set_raw_string(self, text, sha=None):
         """Set the contents of this object from a serialized string."""
         if type(text) != str:
             raise TypeError(text)
-        self.set_raw_chunks([text])
+        self.set_raw_chunks([text], sha)
 
-    def set_raw_chunks(self, chunks):
+    def set_raw_chunks(self, chunks, sha=None):
         """Set the contents of this object from a list of chunks."""
         self._chunked_text = chunks
         self._deserialize(chunks)
-        self._sha = None
+        if sha is None:
+            self._sha = None
+        else:
+            self._sha = FixedSha(sha)
         self._needs_parsing = False
         self._needs_serialization = False
 
@@ -403,25 +406,27 @@ class ShaFile(object):
             raise ObjectFormatException("invalid object header")
 
     @staticmethod
-    def from_raw_string(type_num, string):
+    def from_raw_string(type_num, string, sha=None):
         """Creates an object of the indicated type from the raw string given.
 
         :param type_num: The numeric type of the object.
         :param string: The raw uncompressed contents.
+        :param sha: Optional known sha for the object
         """
         obj = object_class(type_num)()
-        obj.set_raw_string(string)
+        obj.set_raw_string(string, sha)
         return obj
 
     @staticmethod
-    def from_raw_chunks(type_num, chunks):
+    def from_raw_chunks(type_num, chunks, sha=None):
         """Creates an object of the indicated type from the raw chunks given.
 
         :param type_num: The numeric type of the object.
         :param chunks: An iterable of the raw uncompressed contents.
+        :param sha: Optional known sha for the object
         """
         obj = object_class(type_num)()
-        obj.set_raw_chunks(chunks)
+        obj.set_raw_chunks(chunks, sha)
         return obj
 
     @classmethod